ced2d81e8c95fe7a9e644ff3162e685932677179
[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
69         assertThatThrownBy(() -> cacheProvider.removeAutomationComposition(null))
70                 .isInstanceOf(NullPointerException.class);
71
72         assertThatThrownBy(() -> cacheProvider.removeElementDefinition(null)).isInstanceOf(NullPointerException.class);
73
74         assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(null, null))
75                 .isInstanceOf(NullPointerException.class);
76     }
77
78     @Test
79     void testinitCommonProperties() {
80         var automationComposition =
81                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
82         automationComposition.setInstanceId(UUID.randomUUID());
83         var compositionId = UUID.randomUUID();
84         automationComposition.setCompositionId(compositionId);
85         var definitions =
86                 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition);
87         var cacheProvider = new CacheProvider(CommonTestData.getParticipantParameters());
88         cacheProvider.addElementDefinition(compositionId, definitions);
89
90         var participantDeploy =
91                 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
92         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
93                 participantDeploy);
94
95         for (var element : automationComposition.getElements().values()) {
96             var commonProperties =
97                     cacheProvider.getCommonProperties(automationComposition.getInstanceId(), element.getId());
98             assertEquals("value", commonProperties.get("key"));
99
100             commonProperties = cacheProvider
101                     .getCommonProperties(automationComposition.getCompositionId(), element.getDefinition());
102             assertEquals("value", commonProperties.get("key"));
103         }
104
105         assertEquals(automationComposition.getInstanceId(),
106                 cacheProvider.getAutomationComposition(automationComposition.getInstanceId()).getInstanceId());
107
108         assertThat(cacheProvider.getAutomationCompositions()).hasSize(1);
109         cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
110         assertThat(cacheProvider.getAutomationCompositions()).isEmpty();
111
112         cacheProvider.removeElementDefinition(compositionId);
113         assertThat(cacheProvider.getAcElementsDefinitions()).isEmpty();
114     }
115
116     @Test
117     void testDeply() {
118         var automationComposition =
119                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
120         automationComposition.setInstanceId(UUID.randomUUID());
121         var compositionId = UUID.randomUUID();
122         automationComposition.setCompositionId(compositionId);
123         var parameter = CommonTestData.getParticipantParameters();
124         var cacheProvider = new CacheProvider(parameter);
125
126         var participantDeploy =
127                 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
128         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
129                 participantDeploy);
130
131         var ac = cacheProvider.getAutomationComposition(automationComposition.getInstanceId());
132         for (var element : ac.getElements().values()) {
133             element.setOperationalState("OperationalState");
134             element.setUseState("UseState");
135             element.setOutProperties(Map.of("key", "value"));
136         }
137
138         // deploy again
139         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
140                 participantDeploy);
141
142         // check UseState, OperationalState and OutProperties have not changed
143         ac = cacheProvider.getAutomationComposition(automationComposition.getInstanceId());
144         for (var element : ac.getElements().values()) {
145             assertEquals("OperationalState", element.getOperationalState());
146             assertEquals("UseState", element.getUseState());
147             assertEquals("value", element.getOutProperties().get("key"));
148         }
149     }
150
151     @Test
152     void testInitializeAutomationComposition() {
153         var parameter = CommonTestData.getParticipantParameters();
154         var cacheProvider = new CacheProvider(parameter);
155
156         var participantRestartAc = CommonTestData.createParticipantRestartAc();
157         var compositionId = UUID.randomUUID();
158         cacheProvider.initializeAutomationComposition(compositionId, participantRestartAc);
159         var result = cacheProvider.getAutomationComposition(participantRestartAc.getAutomationCompositionId());
160         assertEquals(compositionId, result.getCompositionId());
161         assertEquals(participantRestartAc.getAutomationCompositionId(), result.getInstanceId());
162         for (var acElementRestart : participantRestartAc.getAcElementList()) {
163             var element = result.getElements().get(acElementRestart.getId());
164             assertEquals(element.getOperationalState(), acElementRestart.getOperationalState());
165             assertEquals(element.getUseState(), acElementRestart.getUseState());
166             assertEquals(element.getLockState(), acElementRestart.getLockState());
167             assertEquals(element.getDeployState(), acElementRestart.getDeployState());
168             assertEquals(element.getProperties(), acElementRestart.getProperties());
169             assertEquals(element.getOutProperties(), acElementRestart.getOutProperties());
170         }
171     }
172
173     @Test
174     void testCreateCompositionElementDto() {
175         var parameter = CommonTestData.getParticipantParameters();
176         var cacheProvider = new CacheProvider(parameter);
177         var compositionId = UUID.randomUUID();
178         var automationComposition =
179                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
180         automationComposition.setCompositionId(compositionId);
181         cacheProvider.addElementDefinition(compositionId,
182                 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition));
183         for (var element : automationComposition.getElements().values()) {
184             var result = cacheProvider.createCompositionElementDto(compositionId, element, Map.of());
185             assertEquals(compositionId, result.compositionId());
186             assertEquals(element.getDefinition(), result.elementDefinitionId());
187         }
188     }
189
190     @Test
191     void testGetCompositionElementDtoMap() {
192         var parameter = CommonTestData.getParticipantParameters();
193         var cacheProvider = new CacheProvider(parameter);
194         var compositionId = UUID.randomUUID();
195         var automationComposition =
196                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
197         automationComposition.setCompositionId(compositionId);
198         cacheProvider.addElementDefinition(compositionId,
199                 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition));
200         var result = cacheProvider.getCompositionElementDtoMap(automationComposition);
201         for (var element : automationComposition.getElements().values()) {
202             var compositionElementDto = result.get(element.getId());
203             assertEquals(element.getDefinition(), compositionElementDto.elementDefinitionId());
204         }
205     }
206
207     @Test
208     void testGetInstanceElementDtoMap() {
209         var parameter = CommonTestData.getParticipantParameters();
210         var cacheProvider = new CacheProvider(parameter);
211         var compositionId = UUID.randomUUID();
212         var automationComposition =
213                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
214         automationComposition.setCompositionId(compositionId);
215         var result = cacheProvider.getInstanceElementDtoMap(automationComposition);
216         for (var element : automationComposition.getElements().values()) {
217             var compositionElementDto = result.get(element.getId());
218             assertEquals(element.getId(), compositionElementDto.elementId());
219         }
220     }
221 }