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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.utils
24 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence
25 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
26 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
27 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
28 import org.onap.cps.spi.model.DataNode
29 import org.onap.cps.spi.model.DataNodeBuilder
30 import spock.lang.Specification
32 class AlternateIdCheckerSpec extends Specification {
34 def mockInventoryPersistenceService = Mock(InventoryPersistence)
35 def someDataNode = new DataNodeBuilder().build()
36 def dataNodeFoundException = new DataNodeNotFoundException('', '')
38 def objectUnderTest = new AlternateIdChecker(mockInventoryPersistenceService)
40 def 'Check new cm handle with new alternate id.'() {
41 given: 'inventory persistence can not find cm handle id'
42 mockInventoryPersistenceService.getYangModelCmHandle('ch 1') >> {throw dataNodeFoundException}
43 and: 'inventory persistence can not find alternate id'
44 mockInventoryPersistenceService.getCmHandleDataNodeByAlternateId('alternate id') >> {throw dataNodeFoundException}
45 expect: 'mapping can be added'
46 assert objectUnderTest.canApplyAlternateId('ch 1', 'alternate id')
49 def 'Check new cm handle with used alternate id.'() {
50 given: 'inventory persistence can not find cm handle id'
51 mockInventoryPersistenceService.getYangModelCmHandle('ch 1') >> {throw dataNodeFoundException}
52 and: 'inventory persistence can find alternate id'
53 mockInventoryPersistenceService.getCmHandleDataNodeByAlternateId('alternate id') >> { someDataNode }
54 expect: 'mapping can not be added'
55 assert objectUnderTest.canApplyAlternateId('ch 1', 'alternate id') == false
58 def 'Check for existing cm handle with #currentAlternateId.'() {
59 given: 'a cm handle with the #currentAlternateId'
60 def yangModelCmHandle = new YangModelCmHandle(alternateId: currentAlternateId)
61 and: 'inventory service finds the cm handle'
62 mockInventoryPersistenceService.getYangModelCmHandle('my cm handle') >> yangModelCmHandle
63 expect: 'add mapping returns expected result'
64 assert canAdd == objectUnderTest.canApplyAlternateId('my cm handle', 'same alternate id')
65 where: 'following alternate ids is used'
66 currentAlternateId || canAdd
67 'same alternate id' || true
68 'other alternate id' || false
71 def 'Check a batch of NEW cm handles with #scenario.'() {
72 given: 'a batch of 2 new cm handles alternate id ids #alt1 and #alt2'
73 def batch = [new NcmpServiceCmHandle(cmHandleId: 'ch-1', alternateId: alt1),
74 new NcmpServiceCmHandle(cmHandleId: 'ch-2', alternateId: alt2)]
75 and: 'the database already contains cm handle(s) with these alternate ids: #alreadyinDb'
76 mockInventoryPersistenceService.getCmHandleDataNodeByAlternateId(_) >>
77 { args -> altAlreadyInDb.contains(args[0]) ? new DataNode() : throwDataNodeNotFoundException() }
78 when: 'the batch of new cm handles is checked'
79 def result = objectUnderTest.getIdsOfCmHandlesWithAcceptableAlternateId(batch)
80 then: 'the result only contains the ids of the acceptable cm handles'
81 assert result.contains('ch-1') == acceptCh1
82 assert result.contains('ch-2') == acceptCh2
83 where: 'the following alternate ids are used'
84 scenario | alt1 | alt2 | altAlreadyInDb || acceptCh1 | acceptCh2
85 'no alternate ids' | '' | '' | ['dont matter'] || true | true
86 'new alternate ids' | 'fdn1' | 'fdn2' | ['other fdn'] || true | true
87 'one already used alternate id' | 'fdn1' | 'fdn2' | ['fdn1'] || false | true
88 'two already used alternate ids' | 'fdn1' | 'fdn2' | ['fdn1','fdn2'] || false | false
89 'duplicate alternate id in batch' | 'fdn1' | 'fdn1' | ['dont matter'] || true | false
92 def throwDataNodeNotFoundException() {
93 // cannot 'return' an exception in conditional stub behavior, so hence a method call that will always throw this exception
94 throw dataNodeFoundException