Refactoring Subscription Create LCM use case
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / cmsubscription / CmSubscriptionDmiOutEventToCmSubscriptionNcmpOutEventMapperSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (c) 2023 Nordix Foundation.
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an 'AS IS' BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.events.cmsubscription
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.mapstruct.factory.Mappers
25 import org.onap.cps.ncmp.events.cmsubscription1_0_0.dmi_to_ncmp.CmSubscriptionDmiOutEvent
26 import org.onap.cps.ncmp.events.cmsubscription1_0_0.dmi_to_ncmp.SubscriptionStatus
27 import org.onap.cps.ncmp.utils.TestUtils
28 import org.onap.cps.spi.exceptions.DataValidationException
29 import org.onap.cps.utils.JsonObjectMapper
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.boot.test.context.SpringBootTest
32 import spock.lang.Specification
33
34
35 @SpringBootTest(classes = [JsonObjectMapper, ObjectMapper])
36 class CmSubscriptionDmiOutEventToCmSubscriptionNcmpOutEventMapperSpec extends Specification {
37
38     CmSubscriptionDmiOutEventToCmSubscriptionNcmpOutEventMapper objectUnderTest = Mappers.getMapper(CmSubscriptionDmiOutEventToCmSubscriptionNcmpOutEventMapper)
39
40     @Autowired
41     JsonObjectMapper jsonObjectMapper
42
43     def 'Map subscription event response to subscription event outcome'() {
44         given: 'a Subscription Response Event'
45             def subscriptionResponseJsonData = TestUtils.getResourceFileContent('cmSubscriptionDmiOutEvent.json')
46             def subscriptionResponseEvent = jsonObjectMapper.convertJsonString(subscriptionResponseJsonData, CmSubscriptionDmiOutEvent.class)
47         when: 'the subscription response event is mapped to a subscription event outcome'
48             def result = objectUnderTest.toCmSubscriptionNcmpOutEvent(subscriptionResponseEvent)
49         then: 'the resulting subscription event outcome contains expected pending targets per details grouping'
50             def pendingCmHandleTargetsPerDetails = result.getData().getAdditionalInfo().getPending()
51             assert pendingCmHandleTargetsPerDetails.get(0).getDetails() == 'No reply from DMI yet'
52             assert pendingCmHandleTargetsPerDetails.get(0).getTargets() == ['CMHandle3', 'CMHandle4']
53         and: 'the resulting subscription event outcome contains expected rejected targets per details grouping'
54             def rejectedCmHandleTargetsPerDetails = result.getData().getAdditionalInfo().getRejected()
55             assert rejectedCmHandleTargetsPerDetails.get(0).getDetails() == 'Some other error message from the DMI'
56             assert rejectedCmHandleTargetsPerDetails.get(0).getTargets() == ['CMHandle2']
57             assert rejectedCmHandleTargetsPerDetails.get(1).getDetails() == 'Some error message from the DMI'
58             assert rejectedCmHandleTargetsPerDetails.get(1).getTargets() == ['CMHandle1']
59     }
60
61     def 'Map subscription event response with null of subscription status list to subscription event outcome causes an exception'() {
62         given: 'a Subscription Response Event'
63             def subscriptionResponseJsonData = TestUtils.getResourceFileContent('cmSubscriptionDmiOutEvent.json')
64             def subscriptionResponseEvent = jsonObjectMapper.convertJsonString(subscriptionResponseJsonData, CmSubscriptionDmiOutEvent.class)
65         and: 'set subscription status list to null'
66             subscriptionResponseEvent.getData().setSubscriptionStatus(subscriptionStatusList)
67         when: 'the subscription response event is mapped to a subscription event outcome'
68             objectUnderTest.toCmSubscriptionNcmpOutEvent(subscriptionResponseEvent)
69         then: 'a DataValidationException is thrown with an expected exception details'
70             def exception = thrown(DataValidationException)
71             exception.details == 'SubscriptionStatus list cannot be null or empty'
72         where: 'the following values are used'
73             scenario                            ||     subscriptionStatusList
74             'A null subscription status list'   ||      null
75             'An empty subscription status list' ||      new ArrayList<SubscriptionStatus>()
76     }
77
78     def 'Map subscription event response with subscription status list to subscription event outcome without any exception'() {
79         given: 'a Subscription Response Event'
80             def subscriptionResponseJsonData = TestUtils.getResourceFileContent('cmSubscriptionDmiOutEvent.json')
81             def subscriptionResponseEvent = jsonObjectMapper.convertJsonString(subscriptionResponseJsonData, CmSubscriptionDmiOutEvent.class)
82         when: 'the subscription response event is mapped to a subscription event outcome'
83             objectUnderTest.toCmSubscriptionNcmpOutEvent(subscriptionResponseEvent)
84         then: 'no exception thrown'
85             noExceptionThrown()
86     }
87 }