summaryrefslogtreecommitdiffstats
path: root/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy')
-rw-r--r--src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy56
1 files changed, 51 insertions, 5 deletions
diff --git a/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy b/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy
index 66612960..9d6bc358 100644
--- a/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy
+++ b/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy
@@ -20,8 +20,13 @@
package org.onap.cps.ncmp.dmi.service
+import com.fasterxml.jackson.core.JsonProcessingException
+import com.fasterxml.jackson.databind.ObjectMapper
+import org.onap.cps.ncmp.dmi.config.DmiPluginConfig
+import org.onap.cps.ncmp.dmi.exception.CmHandleRegistrationException
import org.onap.cps.ncmp.dmi.exception.DmiException
import org.onap.cps.ncmp.dmi.exception.ModulesNotFoundException
+import org.onap.cps.ncmp.dmi.service.client.NcmpRestClient
import org.onap.cps.ncmp.dmi.service.operation.SdncOperations
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
@@ -29,13 +34,13 @@ import spock.lang.Specification
class DmiServiceImplSpec extends Specification {
- def objectUnderTest = new DmiServiceImpl()
+ def mockNcmpRestClient = Mock(NcmpRestClient)
+ def mockDmiPluginProperties = Mock(DmiPluginConfig.DmiPluginProperties)
+ def objectMapper = new ObjectMapper()
+ def mockObjectMapper = Mock(ObjectMapper)
def mockSdncOperations = Mock(SdncOperations)
-
- def setup() {
- objectUnderTest.sdncOperations = mockSdncOperations
- }
+ def objectUnderTest = new DmiServiceImpl(mockDmiPluginProperties, mockNcmpRestClient, objectMapper, mockSdncOperations)
def 'Call get modules for cm-handle on dmi Service.'() {
given: 'cm handle id'
@@ -70,4 +75,45 @@ class DmiServiceImplSpec extends Specification {
then: 'ModulesNotFoundException is thrown'
thrown( ModulesNotFoundException )
}
+
+ def 'Register cm handles with ncmp.'() {
+ given: 'cm-handle list and json payload'
+ def givenCmHandlesList = ['node1', 'node2']
+ def expectedJson = '{"dmiPlugin":"test-dmi-service","createdCmHandles":[{"cmHandle":"node1"},{"cmHandle":"node2"}]}'
+ and: 'mockDmiPluginProperties returns test-dmi-service'
+ mockDmiPluginProperties.getDmiServiceName() >> 'test-dmi-service'
+ when: 'register cm handles service method with the given cm handles'
+ objectUnderTest.registerCmHandles(givenCmHandlesList)
+ then: 'register cm handle with ncmp called once and return "created" status'
+ 1 * mockNcmpRestClient.registerCmHandlesWithNcmp(expectedJson) >> new ResponseEntity<>(HttpStatus.CREATED)
+ }
+
+ def 'Register cm handles with ncmp called with exception #scenario.'() {
+ given: 'cm-handle list'
+ def cmHandlesList = ['node1', 'node2']
+ and: 'dmi plugin service name is "test-dmi-service"'
+ mockDmiPluginProperties.getDmiServiceName() >> 'test-dmi-service'
+ and: 'ncmp rest client returns #responseEntity'
+ mockNcmpRestClient.registerCmHandlesWithNcmp(_ as String) >> responseEntity
+ when: 'register cm handles service method called'
+ objectUnderTest.registerCmHandles(cmHandlesList)
+ then: 'a registration exception is thrown'
+ thrown(CmHandleRegistrationException.class)
+ where: 'given #scenario'
+ scenario | responseEntity
+ 'ncmp rest client returns bad request' | new ResponseEntity<>(HttpStatus.BAD_REQUEST)
+ 'ncmp rest client returns internal server error'| new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR)
+ }
+
+ def 'Register cm handles with ncmp with wrong data.'() {
+ given: 'objectMapper mock and cm-handle list'
+ def cmHandlesList = ['node1', 'node2']
+ and: 'objectMapper returns "JsonProcessingException" during parse'
+ objectUnderTest.objectMapper = mockObjectMapper
+ mockObjectMapper.writeValueAsString(_) >> { throw new JsonProcessingException('some error.') }
+ when: 'register cm handles service method called'
+ objectUnderTest.registerCmHandles(cmHandlesList)
+ then: 'a dmi exception is thrown'
+ thrown(DmiException.class)
+ }
}