f207dc511204daddc347394b49f783ceba1d0380
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.ArrayList;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.UUID;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
33 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionException;
34 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
36 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
38
39 class CacheProviderTest {
40
41     @Test
42     void testgetSupportedAcElementTypes() {
43         var parameter = CommonTestData.getParticipantParameters();
44         var cacheProvider = new CacheProvider(parameter);
45         assertEquals(parameter.getIntermediaryParameters().getParticipantId(), cacheProvider.getParticipantId());
46         assertEquals(parameter.getIntermediaryParameters().getParticipantSupportedElementTypes().get(0),
47                 cacheProvider.getSupportedAcElementTypes().get(0));
48     }
49
50     @Test
51     void testNotNull() {
52         var parameter = CommonTestData.getParticipantParameters();
53         var cacheProvider = new CacheProvider(parameter);
54         var instanceId = UUID.randomUUID();
55         assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(null, null, null))
56                 .isInstanceOf(NullPointerException.class);
57         assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(instanceId, null, null))
58                 .isInstanceOf(NullPointerException.class);
59         assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(instanceId, instanceId, null))
60                 .isInstanceOf(NullPointerException.class);
61
62         assertThatThrownBy(() -> cacheProvider.addElementDefinition(null, null))
63                 .isInstanceOf(NullPointerException.class);
64         assertThatThrownBy(() -> cacheProvider.addElementDefinition(instanceId, null))
65                 .isInstanceOf(NullPointerException.class);
66
67         assertThatThrownBy(() -> cacheProvider.getAutomationComposition(null)).isInstanceOf(NullPointerException.class);
68
69         assertThatThrownBy(() -> cacheProvider.getCommonProperties(null, null))
70                 .isInstanceOf(NullPointerException.class);
71         assertThatThrownBy(() -> cacheProvider.getCommonProperties(instanceId, null))
72                 .isInstanceOf(NullPointerException.class);
73
74         assertThatThrownBy(() -> cacheProvider.removeAutomationComposition(null))
75                 .isInstanceOf(NullPointerException.class);
76
77         assertThatThrownBy(() -> cacheProvider.removeElementDefinition(null)).isInstanceOf(NullPointerException.class);
78
79         assertThatThrownBy(() -> cacheProvider.initializeAutomationComposition(null, null))
80                 .isInstanceOf(NullPointerException.class);
81     }
82
83     @Test
84     void testinitCommonProperties() throws AutomationCompositionException {
85         var parameter = CommonTestData.getParticipantParameters();
86         var cacheProvider = new CacheProvider(parameter);
87         var participantDeploy = new ParticipantDeploy();
88         participantDeploy.setParticipantId(cacheProvider.getParticipantId());
89
90         var compositionId = UUID.randomUUID();
91
92         List<AutomationCompositionElementDefinition> definitions = new ArrayList<>();
93         var automationComposition =
94                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
95         automationComposition.setInstanceId(UUID.randomUUID());
96         automationComposition.setCompositionId(compositionId);
97         for (var element : automationComposition.getElements().values()) {
98             var acElementDefinition = new AutomationCompositionElementDefinition();
99             acElementDefinition.setAcElementDefinitionId(element.getDefinition());
100             var nodeTemplate = new ToscaNodeTemplate();
101             nodeTemplate.setProperties(Map.of("key", "value"));
102             acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(nodeTemplate);
103             definitions.add(acElementDefinition);
104
105             var acElement = new AcElementDeploy();
106             acElement.setId(element.getId());
107             acElement.setDefinition(element.getDefinition());
108             participantDeploy.getAcElementList().add(acElement);
109         }
110         cacheProvider.addElementDefinition(compositionId, definitions);
111
112         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
113                 participantDeploy);
114
115         for (var element : automationComposition.getElements().values()) {
116             var commonProperties =
117                     cacheProvider.getCommonProperties(automationComposition.getInstanceId(), element.getId());
118             assertEquals("value", commonProperties.get("key"));
119         }
120
121         assertEquals(automationComposition.getInstanceId(),
122                 cacheProvider.getAutomationComposition(automationComposition.getInstanceId()).getInstanceId());
123
124         assertThat(cacheProvider.getAutomationCompositions()).hasSize(1);
125         cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
126         assertThat(cacheProvider.getAutomationCompositions()).isEmpty();
127
128         cacheProvider.removeElementDefinition(compositionId);
129         assertThat(cacheProvider.getAcElementsDefinitions()).isEmpty();
130     }
131
132     @Test
133     void testInitializeAutomationComposition() {
134         var parameter = CommonTestData.getParticipantParameters();
135         var cacheProvider = new CacheProvider(parameter);
136
137         var participantRestartAc = CommonTestData.createParticipantRestartAc();
138         var compositionId = UUID.randomUUID();
139         cacheProvider.initializeAutomationComposition(compositionId, participantRestartAc);
140         var result = cacheProvider.getAutomationComposition(participantRestartAc.getAutomationCompositionId());
141         assertEquals(compositionId, result.getCompositionId());
142         assertEquals(participantRestartAc.getAutomationCompositionId(), result.getInstanceId());
143     }
144 }