Refactor integration tests so only one DmiDispatcher is used 91/138391/1
authordanielhanrahan <daniel.hanrahan@est.tech>
Thu, 4 Jul 2024 16:03:44 +0000 (17:03 +0100)
committerdanielhanrahan <daniel.hanrahan@est.tech>
Thu, 4 Jul 2024 16:03:44 +0000 (17:03 +0100)
Issue-ID: CPS-2303
Signed-off-by: danielhanrahan <daniel.hanrahan@est.tech>
Change-Id: I4d04fb9b7693a769e9059d3b2b7257a714da0532

integration-test/src/test/groovy/org/onap/cps/integration/base/DmiDispatcher.groovy
integration-test/src/test/groovy/org/onap/cps/integration/functional/NcmpBearerTokenPassthroughSpec.groovy

index 6676cb7..e77815f 100644 (file)
@@ -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<String, List<String>> 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("<MODULE_NAME>", it)
         }.join(',') + ']}'
-        return mockOkResponseWithBody(moduleReferences)
+        return mockResponseWithBody(HttpStatus.OK, moduleReferences)
     }
 
     private getModuleResourcesResponse(cmHandleId) {
         def moduleResources = '[' + getModuleNamesForCmHandle(cmHandleId).collect {
             MODULE_RESOURCES_RESPONSE_TEMPLATE.replaceAll("<MODULE_NAME>", 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)
     }
index 2a35313..664fca8 100644 (file)
 
 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'
             })
     }