badc33ec9a42abe41e8c1d9d676724c530bcb73c
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2025 OpenInfra Foundation Europe. All rights reserved.
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.cache;
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 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29 import java.util.Map;
30 import java.util.UUID;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.ElementState;
33 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
34 import org.onap.policy.clamp.models.acm.concepts.DeployState;
35 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
36 import org.onap.policy.clamp.models.acm.concepts.SubState;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
38
39 class CacheProviderTest {
40
41     @Test
42     void testGetSupportedAcElementTypes() {
43         var parameter = CommonTestData.getParticipantParameters();
44         var cacheProvider = new CacheProvider(parameter);
45         assertEquals(parameter.getIntermediaryParameters().getParticipantId(), cacheProvider.getParticipantId());
46         assertEquals(parameter.getIntermediaryParameters().getParticipantSupportedElementTypes().get(0),
47                 cacheProvider.getSupportedAcElementTypes().get(0));
48     }
49
50     @Test
51     void testInitializeAutomationCompositionNotNull() {
52         var parameter = CommonTestData.getParticipantParameters();
53         var cacheProvider = new CacheProvider(parameter);
54         var instanceId = UUID.randomUUID();
55         var participantDeploy = new ParticipantDeploy();
56
57         assertThatThrownBy(() -> cacheProvider
58                 .initializeAutomationComposition(null, instanceId, participantDeploy, null))
59                 .isInstanceOf(NullPointerException.class);
60         assertThatThrownBy(() -> cacheProvider
61                 .initializeAutomationComposition(instanceId, null, participantDeploy, null))
62                 .isInstanceOf(NullPointerException.class);
63         assertThatThrownBy(() -> cacheProvider
64                 .initializeAutomationComposition(instanceId, instanceId, null, null))
65                 .isInstanceOf(NullPointerException.class);
66
67         var deployState = DeployState.DEPLOYED;
68         var subState = SubState.NONE;
69
70         assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(null, instanceId, participantDeploy,
71                 deployState, subState, null)).isInstanceOf(NullPointerException.class);
72         assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(instanceId, null, participantDeploy,
73                 deployState, subState, null)).isInstanceOf(NullPointerException.class);
74     }
75
76     @Test
77     void testNotNull() {
78         var parameter = CommonTestData.getParticipantParameters();
79         var cacheProvider = new CacheProvider(parameter);
80         var instanceId = UUID.randomUUID();
81
82         assertThatThrownBy(() -> cacheProvider.addElementDefinition(null, null, null))
83                 .isInstanceOf(NullPointerException.class);
84         assertThatThrownBy(() -> cacheProvider.addElementDefinition(instanceId, null, null))
85                 .isInstanceOf(NullPointerException.class);
86
87         assertThatThrownBy(() -> cacheProvider.getAutomationComposition(null)).isInstanceOf(NullPointerException.class);
88
89         var definition = new ToscaConceptIdentifier();
90         assertThatThrownBy(() -> cacheProvider.getCommonProperties(null, definition))
91                 .isInstanceOf(NullPointerException.class);
92         assertThatThrownBy(() -> cacheProvider.getCommonProperties(instanceId, (ToscaConceptIdentifier) null))
93                 .isInstanceOf(NullPointerException.class);
94         assertThatThrownBy(() -> cacheProvider.getCommonProperties(instanceId, (UUID) null))
95                 .isInstanceOf(NullPointerException.class);
96         assertThatThrownBy(() -> cacheProvider.getCommonProperties(null, instanceId))
97                 .isInstanceOf(NullPointerException.class);
98
99         assertThatThrownBy(() -> cacheProvider.removeAutomationComposition(null))
100                 .isInstanceOf(NullPointerException.class);
101
102         assertThatThrownBy(() -> cacheProvider.removeElementDefinition(null)).isInstanceOf(NullPointerException.class);
103     }
104
105     @Test
106     void testInitCommonProperties() {
107         var automationComposition =
108                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
109         automationComposition.setInstanceId(UUID.randomUUID());
110         var compositionId = UUID.randomUUID();
111         automationComposition.setCompositionId(compositionId);
112         var definitions =
113                 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition);
114         var cacheProvider = new CacheProvider(CommonTestData.getParticipantParameters());
115         cacheProvider.addElementDefinition(compositionId, definitions, UUID.randomUUID());
116
117         var participantDeploy =
118                 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
119         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
120                 participantDeploy, UUID.randomUUID());
121
122         for (var element : automationComposition.getElements().values()) {
123             var commonProperties =
124                     cacheProvider.getCommonProperties(automationComposition.getInstanceId(), element.getId());
125             assertEquals("value", commonProperties.get("key"));
126
127             commonProperties = cacheProvider
128                     .getCommonProperties(automationComposition.getCompositionId(), element.getDefinition());
129             assertEquals("value", commonProperties.get("key"));
130         }
131
132         assertEquals(automationComposition.getInstanceId(),
133                 cacheProvider.getAutomationComposition(automationComposition.getInstanceId()).getInstanceId());
134
135         assertThat(cacheProvider.getAutomationCompositions()).hasSize(1);
136         cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
137         assertThat(cacheProvider.getAutomationCompositions()).isEmpty();
138
139         cacheProvider.removeElementDefinition(compositionId);
140         assertThat(cacheProvider.getAcElementsDefinitions()).isEmpty();
141     }
142
143     @Test
144     void testDeploy() {
145         var automationComposition =
146                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
147         automationComposition.setInstanceId(UUID.randomUUID());
148         var compositionId = UUID.randomUUID();
149         automationComposition.setCompositionId(compositionId);
150         var parameter = CommonTestData.getParticipantParameters();
151         var cacheProvider = new CacheProvider(parameter);
152
153         var participantDeploy =
154                 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
155         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
156                 participantDeploy, UUID.randomUUID());
157
158         var ac = cacheProvider.getAutomationComposition(automationComposition.getInstanceId());
159         for (var element : ac.getElements().values()) {
160             element.setOperationalState("OperationalState");
161             element.setUseState("UseState");
162             element.setOutProperties(Map.of("key", "value"));
163         }
164
165         // deploy again
166         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
167                 participantDeploy, UUID.randomUUID());
168
169         // check UseState, OperationalState and OutProperties have not changed
170         ac = cacheProvider.getAutomationComposition(automationComposition.getInstanceId());
171         for (var element : ac.getElements().values()) {
172             assertEquals("OperationalState", element.getOperationalState());
173             assertEquals("UseState", element.getUseState());
174             assertEquals("value", element.getOutProperties().get("key"));
175         }
176     }
177
178     @Test
179     void testInitializeAutomationComposition() {
180         var parameter = CommonTestData.getParticipantParameters();
181         var cacheProvider = new CacheProvider(parameter);
182
183         var participantRestartAc = CommonTestData.createParticipantRestartAc();
184         var compositionId = UUID.randomUUID();
185         cacheProvider.initializeAutomationComposition(compositionId, participantRestartAc);
186         var result = cacheProvider.getAutomationComposition(participantRestartAc.getAutomationCompositionId());
187         assertEquals(compositionId, result.getCompositionId());
188         assertEquals(participantRestartAc.getAutomationCompositionId(), result.getInstanceId());
189         for (var acElementRestart : participantRestartAc.getAcElementList()) {
190             var element = result.getElements().get(acElementRestart.getId());
191             assertEquals(element.getOperationalState(), acElementRestart.getOperationalState());
192             assertEquals(element.getUseState(), acElementRestart.getUseState());
193             assertEquals(element.getLockState(), acElementRestart.getLockState());
194             assertEquals(element.getDeployState(), acElementRestart.getDeployState());
195             assertEquals(element.getProperties(), acElementRestart.getProperties());
196             assertEquals(element.getOutProperties(), acElementRestart.getOutProperties());
197         }
198     }
199
200     @Test
201     void testCreateCompositionElementDto() {
202         var parameter = CommonTestData.getParticipantParameters();
203         var cacheProvider = new CacheProvider(parameter);
204         var compositionId = UUID.randomUUID();
205         var automationComposition =
206                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
207         automationComposition.setCompositionId(compositionId);
208         cacheProvider.addElementDefinition(compositionId,
209                 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition),
210                 UUID.randomUUID());
211         for (var element : automationComposition.getElements().values()) {
212             var result = cacheProvider.createCompositionElementDto(compositionId, element);
213             assertEquals(compositionId, result.compositionId());
214             assertEquals(element.getDefinition(), result.elementDefinitionId());
215         }
216     }
217
218     @Test
219     void testGetCompositionElementDtoMap() {
220         var parameter = CommonTestData.getParticipantParameters();
221         var cacheProvider = new CacheProvider(parameter);
222         var compositionId = UUID.randomUUID();
223         var automationComposition =
224                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
225         automationComposition.setCompositionId(compositionId);
226         cacheProvider.addElementDefinition(compositionId,
227                 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition),
228                 UUID.randomUUID());
229         var result = cacheProvider.getCompositionElementDtoMap(automationComposition);
230         for (var element : automationComposition.getElements().values()) {
231             var compositionElementDto = result.get(element.getId());
232             assertEquals(element.getDefinition(), compositionElementDto.elementDefinitionId());
233             assertEquals(ElementState.PRESENT, result.get(element.getId()).state());
234         }
235         var element = automationComposition.getElements().values().iterator().next();
236         element.setDefinition(new ToscaConceptIdentifier("NotExist", "0.0.0"));
237         result = cacheProvider.getCompositionElementDtoMap(automationComposition);
238         assertEquals(ElementState.NOT_PRESENT, result.get(element.getId()).state());
239     }
240
241     @Test
242     void testGetInstanceElementDtoMap() {
243         var parameter = CommonTestData.getParticipantParameters();
244         var cacheProvider = new CacheProvider(parameter);
245         var compositionId = UUID.randomUUID();
246         var automationComposition =
247                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
248         automationComposition.setCompositionId(compositionId);
249         var result = cacheProvider.getInstanceElementDtoMap(automationComposition);
250         for (var element : automationComposition.getElements().values()) {
251             var compositionElementDto = result.get(element.getId());
252             assertEquals(element.getId(), compositionElementDto.elementId());
253         }
254     }
255
256     @Test
257     void testIsCompositionDefinitionUpdated() {
258         var parameter = CommonTestData.getParticipantParameters();
259         var cacheProvider = new CacheProvider(parameter);
260         var compositionId = UUID.randomUUID();
261         assertTrue(cacheProvider.isCompositionDefinitionUpdated(compositionId, null));
262
263         var revisionId = UUID.randomUUID();
264         assertFalse(cacheProvider.isCompositionDefinitionUpdated(compositionId, revisionId));
265
266         var automationComposition =
267                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
268         automationComposition.setCompositionId(compositionId);
269         cacheProvider.addElementDefinition(compositionId,
270                 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition),
271                 revisionId);
272         assertTrue(cacheProvider.isCompositionDefinitionUpdated(compositionId, revisionId));
273
274         revisionId = UUID.randomUUID();
275         assertFalse(cacheProvider.isCompositionDefinitionUpdated(compositionId, revisionId));
276     }
277
278     @Test
279     void testIsInstanceUpdated() {
280         var parameter = CommonTestData.getParticipantParameters();
281         var cacheProvider = new CacheProvider(parameter);
282         var instanceId = UUID.randomUUID();
283         assertTrue(cacheProvider.isInstanceUpdated(instanceId, null));
284         var revisionId = UUID.randomUUID();
285         assertFalse(cacheProvider.isInstanceUpdated(instanceId, revisionId));
286
287         var automationComposition =
288                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
289         automationComposition.setInstanceId(instanceId);
290
291         var participantDeploy =
292                 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
293         cacheProvider.initializeAutomationComposition(UUID.randomUUID(), automationComposition.getInstanceId(),
294                 participantDeploy, revisionId);
295         assertTrue(cacheProvider.isInstanceUpdated(instanceId, revisionId));
296
297         revisionId = UUID.randomUUID();
298         assertFalse(cacheProvider.isInstanceUpdated(instanceId, revisionId));
299     }
300 }