Fix the algorithm for device trust level
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / trustlevel / TrustLevelManagerSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.trustlevel
22
23 import org.onap.cps.ncmp.api.impl.events.avc.ncmptoclient.AvcEventPublisher
24 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence
25 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
26 import spock.lang.Specification
27
28 class TrustLevelManagerSpec extends Specification {
29
30     def trustLevelPerCmHandle = [:]
31     def trustLevelPerDmiPlugin = [:]
32
33     def mockInventoryPersistence = Mock(InventoryPersistence)
34     def mockAttributeValueChangeEventPublisher = Mock(AvcEventPublisher)
35     def objectUnderTest = new TrustLevelManager(trustLevelPerCmHandle, trustLevelPerDmiPlugin, mockInventoryPersistence, mockAttributeValueChangeEventPublisher)
36
37     def 'Initial cm handle registration'() {
38         given: 'two cm handles: one with no trust level and one trusted'
39             def cmHandleModelsToBeCreated = ['ch-1': null, 'ch-2': TrustLevel.COMPLETE]
40         when: 'the initial registration handled'
41             objectUnderTest.handleInitialRegistrationOfTrustLevels(cmHandleModelsToBeCreated)
42         then: 'no notification sent'
43             0 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_)
44         and: 'both cm handles are in the cache and are trusted'
45             assert trustLevelPerCmHandle.get('ch-1') == TrustLevel.COMPLETE
46             assert trustLevelPerCmHandle.get('ch-2') == TrustLevel.COMPLETE
47     }
48
49     def 'Initial cm handle registration with a cm handle that is not trusted'() {
50         given: 'a not trusted cm handle'
51             def cmHandleModelsToBeCreated = ['ch-2': TrustLevel.NONE]
52         when: 'the initial registration handled'
53             objectUnderTest.handleInitialRegistrationOfTrustLevels(cmHandleModelsToBeCreated)
54         then: 'notification is sent'
55             1 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_)
56     }
57
58     def 'Dmi trust level updated'() {
59         given: 'a trusted dmi plugin'
60             trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.COMPLETE)
61         and: 'a trusted cm handle'
62             trustLevelPerCmHandle.put('ch-1', TrustLevel.COMPLETE)
63         when: 'the update is handled'
64             objectUnderTest.handleUpdateOfDmiTrustLevel('my-dmi', ['ch-1'], TrustLevel.NONE)
65         then: 'notification is sent'
66             1 * mockAttributeValueChangeEventPublisher.publishAvcEvent('ch-1', 'trustLevel', 'COMPLETE', 'NONE')
67         and: 'the dmi in the cache is not trusted'
68             assert trustLevelPerDmiPlugin.get('my-dmi') == TrustLevel.NONE
69     }
70
71     def 'Dmi trust level updated with same value'() {
72         given: 'a trusted dmi plugin'
73             trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.COMPLETE)
74         and: 'a trusted cm handle'
75             trustLevelPerCmHandle.put('ch-1', TrustLevel.COMPLETE)
76         when: 'the update is handled'
77             objectUnderTest.handleUpdateOfDmiTrustLevel('my-dmi', ['ch-1'], TrustLevel.COMPLETE)
78         then: 'no notification is sent'
79             0 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_)
80         and: 'the dmi in the cache is trusted'
81             assert trustLevelPerDmiPlugin.get('my-dmi') == TrustLevel.COMPLETE
82     }
83
84     def 'Device trust level updated'() {
85         given: 'a non trusted cm handle'
86             trustLevelPerCmHandle.put('ch-1', TrustLevel.NONE)
87         and: 'a trusted dmi plugin'
88             trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.COMPLETE)
89         and: 'inventory persistence service returns yang model cm handle'
90             mockInventoryPersistence.getYangModelCmHandle('ch-1') >> new YangModelCmHandle(id: 'ch-1', dmiDataServiceName: 'my-dmi')
91         when: 'update of device to COMPLETE trust level handled'
92             objectUnderTest.handleUpdateOfDeviceTrustLevel('ch-1', TrustLevel.COMPLETE)
93         then: 'the cm handle in the cache is trusted'
94             assert trustLevelPerCmHandle.get('ch-1', TrustLevel.COMPLETE)
95         and: 'notification is sent'
96             1 * mockAttributeValueChangeEventPublisher.publishAvcEvent('ch-1', 'trustLevel', 'NONE', 'COMPLETE')
97     }
98
99     def 'Device trust level updated with same value'() {
100         given: 'a non trusted cm handle'
101             trustLevelPerCmHandle.put('ch-1', TrustLevel.NONE)
102         and: 'a trusted dmi plugin'
103             trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.COMPLETE)
104         and: 'inventory persistence service returns yang model cm handle'
105             mockInventoryPersistence.getYangModelCmHandle('ch-1') >> new YangModelCmHandle(id: 'ch-1', dmiDataServiceName: 'my-dmi')
106         when: 'update of device trust to the same level (NONE)'
107             objectUnderTest.handleUpdateOfDeviceTrustLevel('ch-1', TrustLevel.NONE)
108         then: 'the cm handle in the cache is not trusted'
109             assert trustLevelPerCmHandle.get('ch-1', TrustLevel.NONE)
110         and: 'no notification is sent'
111             0 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_)
112     }
113
114     def 'Dmi trust level restored to complete with non trusted device'() {
115         given: 'a non trusted dmi'
116             trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.NONE)
117         and: 'a non trusted device'
118             trustLevelPerCmHandle.put('ch-1', TrustLevel.NONE)
119         when: 'restore the dmi trust level to COMPLETE'
120             objectUnderTest.handleUpdateOfDmiTrustLevel('my-dmi', ['ch-1'], TrustLevel.COMPLETE)
121         then: 'the cm handle in the cache is still NONE'
122             assert trustLevelPerCmHandle.get('ch-1') == TrustLevel.NONE
123         and: 'no notification is sent'
124             0 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_)
125     }
126
127 }