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"));
101 assertEquals(automationComposition.getInstanceId(),
102 cacheProvider.getAutomationComposition(automationComposition.getInstanceId()).getInstanceId());
104 assertThat(cacheProvider.getAutomationCompositions()).hasSize(1);
105 cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
106 assertThat(cacheProvider.getAutomationCompositions()).isEmpty();
108 cacheProvider.removeElementDefinition(compositionId);
109 assertThat(cacheProvider.getAcElementsDefinitions()).isEmpty();
114 var automationComposition =
115 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
116 automationComposition.setInstanceId(UUID.randomUUID());
117 var compositionId = UUID.randomUUID();
118 automationComposition.setCompositionId(compositionId);
119 var parameter = CommonTestData.getParticipantParameters();
120 var cacheProvider = new CacheProvider(parameter);
122 var participantDeploy =
123 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
124 cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
127 var ac = cacheProvider.getAutomationComposition(automationComposition.getInstanceId());
128 for (var element : ac.getElements().values()) {
129 element.setOperationalState("OperationalState");
130 element.setUseState("UseState");
131 element.setOutProperties(Map.of("key", "value"));
135 cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
138 // check UseState, OperationalState and OutProperties have not changed
139 ac = cacheProvider.getAutomationComposition(automationComposition.getInstanceId());
140 for (var element : ac.getElements().values()) {
141 assertEquals("OperationalState", element.getOperationalState());
142 assertEquals("UseState", element.getUseState());
143 assertEquals("value", element.getOutProperties().get("key"));
148 void testInitializeAutomationComposition() {
149 var parameter = CommonTestData.getParticipantParameters();
150 var cacheProvider = new CacheProvider(parameter);
152 var participantRestartAc = CommonTestData.createParticipantRestartAc();
153 var compositionId = UUID.randomUUID();
154 cacheProvider.initializeAutomationComposition(compositionId, participantRestartAc);
155 var result = cacheProvider.getAutomationComposition(participantRestartAc.getAutomationCompositionId());
156 assertEquals(compositionId, result.getCompositionId());
157 assertEquals(participantRestartAc.getAutomationCompositionId(), result.getInstanceId());
158 for (var acElementRestart : participantRestartAc.getAcElementList()) {
159 var element = result.getElements().get(acElementRestart.getId());
160 assertEquals(element.getOperationalState(), acElementRestart.getOperationalState());
161 assertEquals(element.getUseState(), acElementRestart.getUseState());
162 assertEquals(element.getLockState(), acElementRestart.getLockState());
163 assertEquals(element.getDeployState(), acElementRestart.getDeployState());
164 assertEquals(element.getProperties(), acElementRestart.getProperties());
165 assertEquals(element.getOutProperties(), acElementRestart.getOutProperties());