Skip to content

Commit

Permalink
Make tests use mock version of särmä client
Browse files Browse the repository at this point in the history
  • Loading branch information
Wnt committed Feb 13, 2025
1 parent 1352481 commit 9b71078
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 14 deletions.
9 changes: 7 additions & 2 deletions service/src/main/kotlin/fi/espoo/evaka/EvakaEnv.kt
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,15 @@ private fun snakeCaseName(job: Enum<*>): String =

data class ArchiveEnv(
/** URL up to the endpoint name e.g. http://10.0.0.10/archive-core */
val url: URI
val url: URI,
val useMockClient: Boolean
) {

companion object {
fun fromEnvironment(env: Environment) =
ArchiveEnv(url = URI.create(env.lookup("evaka.integration.särmä.url")))
ArchiveEnv(
url = URI.create(env.lookup("evaka.integration.särmä.url")),
useMockClient = env.lookup("evaka.integration.särmä.use_mock_client") ?: false
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ArchiveChildDocumentService(
logger.info { "Successfully archived document $documentId" }
} else {
logger.error {
"Failed to archive document $documentId. Response code: $responseCode, Response body: ${responseBody?.string() ?: "No response body"}"
"Failed to archive document $documentId. Response code: $responseCode, Response body: ${responseBody ?: "No response body"}"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fi.espoo.evaka.document.archival

import fi.espoo.evaka.ArchiveEnv
import fi.espoo.evaka.EvakaEnv
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class SärmäClientConfig {

@Bean
fun getSärmäClient(evakaEnv: EvakaEnv, archiveEnv: ArchiveEnv?): SärmäClientInterface {
if (!evakaEnv.särmäEnabled || archiveEnv?.useMockClient == true) {
return SärmäMockClient()
}
return SärmäHttpClient(archiveEnv)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ interface SärmäClientInterface {
masterId: String,
classId: String,
virtualArchiveId: String,
): Pair<Int, ResponseBody?>
): Pair<Int, String?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import java.time.Duration
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import org.springframework.stereotype.Component

@Component
class SärmäClient(private val archiveEnv: ArchiveEnv?) : SärmäClientInterface {
class SärmäHttpClient(private val archiveEnv: ArchiveEnv?) : SärmäClientInterface {
private val httpClient =
OkHttpClient.Builder()
.connectTimeout(Duration.ofMinutes(1))
Expand All @@ -26,7 +24,7 @@ class SärmäClient(private val archiveEnv: ArchiveEnv?) : SärmäClientInterfac
masterId: String,
classId: String,
virtualArchiveId: String,
): Pair<Int, ResponseBody?> {
): Pair<Int, String?> {
if (archiveEnv == null) {
throw IllegalStateException("Archive environment not configured")
}
Expand Down Expand Up @@ -59,8 +57,6 @@ class SärmäClient(private val archiveEnv: ArchiveEnv?) : SärmäClientInterfac
httpClient
.newCall(Request.Builder().url(endpointUrl).post(requestBody).build())
.execute()
val responseCode = response.code
val responseBody = response.body
return Pair(responseCode, responseBody)
return Pair(response.code, response.body?.string())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fi.espoo.evaka.document.archival

import fi.espoo.evaka.s3.Document

class SärmäMockClient: SärmäClientInterface {
override fun putDocument(
documentContent: Document,
masterId: String,
classId: String,
virtualArchiveId: String
): Pair<Int, String?> {
return Pair(200, "OK")
}
}
7 changes: 4 additions & 3 deletions service/src/main/resources/application-local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ evaka:
trust_store:
password: password
type: JKS
särmä:
enabled: true
url: https://eoy3xw8tftt5shn.m.pipedream.net/archive-core
# särmä:
# enabled: true
# use_mock_client: true
# url: https://eoy3xw8tftt5shn.m.pipedream.net/archive-core
not_for_prod:
force_unpublish_document_template_enabled: true
jwt:
Expand Down

0 comments on commit 9b71078

Please sign in to comment.