Merge "Update postman collection to utilize newest yang files"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / utils / AlternateIdCheckerSpec.groovy
1 /*
2  * ============LICENSE_START========================================================
3  * Copyright (c) 2024 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.utils
22
23 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence
24 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
25 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
26 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
27 import org.onap.cps.spi.model.DataNode
28 import org.onap.cps.spi.model.DataNodeBuilder
29 import spock.lang.Specification
30
31 class AlternateIdCheckerSpec extends Specification {
32
33     def mockInventoryPersistenceService = Mock(InventoryPersistence)
34     def someDataNode = new DataNodeBuilder().build()
35     def dataNodeFoundException = new DataNodeNotFoundException('', '')
36
37     def objectUnderTest = new AlternateIdChecker(mockInventoryPersistenceService)
38
39     def 'Check new cm handle with new alternate id.'() {
40         given: 'inventory persistence can not find cm handle id'
41             mockInventoryPersistenceService.getYangModelCmHandle('ch 1') >> {throw dataNodeFoundException}
42         and: 'inventory persistence can not find alternate id'
43             mockInventoryPersistenceService.getCmHandleDataNodeByAlternateId('alternate id') >> {throw dataNodeFoundException}
44         expect: 'mapping can be added'
45              assert objectUnderTest.canApplyAlternateId('ch 1', 'alternate id')
46     }
47
48     def 'Check new cm handle with used alternate id.'() {
49         given: 'inventory persistence can not find cm handle id'
50             mockInventoryPersistenceService.getYangModelCmHandle('ch 1') >> {throw dataNodeFoundException}
51         and: 'inventory persistence can find alternate id'
52             mockInventoryPersistenceService.getCmHandleDataNodeByAlternateId('alternate id') >> { someDataNode }
53         expect: 'mapping can not be added'
54             assert objectUnderTest.canApplyAlternateId('ch 1', 'alternate id') == false
55     }
56
57     def 'Check for existing cm handle with #currentAlternateId.'() {
58         given: 'a cm handle with the #currentAlternateId'
59             def yangModelCmHandle = new YangModelCmHandle(alternateId: currentAlternateId)
60         and: 'inventory service finds the cm handle'
61             mockInventoryPersistenceService.getYangModelCmHandle('my cm handle') >> yangModelCmHandle
62         expect: 'add mapping returns expected result'
63             assert canAdd == objectUnderTest.canApplyAlternateId('my cm handle', 'same alternate id')
64         where: 'following alternate ids is used'
65             currentAlternateId   || canAdd
66             'same alternate id'  || true
67             'other alternate id' || false
68     }
69
70     def 'Check a batch of created cm handles with #scenario.'() {
71         given: 'a batch of 2 new cm handles alternate id ids #alt1 and #alt2'
72             def batch = [new NcmpServiceCmHandle(cmHandleId: 'ch-1', alternateId: alt1),
73                          new NcmpServiceCmHandle(cmHandleId: 'ch-2', alternateId: alt2)]
74         and: 'the database already contains cm handle(s) with these alternate ids: #altAlreadyInDb'
75             mockInventoryPersistenceService.getCmHandleDataNodeByAlternateId(_) >>
76                 {  args -> altAlreadyInDb.contains(args[0]) ? new DataNode() : throwDataNodeNotFoundException() }
77         when: 'the batch of new cm handles is checked'
78             def result = objectUnderTest.getIdsOfCmHandlesWithRejectedAlternateId(batch, AlternateIdChecker.Operation.CREATE)
79         then: 'the result contains ids of the rejected cm handles'
80             assert result == expectedRejectedCmHandleIds
81         where: 'the following alternate ids are used'
82             scenario                          | alt1   | alt2   | altAlreadyInDb  || expectedRejectedCmHandleIds
83             'no alternate ids'                | ''     | ''     | ['dont matter'] || []
84             'new alternate ids'               | 'fdn1' | 'fdn2' | ['other fdn']   || []
85             'one already used alternate id'   | 'fdn1' | 'fdn2' | ['fdn1']        || ['ch-1']
86             'duplicate alternate id in batch' | 'fdn1' | 'fdn1' | ['dont matter'] || ['ch-2']
87     }
88
89     def 'Check a batch of updates to existing cm handles with #scenario.'() {
90         given: 'a batch of 1 existing cm handle update alternate id to #proposedAlt'
91             def batch = [new NcmpServiceCmHandle(cmHandleId: 'ch-1', alternateId: proposedAlt)]
92         and: 'the database already contains a cm handle with alternate id: #altAlreadyInDb'
93             mockInventoryPersistenceService.getCmHandleDataNodeByAlternateId(_) >>
94                     {  args -> altAlreadyInDb.equals(args[0]) ? new DataNode() : throwDataNodeNotFoundException() }
95             mockInventoryPersistenceService.getYangModelCmHandle(_) >> new YangModelCmHandle(alternateId: altAlreadyInDb)
96         when: 'the batch of cm handle updates is checked'
97             def result = objectUnderTest.getIdsOfCmHandlesWithRejectedAlternateId(batch, AlternateIdChecker.Operation.UPDATE)
98         then: 'the result contains ids of the rejected cm handles'
99             assert result == expectedRejectedCmHandleIds
100         where: 'the following parameters are used'
101             scenario                      | proposedAlt | altAlreadyInDb || expectedRejectedCmHandleIds
102             'no alternate id'             | 'fdn1'      | ''             || []
103             'used the same alternate id'  | 'fdn1'      | 'fdn1'         || []
104             'used different alternate id' | 'otherFdn'  | 'fdn1'         || ['ch-1']
105     }
106
107     def throwDataNodeNotFoundException() {
108         // cannot 'return' an exception in conditional stub behavior, so hence a method call that will always throw this exception
109         throw dataNodeFoundException
110     }
111
112 }