From c6596809ffabd341abef9c345d11e50fbd2dc1cc Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Thu, 4 Jul 2024 17:03:44 +0100 Subject: Refactor integration tests so only one DmiDispatcher is used Issue-ID: CPS-2303 Signed-off-by: danielhanrahan Change-Id: I4d04fb9b7693a769e9059d3b2b7257a714da0532 --- .../onap/cps/integration/base/DmiDispatcher.groovy | 39 ++++++++++++++++------ .../NcmpBearerTokenPassthroughSpec.groovy | 27 ++------------- 2 files changed, 32 insertions(+), 34 deletions(-) (limited to 'integration-test') diff --git a/integration-test/src/test/groovy/org/onap/cps/integration/base/DmiDispatcher.groovy b/integration-test/src/test/groovy/org/onap/cps/integration/base/DmiDispatcher.groovy index 6676cb74c2..e77815f5ec 100644 --- a/integration-test/src/test/groovy/org/onap/cps/integration/base/DmiDispatcher.groovy +++ b/integration-test/src/test/groovy/org/onap/cps/integration/base/DmiDispatcher.groovy @@ -22,11 +22,11 @@ package org.onap.cps.integration.base import static org.onap.cps.integration.base.CpsIntegrationSpecBase.readResourceDataFile -import org.springframework.http.HttpHeaders import java.util.regex.Matcher import okhttp3.mockwebserver.Dispatcher import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.RecordedRequest +import org.springframework.http.HttpHeaders import org.springframework.http.HttpStatus import org.springframework.http.MediaType @@ -53,25 +53,40 @@ class DmiDispatcher extends Dispatcher { static final MODULE_RESOURCES_RESPONSE_TEMPLATE = readResourceDataFile('mock-dmi-responses/moduleResourcesTemplate.json') def isAvailable = true - Map> moduleNamesPerCmHandleId = [:] + def lastAuthHeaderReceived @Override MockResponse dispatch(RecordedRequest request) { if (!isAvailable) { - return new MockResponse().setResponseCode(HttpStatus.SERVICE_UNAVAILABLE.value()) + return mockResponse(HttpStatus.SERVICE_UNAVAILABLE) } + if (request.path == '/actuator/health') { + return mockResponseWithBody(HttpStatus.OK, '{"status":"UP"}') + } + + lastAuthHeaderReceived = request.getHeader('Authorization') switch (request.path) { - case ~/^\/dmi\/v1\/ch\/(.*)\/modules$/: + // get module references for a CM-handle + case ~'^/dmi/v1/ch/(.*)/modules$': def cmHandleId = Matcher.lastMatcher[0][1] return getModuleReferencesResponse(cmHandleId) - case ~/^\/dmi\/v1\/ch\/(.*)\/moduleResources$/: + // get module resources for a CM-handle + case ~'^/dmi/v1/ch/(.*)/moduleResources$': def cmHandleId = Matcher.lastMatcher[0][1] return getModuleResourcesResponse(cmHandleId) + // pass-through data operation for a CM-handle + case ~'^/dmi/v1/ch/(.*)/data/ds/(.*)$': + return mockResponseWithBody(HttpStatus.OK, '{}') + + // legacy pass-through batch data operation + case ~'^/dmi/v1/data$': + return mockResponseWithBody(HttpStatus.ACCEPTED, '{}') + default: - throw new IllegalArgumentException('Mock DMI does not handle path ' + request.path) + throw new IllegalArgumentException('Mock DMI does not implement endpoint ' + request.path) } } @@ -79,14 +94,14 @@ class DmiDispatcher extends Dispatcher { def moduleReferences = '{"schemas":[' + getModuleNamesForCmHandle(cmHandleId).collect { MODULE_REFERENCES_RESPONSE_TEMPLATE.replaceAll("", it) }.join(',') + ']}' - return mockOkResponseWithBody(moduleReferences) + return mockResponseWithBody(HttpStatus.OK, moduleReferences) } private getModuleResourcesResponse(cmHandleId) { def moduleResources = '[' + getModuleNamesForCmHandle(cmHandleId).collect { MODULE_RESOURCES_RESPONSE_TEMPLATE.replaceAll("", it) }.join(',') + ']' - return mockOkResponseWithBody(moduleResources) + return mockResponseWithBody(HttpStatus.OK, moduleResources) } private getModuleNamesForCmHandle(cmHandleId) { @@ -96,9 +111,13 @@ class DmiDispatcher extends Dispatcher { return moduleNamesPerCmHandleId.get(cmHandleId) } - private static mockOkResponseWithBody(responseBody) { + private static mockResponse(status) { + return new MockResponse().setResponseCode(status.value()) + } + + private static mockResponseWithBody(status, responseBody) { return new MockResponse() - .setResponseCode(HttpStatus.OK.value()) + .setResponseCode(status.value()) .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) .setBody(responseBody) } diff --git a/integration-test/src/test/groovy/org/onap/cps/integration/functional/NcmpBearerTokenPassthroughSpec.groovy b/integration-test/src/test/groovy/org/onap/cps/integration/functional/NcmpBearerTokenPassthroughSpec.groovy index 2a35313f74..664fca82e5 100644 --- a/integration-test/src/test/groovy/org/onap/cps/integration/functional/NcmpBearerTokenPassthroughSpec.groovy +++ b/integration-test/src/test/groovy/org/onap/cps/integration/functional/NcmpBearerTokenPassthroughSpec.groovy @@ -20,13 +20,8 @@ package org.onap.cps.integration.functional -import okhttp3.mockwebserver.Dispatcher -import okhttp3.mockwebserver.MockResponse -import okhttp3.mockwebserver.RecordedRequest -import org.jetbrains.annotations.NotNull import org.onap.cps.integration.base.CpsIntegrationSpecBase import org.springframework.http.HttpHeaders -import org.springframework.http.HttpStatus import org.springframework.http.MediaType import spock.util.concurrent.PollingConditions @@ -40,25 +35,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. class NcmpBearerTokenPassthroughSpec extends CpsIntegrationSpecBase { - def lastAuthHeaderReceived = null - def setup() { dmiDispatcher.moduleNamesPerCmHandleId['ch-1'] = ['M1', 'M2'] registerCmHandle(DMI_URL, 'ch-1', NO_MODULE_SET_TAG) - - mockDmiServer.setDispatcher(new Dispatcher() { - @Override - MockResponse dispatch(@NotNull RecordedRequest request) throws InterruptedException { - if (request.path == '/actuator/health') { - return new MockResponse() - .addHeader("Content-Type", MediaType.APPLICATION_JSON).setBody('{"status":"UP"}') - .setResponseCode(HttpStatus.OK.value()) - } else { - lastAuthHeaderReceived = request.getHeader('Authorization') - return new MockResponse().setResponseCode(HttpStatus.OK.value()) - } - } - }) } def cleanup() { @@ -75,7 +54,7 @@ class NcmpBearerTokenPassthroughSpec extends CpsIntegrationSpecBase { .andExpect(status().is2xxSuccessful()) then: 'DMI has received request with bearer token' - lastAuthHeaderReceived == 'Bearer some-bearer-token' + assert dmiDispatcher.lastAuthHeaderReceived == 'Bearer some-bearer-token' where: 'all HTTP operations are applied' httpMethod << [GET, POST, PUT, PATCH, DELETE] @@ -91,7 +70,7 @@ class NcmpBearerTokenPassthroughSpec extends CpsIntegrationSpecBase { .andExpect(status().is2xxSuccessful()) then: 'DMI has received request with no authorization header' - lastAuthHeaderReceived == null + assert dmiDispatcher.lastAuthHeaderReceived == null where: 'all HTTP operations are applied' httpMethod << [GET, POST, PUT, PATCH, DELETE] @@ -115,7 +94,7 @@ class NcmpBearerTokenPassthroughSpec extends CpsIntegrationSpecBase { then: 'DMI will receive the async request with bearer token' new PollingConditions().within(3, () -> { - assert lastAuthHeaderReceived == 'Bearer some-bearer-token' + assert dmiDispatcher.lastAuthHeaderReceived == 'Bearer some-bearer-token' }) } -- cgit 1.2.3-korg