96add2025fef736b6acbac5c4df041e0dce60b9b
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-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.policy.clamp.acm.participant.intermediary.handler;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26
27 import java.util.Map;
28 import java.util.UUID;
29 import org.junit.jupiter.api.Test;
30 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
32
33 class CacheProviderTest {
34
35     @Test
36     void testgetSupportedAcElementTypes() {
37         var parameter = CommonTestData.getParticipantParameters();
38         var cacheProvider = new CacheProvider(parameter);
39         assertEquals(parameter.getIntermediaryParameters().getParticipantId(), cacheProvider.getParticipantId());
40         assertEquals(parameter.getIntermediaryParameters().getParticipantSupportedElementTypes().get(0),
41                 cacheProvider.getSupportedAcElementTypes().get(0));
42     }
43
44     @Test
45     void testNotNull() {
46         var parameter = CommonTestData.getParticipantParameters();
47         var cacheProvider = new CacheProvider(parameter);
48         var instanceId = UUID.randomUUID();
49         assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(null, null, null))
50                 .isInstanceOf(NullPointerException.class);
51         assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(instanceId, null, null))
52                 .isInstanceOf(NullPointerException.class);
53         assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(instanceId, instanceId, null))
54                 .isInstanceOf(NullPointerException.class);
55
56         assertThatThrownBy(() -> cacheProvider.addElementDefinition(null, null))
57                 .isInstanceOf(NullPointerException.class);
58         assertThatThrownBy(() -> cacheProvider.addElementDefinition(instanceId, null))
59                 .isInstanceOf(NullPointerException.class);
60
61         assertThatThrownBy(() -> cacheProvider.getAutomationComposition(null)).isInstanceOf(NullPointerException.class);
62
63         var definition = new ToscaConceptIdentifier();
64         assertThatThrownBy(() -> cacheProvider.getCommonProperties(null, definition))
65                 .isInstanceOf(NullPointerException.class);
66         assertThatThrownBy(() -> cacheProvider.getCommonProperties(instanceId, (UUID) null))
67                 .isInstanceOf(NullPointerException.class);
68         assertThatThrownBy(() -> cacheProvider.getCommonProperties(null, instanceId))
69                 .isInstanceOf(NullPointerException.class);
70
71         assertThatThrownBy(() -> cacheProvider.removeAutomationComposition(null))
72                 .isInstanceOf(NullPointerException.class);
73
74         assertThatThrownBy(() -> cacheProvider.removeElementDefinition(null)).isInstanceOf(NullPointerException.class);
75
76         assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(null, null))
77                 .isInstanceOf(NullPointerException.class);
78     }
79
80     @Test
81     void testinitCommonProperties() {
82         var automationComposition =
83                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
84         automationComposition.setInstanceId(UUID.randomUUID());
85         var compositionId = UUID.randomUUID();
86         automationComposition.setCompositionId(compositionId);
87         var definitions =
88                 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition);
89         var cacheProvider = new CacheProvider(CommonTestData.getParticipantParameters());
90         cacheProvider.addElementDefinition(compositionId, definitions);
91
92         var participantDeploy =
93                 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
94         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
95                 participantDeploy);
96
97         for (var element : automationComposition.getElements().values()) {
98             var commonProperties =
99                     cacheProvider.getCommonProperties(automationComposition.getInstanceId(), element.getId());
100             assertEquals("value", commonProperties.get("key"));
101
102             commonProperties = cacheProvider
103                     .getCommonProperties(automationComposition.getCompositionId(), element.getDefinition());
104             assertEquals("value", commonProperties.get("key"));
105         }
106
107         assertEquals(automationComposition.getInstanceId(),
108                 cacheProvider.getAutomationComposition(automationComposition.getInstanceId()).getInstanceId());
109
110         assertThat(cacheProvider.getAutomationCompositions()).hasSize(1);
111         cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
112         assertThat(cacheProvider.getAutomationCompositions()).isEmpty();
113
114         cacheProvider.removeElementDefinition(compositionId);
115         assertThat(cacheProvider.getAcElementsDefinitions()).isEmpty();
116     }
117
118     @Test
119     void testDeply() {
120         var automationComposition =
121                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
122         automationComposition.setInstanceId(UUID.randomUUID());
123         var compositionId = UUID.randomUUID();
124         automationComposition.setCompositionId(compositionId);
125         var parameter = CommonTestData.getParticipantParameters();
126         var cacheProvider = new CacheProvider(parameter);
127
128         var participantDeploy =
129                 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
130         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
131                 participantDeploy);
132
133         var ac = cacheProvider.getAutomationComposition(automationComposition.getInstanceId());
134         for (var element : ac.getElements().values()) {
135             element.setOperationalState("OperationalState");
136             element.setUseState("UseState");
137             element.setOutProperties(Map.of("key", "value"));
138         }
139
140         // deploy again
141         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
142                 participantDeploy);
143
144         // check UseState, OperationalState and OutProperties have not changed
145         ac = cacheProvider.getAutomationComposition(automationComposition.getInstanceId());
146         for (var element : ac.getElements().values()) {
147             assertEquals("OperationalState", element.getOperationalState());
148             assertEquals("UseState", element.getUseState());
149             assertEquals("value", element.getOutProperties().get("key"));
150         }
151     }
152
153     @Test
154     void testInitializeAutomationComposition() {
155         var parameter = CommonTestData.getParticipantParameters();
156         var cacheProvider = new CacheProvider(parameter);
157
158         var participantRestartAc = CommonTestData.createParticipantRestartAc();
159         var compositionId = UUID.randomUUID();
160         cacheProvider.initializeAutomationComposition(compositionId, participantRestartAc);
161         var result = cacheProvider.getAutomationComposition(participantRestartAc.getAutomationCompositionId());
162         assertEquals(compositionId, result.getCompositionId());
163         assertEquals(participantRestartAc.getAutomationCompositionId(), result.getInstanceId());
164         for (var acElementRestart : participantRestartAc.getAcElementList()) {
165             var element = result.getElements().get(acElementRestart.getId());
166             assertEquals(element.getOperationalState(), acElementRestart.getOperationalState());
167             assertEquals(element.getUseState(), acElementRestart.getUseState());
168             assertEquals(element.getLockState(), acElementRestart.getLockState());
169             assertEquals(element.getDeployState(), acElementRestart.getDeployState());
170             assertEquals(element.getProperties(), acElementRestart.getProperties());
171             assertEquals(element.getOutProperties(), acElementRestart.getOutProperties());
172         }
173     }
174
175     @Test
176     void testCreateCompositionElementDto() {
177         var parameter = CommonTestData.getParticipantParameters();
178         var cacheProvider = new CacheProvider(parameter);
179         var compositionId = UUID.randomUUID();
180         var automationComposition =
181                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
182         automationComposition.setCompositionId(compositionId);
183         cacheProvider.addElementDefinition(compositionId,
184                 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition));
185         for (var element : automationComposition.getElements().values()) {
186             var result = cacheProvider.createCompositionElementDto(compositionId, element, Map.of());
187             assertEquals(compositionId, result.compositionId());
188             assertEquals(element.getDefinition(), result.elementDefinitionId());
189         }
190     }
191
192     @Test
193     void testGetCompositionElementDtoMap() {
194         var parameter = CommonTestData.getParticipantParameters();
195         var cacheProvider = new CacheProvider(parameter);
196         var compositionId = UUID.randomUUID();
197         var automationComposition =
198                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
199         automationComposition.setCompositionId(compositionId);
200         cacheProvider.addElementDefinition(compositionId,
201                 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition));
202         var result = cacheProvider.getCompositionElementDtoMap(automationComposition);
203         for (var element : automationComposition.getElements().values()) {
204             var compositionElementDto = result.get(element.getId());
205             assertEquals(element.getDefinition(), compositionElementDto.elementDefinitionId());
206         }
207     }
208
209     @Test
210     void testGetInstanceElementDtoMap() {
211         var parameter = CommonTestData.getParticipantParameters();
212         var cacheProvider = new CacheProvider(parameter);
213         var compositionId = UUID.randomUUID();
214         var automationComposition =
215                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
216         automationComposition.setCompositionId(compositionId);
217         var result = cacheProvider.getInstanceElementDtoMap(automationComposition);
218         for (var element : automationComposition.getElements().values()) {
219             var compositionElementDto = result.get(element.getId());
220             assertEquals(element.getId(), compositionElementDto.elementId());
221         }
222     }
223 }