bbce423518a7ea6881e42017866fa81ea379dee0
[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
80     @Test
81     void testinitCommonProperties() throws AutomationCompositionException {
82         var parameter = CommonTestData.getParticipantParameters();
83         var cacheProvider = new CacheProvider(parameter);
84         var participantDeploy = new ParticipantDeploy();
85         participantDeploy.setParticipantId(cacheProvider.getParticipantId());
86
87         var compositionId = UUID.randomUUID();
88
89         List<AutomationCompositionElementDefinition> definitions = new ArrayList<>();
90         var automationComposition =
91                 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
92         automationComposition.setInstanceId(UUID.randomUUID());
93         automationComposition.setCompositionId(compositionId);
94         for (var element : automationComposition.getElements().values()) {
95             var acElementDefinition = new AutomationCompositionElementDefinition();
96             acElementDefinition.setAcElementDefinitionId(element.getDefinition());
97             var nodeTemplate = new ToscaNodeTemplate();
98             nodeTemplate.setProperties(Map.of("key", "value"));
99             acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(nodeTemplate);
100             definitions.add(acElementDefinition);
101
102             var acElement = new AcElementDeploy();
103             acElement.setId(element.getId());
104             acElement.setDefinition(element.getDefinition());
105             participantDeploy.getAcElementList().add(acElement);
106         }
107         cacheProvider.addElementDefinition(compositionId, definitions);
108
109         cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
110                 participantDeploy);
111
112         for (var element : automationComposition.getElements().values()) {
113             var commonProperties =
114                     cacheProvider.getCommonProperties(automationComposition.getInstanceId(), element.getId());
115             assertEquals("value", commonProperties.get("key"));
116         }
117
118         assertEquals(automationComposition.getInstanceId(),
119                 cacheProvider.getAutomationComposition(automationComposition.getInstanceId()).getInstanceId());
120
121         assertThat(cacheProvider.getAutomationCompositions()).hasSize(1);
122         cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
123         assertThat(cacheProvider.getAutomationCompositions()).isEmpty();
124
125         cacheProvider.removeElementDefinition(compositionId);
126         assertThat(cacheProvider.getAcElementsDefinitions()).isEmpty();
127     }
128 }