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