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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.intermediary.handler;
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;
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;
33 class CacheProviderTest {
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));
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);
56 assertThatThrownBy(() -> cacheProvider.addElementDefinition(null, null))
57 .isInstanceOf(NullPointerException.class);
58 assertThatThrownBy(() -> cacheProvider.addElementDefinition(instanceId, null))
59 .isInstanceOf(NullPointerException.class);
61 assertThatThrownBy(() -> cacheProvider.getAutomationComposition(null)).isInstanceOf(NullPointerException.class);
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);
69 assertThatThrownBy(() -> cacheProvider.removeAutomationComposition(null))
70 .isInstanceOf(NullPointerException.class);
72 assertThatThrownBy(() -> cacheProvider.removeElementDefinition(null)).isInstanceOf(NullPointerException.class);
74 assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(null, null))
75 .isInstanceOf(NullPointerException.class);
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);
86 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition);
87 var cacheProvider = new CacheProvider(CommonTestData.getParticipantParameters());
88 cacheProvider.addElementDefinition(compositionId, definitions);
90 var participantDeploy =
91 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
92 cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
95 for (var element : automationComposition.getElements().values()) {
96 var commonProperties =
97 cacheProvider.getCommonProperties(automationComposition.getInstanceId(), element.getId());
98 assertEquals("value", commonProperties.get("key"));
100 commonProperties = cacheProvider
101 .getCommonProperties(automationComposition.getCompositionId(), element.getDefinition());
102 assertEquals("value", commonProperties.get("key"));
105 assertEquals(automationComposition.getInstanceId(),
106 cacheProvider.getAutomationComposition(automationComposition.getInstanceId()).getInstanceId());
108 assertThat(cacheProvider.getAutomationCompositions()).hasSize(1);
109 cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
110 assertThat(cacheProvider.getAutomationCompositions()).isEmpty();
112 cacheProvider.removeElementDefinition(compositionId);
113 assertThat(cacheProvider.getAcElementsDefinitions()).isEmpty();
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);
126 var participantDeploy =
127 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
128 cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
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"));
139 cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
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"));
152 void testInitializeAutomationComposition() {
153 var parameter = CommonTestData.getParticipantParameters();
154 var cacheProvider = new CacheProvider(parameter);
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());
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());