From c454bb912798d90ee0d6cb6fc56c8926dc120bb1 Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Tue, 26 Mar 2024 18:03:36 +0000 Subject: [PATCH] [BUG] Mock DMI will respond to Health Checks during tests Health Check for DMI is sometimes running during integration tests, causing the mockDmiServer to fail due to unexpected Rest requests. As a fix, the mock DMI will respond to health checks. Issue-ID: CPS-2167 Signed-off-by: danielhanrahan Change-Id: Ie4a0059583e66dfcc00e1aa998671a662ccf053a --- .../integration/base/CpsIntegrationSpecBase.groovy | 22 ++++++------- .../NcmpBearerTokenPassthroughSpec.groovy | 7 +++-- .../functional/NcmpCmHandleCreateSpec.groovy | 7 +++-- .../functional/NcmpCmHandleUpgradeSpec.groovy | 36 +++++++++++++--------- 4 files changed, 42 insertions(+), 30 deletions(-) diff --git a/integration-test/src/test/groovy/org/onap/cps/integration/base/CpsIntegrationSpecBase.groovy b/integration-test/src/test/groovy/org/onap/cps/integration/base/CpsIntegrationSpecBase.groovy index 2603c48ed..51b02387e 100644 --- a/integration-test/src/test/groovy/org/onap/cps/integration/base/CpsIntegrationSpecBase.groovy +++ b/integration-test/src/test/groovy/org/onap/cps/integration/base/CpsIntegrationSpecBase.groovy @@ -50,6 +50,7 @@ import org.springframework.context.annotation.ComponentScan import org.springframework.data.jpa.repository.config.EnableJpaRepositories import org.springframework.http.HttpStatus import org.springframework.http.MediaType +import org.springframework.test.web.client.ExpectedCount import org.springframework.test.web.client.MockRestServiceServer import org.springframework.test.web.servlet.MockMvc import org.springframework.web.client.RestTemplate @@ -120,10 +121,10 @@ abstract class CpsIntegrationSpecBase extends Specification { MockRestServiceServer mockDmiServer = null - static final DMI_URL = 'http://mock-dmi-server' - - def static GENERAL_TEST_DATASPACE = 'generalTestDataspace' - def static BOOKSTORE_SCHEMA_SET = 'bookstoreSchemaSet' + static DMI_URL = 'http://mock-dmi-server' + static NO_MODULE_SET_TAG = '' + static GENERAL_TEST_DATASPACE = 'generalTestDataspace' + static BOOKSTORE_SCHEMA_SET = 'bookstoreSchemaSet' def static initialized = false def now = OffsetDateTime.now() @@ -137,10 +138,6 @@ abstract class CpsIntegrationSpecBase extends Specification { mockDmiServer = MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build() } - def cleanup() { - mockDmiServer.reset() - } - def static readResourceDataFile(filename) { return new File('src/test/resources/data/' + filename).text } @@ -203,15 +200,13 @@ abstract class CpsIntegrationSpecBase extends Specification { // *** NCMP Integration Test Utilities *** - def registerCmHandle(dmiPlugin, cmHandleId, moduleSetTag, dmiModuleReferencesResponse, dmiModuleResourcesResponse) { + def registerCmHandle(dmiPlugin, cmHandleId, moduleSetTag) { def cmHandleToCreate = new NcmpServiceCmHandle(cmHandleId: cmHandleId, moduleSetTag: moduleSetTag) networkCmProxyDataService.updateDmiRegistrationAndSyncModule(new DmiPluginRegistration(dmiPlugin: dmiPlugin, createdCmHandles: [cmHandleToCreate])) - mockDmiResponsesForModuleSync(dmiPlugin, cmHandleId, dmiModuleReferencesResponse, dmiModuleResourcesResponse) moduleSyncWatchdog.moduleSyncAdvisedCmHandles() new PollingConditions().within(3, () -> { CmHandleState.READY == networkCmProxyDataService.getCmHandleCompositeState(cmHandleId).cmHandleState }) - mockDmiServer.reset() } def deregisterCmHandle(dmiPlugin, cmHandleId) { @@ -234,6 +229,11 @@ abstract class CpsIntegrationSpecBase extends Specification { .andRespond(withStatus(HttpStatus.SERVICE_UNAVAILABLE)) } + def mockDmiWillRespondToHealthChecks(dmiPlugin) { + mockDmiServer.expect(ExpectedCount.between(0, Integer.MAX_VALUE), requestTo("${dmiPlugin}/actuator/health")) + .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body('{"status":"UP"}')) + } + def overrideCmHandleLastUpdateTime(cmHandleId, newUpdateTime) { String ISO_TIMESTAMP_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; DateTimeFormatter ISO_TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern(ISO_TIMESTAMP_PATTERN); 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 0dabbf30a..28c428046 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 @@ -40,12 +40,15 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. class NcmpBearerTokenPassthroughSpec extends CpsIntegrationSpecBase { - static final NO_MODULE_SET_TAG = '' static final MODULE_REFERENCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreAWithModules_M1_M2_Response.json') static final MODULE_RESOURCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreAWithModules_M1_M2_ResourcesResponse.json') def setup() { - registerCmHandle(DMI_URL, 'ch-1', NO_MODULE_SET_TAG, MODULE_REFERENCES_RESPONSE, MODULE_RESOURCES_RESPONSE) + mockDmiWillRespondToHealthChecks(DMI_URL) + mockDmiResponsesForModuleSync(DMI_URL, 'ch-1', MODULE_REFERENCES_RESPONSE, MODULE_RESOURCES_RESPONSE) + registerCmHandle(DMI_URL, 'ch-1', NO_MODULE_SET_TAG) + mockDmiServer.reset() + mockDmiWillRespondToHealthChecks(DMI_URL) } def cleanup() { diff --git a/integration-test/src/test/groovy/org/onap/cps/integration/functional/NcmpCmHandleCreateSpec.groovy b/integration-test/src/test/groovy/org/onap/cps/integration/functional/NcmpCmHandleCreateSpec.groovy index f03872d56..a6b516cd7 100644 --- a/integration-test/src/test/groovy/org/onap/cps/integration/functional/NcmpCmHandleCreateSpec.groovy +++ b/integration-test/src/test/groovy/org/onap/cps/integration/functional/NcmpCmHandleCreateSpec.groovy @@ -48,6 +48,7 @@ class NcmpCmHandleCreateSpec extends CpsIntegrationSpecBase { def setup() { objectUnderTest = networkCmProxyDataService + mockDmiWillRespondToHealthChecks(DMI_URL) } def 'CM Handle registration is successful.'() { @@ -122,8 +123,10 @@ class NcmpCmHandleCreateSpec extends CpsIntegrationSpecBase { def 'Create a CM-handle with existing moduleSetTag.'() { given: 'existing CM-handles cm-1 with moduleSetTag "A", and cm-2 with moduleSetTag "B"' - registerCmHandle(DMI_URL, 'ch-1', 'A', MODULE_REFERENCES_RESPONSE_A, MODULE_RESOURCES_RESPONSE_A) - registerCmHandle(DMI_URL, 'ch-2', 'B', MODULE_REFERENCES_RESPONSE_B, MODULE_RESOURCES_RESPONSE_B) + mockDmiResponsesForModuleSync(DMI_URL, 'ch-1', MODULE_REFERENCES_RESPONSE_A, MODULE_RESOURCES_RESPONSE_A) + mockDmiResponsesForModuleSync(DMI_URL, 'ch-2', MODULE_REFERENCES_RESPONSE_B, MODULE_RESOURCES_RESPONSE_B) + registerCmHandle(DMI_URL, 'ch-1', 'A') + registerCmHandle(DMI_URL, 'ch-2', 'B') assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences('ch-1').moduleName.sort() assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences('ch-2').moduleName.sort() diff --git a/integration-test/src/test/groovy/org/onap/cps/integration/functional/NcmpCmHandleUpgradeSpec.groovy b/integration-test/src/test/groovy/org/onap/cps/integration/functional/NcmpCmHandleUpgradeSpec.groovy index c5c59e05c..5421ad323 100644 --- a/integration-test/src/test/groovy/org/onap/cps/integration/functional/NcmpCmHandleUpgradeSpec.groovy +++ b/integration-test/src/test/groovy/org/onap/cps/integration/functional/NcmpCmHandleUpgradeSpec.groovy @@ -41,23 +41,24 @@ class NcmpCmHandleUpgradeSpec extends CpsIntegrationSpecBase { static final INITIAL_MODULE_RESOURCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreAWithModules_M1_M2_ResourcesResponse.json') static final UPDATED_MODULE_REFERENCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreBWithModules_M1_M3_Response.json') static final UPDATED_MODULE_RESOURCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreBWithModules_M1_M3_ResourcesResponse.json') - static final NO_MODULE_SET_TAG = '' static final CM_HANDLE_ID = 'ch-1' static final CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG = 'ch-2' def setup() { objectUnderTest = networkCmProxyDataService + mockDmiWillRespondToHealthChecks(DMI_URL) } def 'Upgrade CM-handle with new moduleSetTag or no moduleSetTag.'() { - given: 'an existing CM-handle with expected initial modules: M1 and M2' - registerCmHandle(DMI_URL, CM_HANDLE_ID, initialModuleSetTag, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE) - assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort() - + given: 'DMI will return modules for initial registration' + mockDmiResponsesForModuleSync(DMI_URL, CM_HANDLE_ID, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE) and: 'DMI returns different modules for upgrade' mockDmiResponsesForModuleSync(DMI_URL, CM_HANDLE_ID, UPDATED_MODULE_REFERENCES_RESPONSE, UPDATED_MODULE_RESOURCES_RESPONSE) - when: "CM-handle is upgraded with given moduleSetTag '${updatedModuleSetTag}'" + when: 'a CM-handle is created with expected initial modules: M1 and M2' + registerCmHandle(DMI_URL, CM_HANDLE_ID, initialModuleSetTag) + assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort() + and: "the CM-handle is upgraded with given moduleSetTag '${updatedModuleSetTag}'" def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: updatedModuleSetTag) def dmiPluginRegistrationResponse = networkCmProxyDataService.updateDmiRegistrationAndSyncModule( new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade)) @@ -101,11 +102,14 @@ class NcmpCmHandleUpgradeSpec extends CpsIntegrationSpecBase { } def 'Upgrade CM-handle with existing moduleSetTag.'() { - given: "an existing CM-handle handle with moduleSetTag '${updatedModuleSetTag}'" - registerCmHandle(DMI_URL, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG, updatedModuleSetTag, UPDATED_MODULE_REFERENCES_RESPONSE, UPDATED_MODULE_RESOURCES_RESPONSE) + given: 'DMI will return modules for registration' + mockDmiResponsesForModuleSync(DMI_URL, CM_HANDLE_ID, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE) + mockDmiResponsesForModuleSync(DMI_URL, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG, UPDATED_MODULE_REFERENCES_RESPONSE, UPDATED_MODULE_RESOURCES_RESPONSE) + and: "an existing CM-handle handle with moduleSetTag '${updatedModuleSetTag}'" + registerCmHandle(DMI_URL, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG, updatedModuleSetTag) assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG).moduleName.sort() and: "a CM-handle with moduleSetTag '${initialModuleSetTag}' which will be upgraded" - registerCmHandle(DMI_URL, CM_HANDLE_ID, initialModuleSetTag, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE) + registerCmHandle(DMI_URL, CM_HANDLE_ID, initialModuleSetTag) assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort() when: "CM-handle is upgraded to moduleSetTag '${updatedModuleSetTag}'" @@ -142,7 +146,8 @@ class NcmpCmHandleUpgradeSpec extends CpsIntegrationSpecBase { def 'Skip upgrade of CM-handle with same moduleSetTag as before.'() { given: 'an existing CM-handle with expected initial modules: M1 and M2' - registerCmHandle(DMI_URL, CM_HANDLE_ID, 'same', INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE) + mockDmiResponsesForModuleSync(DMI_URL, CM_HANDLE_ID, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE) + registerCmHandle(DMI_URL, CM_HANDLE_ID, 'same') assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort() when: 'CM-handle is upgraded with the same moduleSetTag' @@ -164,13 +169,14 @@ class NcmpCmHandleUpgradeSpec extends CpsIntegrationSpecBase { } def 'Upgrade of CM-handle fails due to DMI error.'() { - given: 'an existing CM-handle' - registerCmHandle(DMI_URL, CM_HANDLE_ID, 'oldTag', INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE) - - and: 'DMI returns error code' + given: 'DMI will return modules for initial registration' + mockDmiResponsesForModuleSync(DMI_URL, CM_HANDLE_ID, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE) + and: 'DMI returns error code for upgrade' mockDmiServer.expect(anything()).andRespond(withStatus(HttpStatus.SERVICE_UNAVAILABLE)) - when: "CM-handle is upgraded" + when: 'a CM-handle is created' + registerCmHandle(DMI_URL, CM_HANDLE_ID, 'oldTag') + and: 'the CM-handle is upgraded' def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: 'newTag') networkCmProxyDataService.updateDmiRegistrationAndSyncModule( new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade)) -- 2.16.6