e55de0192eb13fea24611a665650bfa591fb19f2
[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
32 class CacheProviderTest {
33
34     @Test
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));
41     }
42
43     @Test
44     void testNotNull() {
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);
54
55         assertThatThrownBy(() -> cacheProvider.addElementDefinition(null, null))
56                 .isInstanceOf(NullPointerException.class);
57         assertThatThrownBy(() -> cacheProvider.addElementDefinition(instanceId, null))
58                 .isInstanceOf(NullPointerException.class);
59
60         assertThatThrownBy(() -> cacheProvider.getAutomationComposition(null)).isInstanceOf(NullPointerException.class);
61
62         assertThatThrownBy(() -> cacheProvider.getCommonProperties(null, null))
63                 .isInstanceOf(NullPointerException.class);
64         assertThatThrownBy(() -> cacheProvider.getCommonProperties(instanceId, null))
65                 .isInstanceOf(NullPointerException.class);
66
67         assertThatThrownBy(() -> cacheProvider.removeAutomationComposition(null))
68                 .isInstanceOf(NullPointerException.class);
69
70         assertThatThrownBy(() -> cacheProvider.removeElementDefinition(null)).isInstanceOf(NullPointerException.class);
71
72         assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(null, null))
73                 .isInstanceOf(NullPointerException.class);
74     }
75
76     @Test
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);
83         var definitions =
84                 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition);
85         var cacheProvider = new CacheProvider(CommonTestData.getParticipantParameters());
86         cacheProvider.addElementDefinition(compositionId, definitions);
87
88         var participantDeploy =
89                 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
90         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
91                 participantDeploy);
92
93         for (var element : automationComposition.getElements().values()) {
94             var commonProperties =
95                     cacheProvider.getCommonProperties(automationComposition.getInstanceId(), element.getId());
96             assertEquals("value", commonProperties.get("key"));
97         }
98
99         assertEquals(automationComposition.getInstanceId(),
100                 cacheProvider.getAutomationComposition(automationComposition.getInstanceId()).getInstanceId());
101
102         assertThat(cacheProvider.getAutomationCompositions()).hasSize(1);
103         cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
104         assertThat(cacheProvider.getAutomationCompositions()).isEmpty();
105
106         cacheProvider.removeElementDefinition(compositionId);
107         assertThat(cacheProvider.getAcElementsDefinitions()).isEmpty();
108     }
109
110     @Test
111     void testDeply() {
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);
119
120         var participantDeploy =
121                 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
122         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
123                 participantDeploy);
124
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"));
130         }
131
132         // deploy again
133         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
134                 participantDeploy);
135
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"));
142         }
143     }
144
145     @Test
146     void testInitializeAutomationComposition() {
147         var parameter = CommonTestData.getParticipantParameters();
148         var cacheProvider = new CacheProvider(parameter);
149
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     }
157 }