Merge "Revert "Introduce Hazelcast for alternateId-cmHandle relation pt. 2 - error...
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / utils / CmHandleIdMapperSpec.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 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
31
32 class CmHandleIdMapperSpec extends Specification {
33
34     def alternateIdPerCmHandle = new HashMap<String, String>()
35     def cmHandlePerAlternateId = new HashMap<String, String>()
36     def mockCpsCmHandlerQueryService = Mock(NetworkCmProxyCmHandleQueryService)
37
38     def objectUnderTest = new CmHandleIdMapper(alternateIdPerCmHandle, cmHandlePerAlternateId, mockCpsCmHandlerQueryService)
39
40     def logger = Spy(ListAppender<ILoggingEvent>)
41
42     def setup() {
43         ((Logger) LoggerFactory.getLogger(CmHandleIdMapper.class)).addAppender(logger)
44         logger.start()
45         mockCpsCmHandlerQueryService.getAllCmHandles() >> []
46         assert objectUnderTest.addMapping('my cmhandle id', 'my alternate id')
47     }
48
49     void cleanup() {
50         ((Logger) LoggerFactory.getLogger(CmHandleIdMapper.class)).detachAndStopAllAppenders()
51     }
52
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'
58     }
59
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
67             'empty'  | ''
68             'blank'  | '  '
69             'null'   | null
70     }
71
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
79     }
80
81     def 'Attempt to remove a non-existing entry from the cache.'() {
82         when: 'removing an entry that is not cached'
83             objectUnderTest.removeMapping('non-cached cmhandle id')
84         then: 'deleting from the cmhandle cache returns null'
85             assert alternateIdPerCmHandle.remove('non-cached cmhandle id') == null
86         and: 'removal from the alternate id cache is skipped'
87             0 * cmHandlePerAlternateId.remove(_)
88     }
89
90     def 'Cannot update existing alternate id.'() {
91         given: 'attempt to update an existing alternate id'
92             objectUnderTest.addMapping('my cmhandle id', 'other id')
93         expect: 'still returns the original alternate id'
94             assert objectUnderTest.cmHandleIdToAlternateId('my cmhandle id') == 'my alternate id'
95         and: 'converting other alternate id returns null'
96             assert objectUnderTest.alternateIdToCmHandleId('other id') == null
97         and: 'a warning is logged with the original alternate id'
98             def lastLoggingEvent = logger.list[1]
99             assert lastLoggingEvent.level == Level.WARN
100             assert lastLoggingEvent.formattedMessage.contains('my alternate id')
101     }
102
103     def 'Update existing alternate id with the same value.'() {
104         expect: 'update an existing alternate id with the same value returns false (no update)'
105             assert objectUnderTest.addMapping('my cmhandle id', 'my alternate id') == false
106         and: 'conversion still returns the original alternate id'
107             assert objectUnderTest.cmHandleIdToAlternateId('my cmhandle id') == 'my alternate id'
108     }
109
110     def 'Initializing cache #scenario.'() {
111         when: 'the cache is (re-)initialized'
112             objectUnderTest.cacheIsInitialized = false
113             objectUnderTest.initializeCache()
114         then: 'the alternate id can be converted to cmhandle id'
115             assert objectUnderTest.alternateIdToCmHandleId('alt-1') == convertedCmHandleId
116         and: 'the cm handle id can be converted to alternate id'
117             assert objectUnderTest.cmHandleIdToAlternateId('ch-1') == convertedAlternatId
118         and: 'the query service is called to get the initial data'
119             1 * mockCpsCmHandlerQueryService.getAllCmHandles() >> persistedCmHandles
120         where: 'the initial data has a cm handle #scenario'
121             scenario                  | persistedCmHandles                                                  || convertedAlternatId | convertedCmHandleId
122             'with alternate id'       | [new NcmpServiceCmHandle(cmHandleId: 'ch-1', alternateId: 'alt-1')] || 'alt-1'             | 'ch-1'
123             'without alternate id'    | [new NcmpServiceCmHandle(cmHandleId: 'ch-1')]                       || null                | null
124             'with blank alternate id' | [new NcmpServiceCmHandle(cmHandleId: 'ch-1', alternateId: ' ')]     || null                | null
125     }
126 }