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