Skip to content

Commit

Permalink
Flytter HttpHeaderTestRestTemplate hit fra commons-test repo
Browse files Browse the repository at this point in the history
  • Loading branch information
simhos committed Dec 20, 2023
1 parent de8a591 commit df69c76
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import io.kotest.assertions.assertSoftly
import io.mockk.every
import no.nav.bidrag.commons.util.KildesystemIdenfikator
import no.nav.bidrag.commons.web.HttpResponse
import no.nav.bidrag.commons.web.test.HttpHeaderTestRestTemplate
import no.nav.bidrag.dokument.security.HttpHeaderTestRestTemplate
import no.nav.bidrag.dokument.service.JournalpostService
import no.nav.bidrag.transport.dokument.JournalpostResponse
import no.nav.security.token.support.spring.test.EnableMockOAuth2Server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import io.mockk.verify
import no.nav.bidrag.commons.web.HttpResponse.Companion.from
import no.nav.bidrag.commons.web.test.HttpHeaderTestRestTemplate
import no.nav.bidrag.dokument.consumer.BidragDokumentConsumer.Companion.createEnhetHeader
import no.nav.bidrag.dokument.security.HttpHeaderTestRestTemplate
import no.nav.bidrag.dokument.service.JournalpostService
import no.nav.security.token.support.spring.test.EnableMockOAuth2Server
import org.assertj.core.api.Assertions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package no.nav.bidrag.dokument.controller

import no.nav.bidrag.commons.web.test.HttpHeaderTestRestTemplate
import no.nav.bidrag.dokument.BidragDokumentTest
import no.nav.bidrag.dokument.consumer.stub.RestConsumerStub
import no.nav.bidrag.dokument.security.HttpHeaderTestRestTemplate
import no.nav.bidrag.transport.dokument.DokumentTilgangResponse
import no.nav.security.token.support.spring.test.EnableMockOAuth2Server
import org.assertj.core.api.Assertions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.http.HttpHeader
import com.github.tomakehurst.wiremock.matching.StringValuePattern
import no.nav.bidrag.commons.web.EnhetFilter
import no.nav.bidrag.commons.web.test.HttpHeaderTestRestTemplate
import no.nav.bidrag.dokument.BidragDokumentTest
import no.nav.bidrag.dokument.consumer.BidragDokumentConsumer
import no.nav.bidrag.dokument.consumer.BidragDokumentConsumer.Companion.createEnhetHeader
import no.nav.bidrag.dokument.consumer.stub.RestConsumerStub
import no.nav.bidrag.dokument.security.HttpHeaderTestRestTemplate
import no.nav.bidrag.transport.dokument.AvvikType
import no.nav.bidrag.transport.dokument.Avvikshendelse
import no.nav.bidrag.transport.dokument.BehandleAvvikshendelseResponse
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package no.nav.bidrag.dokument.security

import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.boot.test.web.client.exchange
import org.springframework.core.ParameterizedTypeReference
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.ResponseEntity
import java.net.URI
import java.util.Stack
import kotlin.collections.HashMap


class HttpHeaderTestRestTemplate(val testRestTemplate: TestRestTemplate) {

private val headersForSingleCallbacks = Stack<Pair<String, String>>()
private val valueGenerators: MutableMap<String, ValueGenerator> = HashMap()
fun <T> exchange(url: String?, httpMethod: HttpMethod?, httpEntity: HttpEntity<*>, responseClass: Class<T>?): ResponseEntity<T> {
return testRestTemplate.exchange(url, httpMethod, newEntityWithAddedHeaders(httpEntity), responseClass)
}

fun <T> exchange(
url: String?,
httpMethod: HttpMethod?,
httpEntity: HttpEntity<*>?,
typeReference: ParameterizedTypeReference<T>?
): ResponseEntity<T> {
return testRestTemplate.exchange(url, httpMethod, newEntityWithAddedHeaders(httpEntity), typeReference)
}

fun <T> postForEntity(url: String?, httpEntity: HttpEntity<*>, responseClass: Class<T>?): ResponseEntity<T> {
return testRestTemplate.postForEntity(url, newEntityWithAddedHeaders(httpEntity), responseClass)
}

inline fun <reified T : Any> getForEntity(uri: URI, request: Any? = null): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.GET, newEntityWithAddedHeaders(request))
}

inline fun <reified T : Any> putForEntity(uri: URI, request: Any? = null): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.PUT, newEntityWithAddedHeaders(request))
}

inline fun <reified T : Any> postForEntity(uri: URI, request: Any? = null): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.POST, newEntityWithAddedHeaders(request))
}

inline fun <reified T : Any> patchForEntity(uri: URI, request: Any? = null): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.PATCH, newEntityWithAddedHeaders(request))
}

inline fun <reified T : Any> optionsForEntity(uri: URI, request: Any? = null): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.OPTIONS, newEntityWithAddedHeaders(request))
}

inline fun <reified T : Any> delete(uri: URI): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.DELETE, newEntityWithAddedHeaders())
}

inline fun <reified T : Any> getForEntity(uri: String, request: Any? = null): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.GET, newEntityWithAddedHeaders(request))
}

inline fun <reified T : Any> putForEntity(uri: String, request: Any? = null): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.PUT, newEntityWithAddedHeaders(request))
}

inline fun <reified T : Any> postForEntity(uri: String, request: Any? = null): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.POST, newEntityWithAddedHeaders(request))
}

inline fun <reified T : Any> patchForEntity(uri: String, request: Any? = null): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.PATCH, newEntityWithAddedHeaders(request))
}

inline fun <reified T : Any> optionsForEntity(uri: String, request: Any? = null): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.OPTIONS, newEntityWithAddedHeaders(request))
}

inline fun <reified T : Any> delete(uri: String): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.DELETE, newEntityWithAddedHeaders())
}

fun newEntityWithAddedHeaders(request: Any? = null): HttpEntity<*> {
val tempHeaders = HttpHeaders().apply {
valueGenerators.forEach { add(it.key, it.value.generate()) }
}

while (!headersForSingleCallbacks.empty()) {
val headerWithValue = headersForSingleCallbacks.pop()
tempHeaders.add(headerWithValue.first, headerWithValue.second)
}

if (request is HttpEntity<*>) {
return HttpEntity(request.body, tempHeaders.apply { putAll(request.headers) })
}
return HttpEntity(request, tempHeaders)
}

fun add(headerName: String, valueGenerator: ValueGenerator) {
valueGenerators[headerName] = valueGenerator
}

fun addHeaderForSingleHttpEntityCallback(headerName: String, headerValue: String) {
val headerWithValue = headerName to headerValue
headersForSingleCallbacks.push(headerWithValue)
}

fun interface ValueGenerator {
fun generate(): String?
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.nimbusds.jose.JOSEObjectType;
import java.util.List;
import java.util.Map;
import no.nav.bidrag.commons.web.test.HttpHeaderTestRestTemplate;
import no.nav.security.mock.oauth2.MockOAuth2Server;
import no.nav.security.mock.oauth2.token.DefaultOAuth2TokenCallback;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -18,12 +17,14 @@
@Configuration
@Profile(TEST_PROFILE)
public class HttpHeaderTestRestTemplateConfiguration {

@Autowired
private MockOAuth2Server mockOAuth2Server;

@Bean
HttpHeaderTestRestTemplate securedTestRestTemplate(TestRestTemplate testRestTemplate) {
HttpHeaderTestRestTemplate httpHeaderTestRestTemplate = new HttpHeaderTestRestTemplate(testRestTemplate);
httpHeaderTestRestTemplate.add(HttpHeaders.AUTHORIZATION, ()->generateTestToken());
httpHeaderTestRestTemplate.add(HttpHeaders.AUTHORIZATION, this::generateTestToken);

return httpHeaderTestRestTemplate;
}
Expand Down

0 comments on commit df69c76

Please sign in to comment.