Skip to content

Instantly share code, notes, and snippets.

@iainporter
iainporter / FilePollingConfiguration.java
Created August 28, 2020 16:27
Config for directories
@Configuration
public class FilePollingConfiguration {
@Bean(name="inboundReadDirectory")
public File inboundReadDirectory(@Value("${inbound.read.path}") String path) {
return makeDirectory(path);
}
@Bean(name="inboundProcessedDirectory")
public File inboundProcessedDirectory(@Value("${inbound.processed.path}") String path) {
@iainporter
iainporter / native-profile.xml
Created August 28, 2020 11:06
Native profile for building native image in qaurkus
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
@iainporter
iainporter / GetAccessToken.kt
Created August 15, 2020 15:26
get access token for tests
private fun getAccessToken(): String {
val clientId = "backend-service"
val clientSecret = "8155b2ad-cd9d-48ae-a5e1-ea11d5cfcb79"
val response = RestAssured.given()
.auth().preemptive().basic(clientId, clientSecret)
.contentType("application/x-www-form-urlencoded")
.formParam("grant_type", "client_credentials")
.post("http://localhost:$authServerPort/auth/realms/porterhead/protocol/openid-connect/token")
.then()
.extract()
@iainporter
iainporter / KeycloakContainer.kt
Created August 15, 2020 15:24
Set up keycloak container
var keycloakContainer = KGenericContainer("quay.io/keycloak/keycloak:11.0.0")
.withNetwork(network)
.withNetworkAliases("keycloak")
.withExposedPorts(8080)
.withEnv("KEYCLOAK_USER", "admin")
.withEnv("KEYCLOAK_PASSWORD", "admin")
.withEnv("KEYCLOAK_IMPORT", "/tmp/realm.json")
.withEnv("JAVA_OPTS", "-Dkeycloak.profile.feature.scripts=enabled -Dkeycloak.profile.feature.upload_scripts=enabled")
.withClasspathResourceMapping("config/porterhead-realm.json", "/tmp/realm.json", BindMode.READ_ONLY)
.waitingFor(Wait.forHttp("/auth"))
@iainporter
iainporter / SendSmsResourceTest.kt
Created August 15, 2020 15:11
unit test sending a message
@Test
@DisplayName("POST /v1/sms with valid request returns 202")
fun testSuccessfulSend() {
whenever(smsService.createMessage(any(), any())).thenReturn(testDouble())
given()
.`when`()
.contentType(ContentType.JSON)
.auth().oauth2(generateJWT(keyPair))
.body("""{"text":"Hello World", "fromNumber":"+1234567890", "toNumber":"+1234567899"}""")
.post("/v1/sms")
@iainporter
iainporter / GenerateToken.kt
Last active August 15, 2020 15:07
Generate a JWT
fun generateJWT(keyPair: RSAKey): String {
val signer: JWSSigner = RSASSASigner(keyPair.toRSAKey())
// Prepare JWT with claims set
val claimsSet = JWTClaimsSet.Builder()
.subject("backend-service")
.issuer("https://example.com")
.expirationTime(Date(Date().time + 60 * 1000))
.build()
val signedJWT = SignedJWT(
JWSHeader.Builder(JWSAlgorithm.RS256).keyID(keyPair.keyID).type(JOSEObjectType.JWT).build(),
@iainporter
iainporter / PublicKeyStub,kt
Created August 15, 2020 15:02
Public key mock response
postStubMapping(publicKeysStub(keyPair.toPublicJWK().toJSONString()))
private fun publicKeysStub(keys: String): String {
return """
{
"name": "public_keys_stub",
"request": {
"method": "GET",
"url": "/v1/keys"
},
@iainporter
iainporter / OidcConfigurationStub.kt
Created August 15, 2020 11:36
add wiremock stub for well known oidc endpoint
private fun oidcConfigurationStub(): String {
return """
{
"name": "oidc_configuration",
"request": {
"method": "GET",
"url": "/mock-server/.well-known/openid-configuration"
},
"response": {
"status": 200,
@iainporter
iainporter / PrivateKey.kt
Created August 15, 2020 11:33
generate a private key
fun generatePrivateKey() :RSAKey {
return RSAKeyGenerator(2048)
.keyID("123").keyUse(KeyUse.SIGNATURE)
.generate()
}
@iainporter
iainporter / WiremockTestResource.kt
Created August 15, 2020 11:29
Test resource for quarkus
open class WiremockTestResource : QuarkusTestResourceLifecycleManager {
lateinit var wireMockServer: WireMockServer
lateinit var keyPair: RSAKey
override fun start(): MutableMap<String, String> {
wireMockServer = WireMockServer(WireMockConfiguration().dynamicPort())
wireMockServer.start()
keyPair = generatePrivateKey()
postStubMapping(oidcConfigurationStub())