d5fe5785b42a8df08e0b68d608cda4aadf4667d0
[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
101         assertEquals(automationComposition.getInstanceId(),
102                 cacheProvider.getAutomationComposition(automationComposition.getInstanceId()).getInstanceId());
103
104         assertThat(cacheProvider.getAutomationCompositions()).hasSize(1);
105         cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
106         assertThat(cacheProvider.getAutomationCompositions()).isEmpty();
107
108         cacheProvider.removeElementDefinition(compositionId);
109         assertThat(cacheProvider.getAcElementsDefinitions()).isEmpty();
110     }
111
112     @Test
113     void testDeply() {
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);
121
122         var participantDeploy =
123                 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
124         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
125                 participantDeploy);
126
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"));
132         }
133
134         // deploy again
135         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
136                 participantDeploy);
137
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"));
144         }
145     }
146
147     @Test
148     void testInitializeAutomationComposition() {
149         var parameter = CommonTestData.getParticipantParameters();
150         var cacheProvider = new CacheProvider(parameter);
151
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());
166         }
167     }
168 }