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;
32 class CacheProviderTest {
35 void testgetSupportedAcElementTypes() {
36 var parameter = CommonTestData.getParticipantParameters();
37 var cacheProvider = new CacheProvider(parameter);
38 assertEquals(parameter.getIntermediaryParameters().getParticipantId(), cacheProvider.getParticipantId());
39 assertEquals(parameter.getIntermediaryParameters().getParticipantSupportedElementTypes().get(0),
40 cacheProvider.getSupportedAcElementTypes().get(0));
45 var parameter = CommonTestData.getParticipantParameters();
46 var cacheProvider = new CacheProvider(parameter);
47 var instanceId = UUID.randomUUID();
48 assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(null, null, null))
49 .isInstanceOf(NullPointerException.class);
50 assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(instanceId, null, null))
51 .isInstanceOf(NullPointerException.class);
52 assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(instanceId, instanceId, null))
53 .isInstanceOf(NullPointerException.class);
55 assertThatThrownBy(() -> cacheProvider.addElementDefinition(null, null))
56 .isInstanceOf(NullPointerException.class);
57 assertThatThrownBy(() -> cacheProvider.addElementDefinition(instanceId, null))
58 .isInstanceOf(NullPointerException.class);
60 assertThatThrownBy(() -> cacheProvider.getAutomationComposition(null)).isInstanceOf(NullPointerException.class);
62 assertThatThrownBy(() -> cacheProvider.getCommonProperties(null, null))
63 .isInstanceOf(NullPointerException.class);
64 assertThatThrownBy(() -> cacheProvider.getCommonProperties(instanceId, null))
65 .isInstanceOf(NullPointerException.class);
67 assertThatThrownBy(() -> cacheProvider.removeAutomationComposition(null))
68 .isInstanceOf(NullPointerException.class);
70 assertThatThrownBy(() -> cacheProvider.removeElementDefinition(null)).isInstanceOf(NullPointerException.class);
72 assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(null, null))
73 .isInstanceOf(NullPointerException.class);
77 void testinitCommonProperties() {
78 var automationComposition =
79 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
80 automationComposition.setInstanceId(UUID.randomUUID());
81 var compositionId = UUID.randomUUID();
82 automationComposition.setCompositionId(compositionId);
84 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition);
85 var cacheProvider = new CacheProvider(CommonTestData.getParticipantParameters());
86 cacheProvider.addElementDefinition(compositionId, definitions);
88 var participantDeploy =
89 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
90 cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
93 for (var element : automationComposition.getElements().values()) {
94 var commonProperties =
95 cacheProvider.getCommonProperties(automationComposition.getInstanceId(), element.getId());
96 assertEquals("value", commonProperties.get("key"));
99 assertEquals(automationComposition.getInstanceId(),
100 cacheProvider.getAutomationComposition(automationComposition.getInstanceId()).getInstanceId());
102 assertThat(cacheProvider.getAutomationCompositions()).hasSize(1);
103 cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
104 assertThat(cacheProvider.getAutomationCompositions()).isEmpty();
106 cacheProvider.removeElementDefinition(compositionId);
107 assertThat(cacheProvider.getAcElementsDefinitions()).isEmpty();
112 var automationComposition =
113 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
114 automationComposition.setInstanceId(UUID.randomUUID());
115 var compositionId = UUID.randomUUID();
116 automationComposition.setCompositionId(compositionId);
117 var parameter = CommonTestData.getParticipantParameters();
118 var cacheProvider = new CacheProvider(parameter);
120 var participantDeploy =
121 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
122 cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
125 var ac = cacheProvider.getAutomationComposition(automationComposition.getInstanceId());
126 for (var element : ac.getElements().values()) {
127 element.setOperationalState("OperationalState");
128 element.setUseState("UseState");
129 element.setOutProperties(Map.of("key", "value"));
133 cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
136 // check UseState, OperationalState and OutProperties have not changed
137 ac = cacheProvider.getAutomationComposition(automationComposition.getInstanceId());
138 for (var element : ac.getElements().values()) {
139 assertEquals("OperationalState", element.getOperationalState());
140 assertEquals("UseState", element.getUseState());
141 assertEquals("value", element.getOutProperties().get("key"));
146 void testInitializeAutomationComposition() {
147 var parameter = CommonTestData.getParticipantParameters();
148 var cacheProvider = new CacheProvider(parameter);
150 var participantRestartAc = CommonTestData.createParticipantRestartAc();
151 var compositionId = UUID.randomUUID();
152 cacheProvider.initializeAutomationComposition(compositionId, participantRestartAc);
153 var result = cacheProvider.getAutomationComposition(participantRestartAc.getAutomationCompositionId());
154 assertEquals(compositionId, result.getCompositionId());
155 assertEquals(participantRestartAc.getAutomationCompositionId(), result.getInstanceId());
156 for (var acElementRestart : participantRestartAc.getAcElementList()) {
157 var element = result.getElements().get(acElementRestart.getId());
158 assertEquals(element.getOperationalState(), acElementRestart.getOperationalState());
159 assertEquals(element.getUseState(), acElementRestart.getUseState());
160 assertEquals(element.getLockState(), acElementRestart.getLockState());
161 assertEquals(element.getDeployState(), acElementRestart.getDeployState());
162 assertEquals(element.getProperties(), acElementRestart.getProperties());
163 assertEquals(element.getOutProperties(), acElementRestart.getOutProperties());