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
23 import ch.qos.logback.classic.Level
24 import ch.qos.logback.classic.Logger
25 import ch.qos.logback.classic.spi.ILoggingEvent
26 import ch.qos.logback.core.read.ListAppender
27 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
28 import org.slf4j.LoggerFactory
29 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandleQueryService
30 import spock.lang.Specification
32 class CmHandleIdMapperSpec extends Specification {
34 def alternateIdPerCmHandle = new HashMap<String, String>()
35 def cmHandlePerAlternateId = new HashMap<String, String>()
36 def mockCpsCmHandlerQueryService = Mock(NetworkCmProxyCmHandleQueryService)
38 def objectUnderTest = new CmHandleIdMapper(alternateIdPerCmHandle, cmHandlePerAlternateId, mockCpsCmHandlerQueryService)
40 def logger = Spy(ListAppender<ILoggingEvent>)
43 ((Logger) LoggerFactory.getLogger(CmHandleIdMapper.class)).addAppender(logger)
45 mockCpsCmHandlerQueryService.getAllCmHandles() >> []
46 assert objectUnderTest.addMapping('my cmhandle id', 'my alternate id')
50 ((Logger) LoggerFactory.getLogger(CmHandleIdMapper.class)).detachAndStopAllAppenders()
53 def 'Checking entries in the cache.'() {
54 expect: 'the alternate id can be converted to cmhandle id'
55 assert objectUnderTest.alternateIdToCmHandleId('my alternate id') == 'my cmhandle id'
56 and: 'the cmhandle id can be converted to alternate id'
57 assert objectUnderTest.cmHandleIdToAlternateId('my cmhandle id') == 'my alternate id'
60 def 'Attempt adding #scenario alternate id.'() {
61 expect: 'cmhandle id - alternate id mapping fails'
62 assert objectUnderTest.addMapping('ch-1', alternateId) == false
63 and: 'alternate id looked up by cmhandle id unsuccessfully'
64 assert objectUnderTest.cmHandleIdToAlternateId('ch-1') == null
65 where: 'alternate id has an invalid value'
66 scenario | alternateId
72 def 'Remove an entry from the cache.'() {
73 when: 'removing an entry'
74 objectUnderTest.removeMapping('my cmhandle id')
75 then: 'converting alternate id returns null'
76 assert objectUnderTest.alternateIdToCmHandleId('my alternate id') == null
77 and: 'converting cmhandle id returns null'
78 assert objectUnderTest.cmHandleIdToAlternateId('my cmhandle id') == null
81 def 'Cannot update existing alternate id.'() {
82 given: 'attempt to update an existing alternate id'
83 objectUnderTest.addMapping('my cmhandle id', 'other id')
84 expect: 'still returns the original alternate id'
85 assert objectUnderTest.cmHandleIdToAlternateId('my cmhandle id') == 'my alternate id'
86 and: 'converting other alternate id returns null'
87 assert objectUnderTest.alternateIdToCmHandleId('other id') == null
88 and: 'a warning is logged with the original alternate id'
89 def lastLoggingEvent = logger.list[1]
90 assert lastLoggingEvent.level == Level.WARN
91 assert lastLoggingEvent.formattedMessage.contains('my alternate id')
94 def 'Update existing alternate id with the same value.'() {
95 expect: 'update an existing alternate id with the same value returns false (no update)'
96 assert objectUnderTest.addMapping('my cmhandle id', 'my alternate id') == false
97 and: 'conversion still returns the original alternate id'
98 assert objectUnderTest.cmHandleIdToAlternateId('my cmhandle id') == 'my alternate id'
101 def 'Initializing cache #scenario.'() {
102 when: 'the cache is (re-)initialized'
103 objectUnderTest.cacheIsInitialized = false
104 objectUnderTest.initializeCache()
105 then: 'the alternate id can be converted to cmhandle id'
106 assert objectUnderTest.alternateIdToCmHandleId('alt-1') == convertedCmHandleId
107 and: 'the cm handle id can be converted to alternate id'
108 assert objectUnderTest.cmHandleIdToAlternateId('ch-1') == convertedAlternatId
109 and: 'the query service is called to get the initial data'
110 1 * mockCpsCmHandlerQueryService.getAllCmHandles() >> persistedCmHandles
111 where: 'the initial data has a cm handle #scenario'
112 scenario | persistedCmHandles || convertedAlternatId | convertedCmHandleId
113 'with alternate id' | [new NcmpServiceCmHandle(cmHandleId: 'ch-1', alternateId: 'alt-1')] || 'alt-1' | 'ch-1'
114 'without alternate id' | [new NcmpServiceCmHandle(cmHandleId: 'ch-1')] || null | null
115 'with blank alternate id' | [new NcmpServiceCmHandle(cmHandleId: 'ch-1', alternateId: ' ')] || null | null