Merge "Get Node API fix"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / event / lcm / LcmEventsCreatorSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022 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.event.lcm
22
23 import org.onap.cps.ncmp.api.inventory.CmHandleState
24 import org.onap.cps.ncmp.api.inventory.CompositeState
25 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
26 import org.onap.ncmp.cmhandle.event.lcm.Values
27 import spock.lang.Specification
28
29 import static org.onap.cps.ncmp.api.inventory.CmHandleState.ADVISED
30 import static org.onap.cps.ncmp.api.inventory.CmHandleState.DELETING
31 import static org.onap.cps.ncmp.api.inventory.CmHandleState.READY
32
33 class LcmEventsCreatorSpec extends Specification {
34
35     def objectUnderTest = new LcmEventsCreator()
36     def cmHandleId = 'test-cm-handle'
37
38     def 'Map the LcmEvent for #operation'() {
39         given: 'NCMP cm handle details with current and old details'
40             def existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: existingCmHandleState),
41                 publicProperties: ['publicProperty1': 'value1', 'publicProperty2': 'value2', 'publicProperty3': 'value3'])
42             def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: false, cmHandleState: targetCmHandleState),
43                 publicProperties: ['publicProperty1': 'value1', 'publicProperty2': 'value22'])
44         when: 'the event is populated'
45             def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
46         then: 'event header is mapped correctly'
47             assert result.eventSource == 'org.onap.ncmp'
48             assert result.eventCorrelationId == cmHandleId
49             assert result.eventType == LcmEventType.UPDATE.eventType
50         and: 'event payload is mapped correctly with correct cmhandle id'
51             assert result.event.cmHandleId == cmHandleId
52         and: 'it should have correct old values'
53             assert result.event.oldValues.cmHandleState == expectedExistingCmHandleState
54             assert result.event.oldValues.dataSyncEnabled == true
55         and: 'the correct new values'
56             assert result.event.newValues.cmHandleState == expectedTargetCmHandleState
57             assert result.event.newValues.dataSyncEnabled == false
58         and: 'cmhandle properties are just the one which are differing'
59             assert result.event.oldValues.cmHandleProperties == [['publicProperty2': 'value2', 'publicProperty3': 'value3']]
60             assert result.event.newValues.cmHandleProperties == [['publicProperty2': 'value22']]
61         where: 'following parameters are provided'
62             operation  | existingCmHandleState | targetCmHandleState || expectedExistingCmHandleState | expectedTargetCmHandleState
63             'UPDATE'   | ADVISED               | READY               || Values.CmHandleState.ADVISED  | Values.CmHandleState.READY
64             'DELETING' | READY                 | DELETING            || Values.CmHandleState.READY    | Values.CmHandleState.DELETING
65
66
67     }
68
69     def 'Map the LcmEvent for operation CREATE'() {
70         given: 'NCMP cm handle details'
71             def targetNcmpServiceCmhandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: false, cmHandleState: READY),
72                 publicProperties: ['publicProperty1': 'value11', 'publicProperty2': 'value22'])
73             def existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: ['publicProperty1': 'value1', 'publicProperty2': 'value2'])
74         when: 'the event is populated'
75             def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmhandle, existingNcmpServiceCmHandle)
76         then: 'event header is mapped correctly'
77             assert result.eventSource == 'org.onap.ncmp'
78             assert result.eventCorrelationId == cmHandleId
79             assert result.eventType == LcmEventType.CREATE.eventType
80         and: 'event payload is mapped correctly'
81             assert result.event.cmHandleId == cmHandleId
82             assert result.event.newValues.cmHandleState == Values.CmHandleState.READY
83             assert result.event.newValues.dataSyncEnabled == false
84             assert result.event.newValues.cmHandleProperties == [['publicProperty1': 'value11', 'publicProperty2': 'value22']]
85         and: 'it should not have any old values'
86             assert result.event.oldValues == null
87     }
88
89     def 'Map the LcmEvent for DELETE operation'() {
90         given: 'NCMP cm handle details'
91             def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: false, cmHandleState: CmHandleState.DELETED),
92                 publicProperties: ['publicProperty1': 'value11', 'publicProperty2': 'value22'])
93             def existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: DELETING),
94                 publicProperties: ['publicProperty1': 'value1'])
95         when: 'the event is populated'
96             def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
97         then: 'event header is mapped correctly'
98             assert result.eventSource == 'org.onap.ncmp'
99             assert result.eventCorrelationId == cmHandleId
100             assert result.eventType == LcmEventType.DELETE.eventType
101         and: 'event payload is mapped correctly '
102             assert result.event.cmHandleId == cmHandleId
103             assert result.event.oldValues == null
104             assert result.event.newValues == null
105     }
106 }