Add ACM interceptor for DME
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / test / java / com / oransc / rappmanager / models / AcmInterceptorTest.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 OpenInfra Foundation Europe. All rights reserved.
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  * ============LICENSE_END========================================================================
17  */
18
19 package com.oransc.rappmanager.models;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.mockito.Mockito.mock;
23
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonParser;
26 import com.oransc.rappmanager.models.rapp.Rapp;
27 import com.oransc.rappmanager.models.rappinstance.RappInstance;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.UUID;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
39
40 class AcmInterceptorTest implements AcmInterceptor {
41
42     @Test
43     void testInjectAutomationComposition() {
44         AutomationComposition automationComposition = new AutomationComposition();
45         Map<UUID, AutomationCompositionElement> elements =
46                 new HashMap<>(Map.of(UUID.randomUUID(), new AutomationCompositionElement()));
47         automationComposition.setElements(elements);
48         assertEquals(1, automationComposition.getElements().size());
49         injectAutomationComposition(automationComposition, mock(Rapp.class), mock(RappInstance.class));
50         assertEquals(2, automationComposition.getElements().size());
51     }
52
53     @Test
54     void testInjectToscaServiceTemplate() {
55         ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
56         toscaServiceTemplate.setDataTypes(new HashMap<>(Map.of("datatype1", new ToscaDataType())));
57         toscaServiceTemplate.setNodeTypes(new HashMap<>(Map.of("nodetype1", new ToscaNodeType())));
58         ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
59         ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
60         String elements = "elements";
61         toscaNodeTemplate.setProperties(new HashMap<>(Map.of(elements, "[{}]")));
62         String nodeTemplateKey = "onap.policy.clamp.ac.element.AutomationCompositionDefinition";
63
64         toscaTopologyTemplate.setNodeTemplates(new HashMap<>(Map.of(nodeTemplateKey, toscaNodeTemplate)));
65         toscaServiceTemplate.setToscaTopologyTemplate(toscaTopologyTemplate);
66         Object o =
67                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().get(nodeTemplateKey).getProperties()
68                         .get(elements);
69         JsonArray elementArray = JsonParser.parseString(o.toString()).getAsJsonArray();
70         assertEquals(1, toscaServiceTemplate.getDataTypes().size());
71         assertEquals(1, toscaServiceTemplate.getNodeTypes().size());
72         assertEquals(1, elementArray.size());
73         injectToscaServiceTemplate(toscaServiceTemplate);
74         Object modObject =
75                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().get(nodeTemplateKey).getProperties()
76                         .get(elements);
77         JsonArray newElementArray = JsonParser.parseString(modObject.toString()).getAsJsonArray();
78         assertEquals(2, toscaServiceTemplate.getDataTypes().size());
79         assertEquals(2, toscaServiceTemplate.getNodeTypes().size());
80         assertEquals(2, newElementArray.size());
81     }
82
83     @Override
84     public void injectToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
85         injectToscaServiceTemplate(toscaServiceTemplate, "element1", "1.0.0");
86     }
87
88     @Override
89     public Map<String, ToscaNodeTemplate> getNodeTemplates() {
90         return new HashMap<>(Map.of(UUID.randomUUID().toString(), new ToscaNodeTemplate()));
91     }
92
93     @Override
94     public Map<String, ToscaNodeType> getNodeTypes() {
95         return new HashMap<>(Map.of(UUID.randomUUID().toString(), new ToscaNodeType()));
96     }
97
98     @Override
99     public Map<String, ToscaDataType> getDataTypes() {
100         return new HashMap<>(Map.of(UUID.randomUUID().toString(), new ToscaDataType()));
101     }
102
103     @Override
104     public Map<UUID, AutomationCompositionElement> getInstantiationElement(Rapp rapp, RappInstance rappInstance) {
105         return new HashMap<>(Map.of(UUID.randomUUID(), new AutomationCompositionElement()));
106     }
107 }