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