Add template and tosca model entities and repositories
[clamp.git] / src / test / java / org / onap / clamp / loop / LoopToJsonTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  *  Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END============================================
22  * ===================================================================
23  *
24  */
25
26 package org.onap.clamp.loop;
27
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.junit.Assert.assertNotNull;
30
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33 import com.google.gson.JsonObject;
34 import com.google.gson.JsonSyntaxException;
35
36 import java.io.IOException;
37 import java.util.HashSet;
38 import java.util.Random;
39
40 import org.junit.Test;
41 import org.onap.clamp.clds.util.JsonUtils;
42 import org.onap.clamp.clds.util.ResourceFileUtil;
43 import org.onap.clamp.loop.components.external.PolicyComponent;
44 import org.onap.clamp.loop.log.LogType;
45 import org.onap.clamp.loop.log.LoopLog;
46 import org.onap.clamp.loop.service.Service;
47 import org.onap.clamp.loop.template.LoopTemplate;
48 import org.onap.clamp.loop.template.MicroServiceModel;
49 import org.onap.clamp.loop.template.PolicyModel;
50 import org.onap.clamp.policy.microservice.MicroServicePolicy;
51 import org.onap.clamp.policy.operational.OperationalPolicy;
52 import org.skyscreamer.jsonassert.JSONAssert;
53
54 public class LoopToJsonTest {
55
56     private Gson gson = new Gson();
57
58     private OperationalPolicy getOperationalPolicy(String configJson, String name) {
59         return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class));
60     }
61
62     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
63             String dcaeId, String dcaeUrl, String dcaeBlueprintId) throws JsonSyntaxException, IOException {
64         Loop loop = new Loop(name, blueprint, svgRepresentation);
65         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
66         loop.setLastComputedState(LoopState.DESIGN);
67         loop.setDcaeDeploymentId(dcaeId);
68         loop.setDcaeDeploymentStatusUrl(dcaeUrl);
69         loop.setDcaeBlueprintId(dcaeBlueprintId);
70         return loop;
71     }
72
73     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
74             String policyTosca, String jsonProperties, boolean shared) {
75         MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared,
76                 gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
77         microService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
78         return microService;
79     }
80
81     private MicroServiceModel getMicroServiceModel(String yaml, String name, PolicyModel policyModel) {
82         MicroServiceModel model = new MicroServiceModel();
83         model.setBlueprint(yaml);
84         model.setName(name);
85         model.setPolicyModel(policyModel);
86         return model;
87     }
88
89     private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym,
90             String policyVariant) {
91         return new PolicyModel(policyType, policyModelTosca, version, policyAcronym, policyVariant);
92     }
93
94     private LoopTemplate getLoopTemplate(String name, String blueprint, String svgRepresentation,
95             Integer maxInstancesAllowed) {
96         LoopTemplate template = new LoopTemplate(name, blueprint, svgRepresentation, maxInstancesAllowed, null);
97         template.addMicroServiceModel(getMicroServiceModel("yaml", "microService1",
98                 getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools", "type1")));
99         return template;
100     }
101
102     private LoopLog getLoopLog(LogType type, String message, Loop loop) {
103         LoopLog log = new LoopLog(message, type, "CLAMP", loop);
104         log.setId(Long.valueOf(new Random().nextInt()));
105         return log;
106     }
107
108     @Test
109     public void loopGsonTest() throws IOException {
110         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
111                 "123456789", "https://dcaetest.org", "UUID-blueprint");
112         OperationalPolicy opPolicy = this.getOperationalPolicy(
113                 ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
114         loopTest.addOperationalPolicy(opPolicy);
115         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
116                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
117                 "{\"param1\":\"value1\"}", true);
118         loopTest.addMicroServicePolicy(microServicePolicy);
119         LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
120         loopTest.addLog(loopLog);
121         LoopTemplate loopTemplate = getLoopTemplate("templateName", "yaml", "svg", 1);
122         loopTest.setLoopTemplate(loopTemplate);
123
124         String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest);
125         assertThat(jsonSerialized).isNotNull().isNotEmpty();
126         System.out.println(jsonSerialized);
127         Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
128         assertNotNull(loopTestDeserialized);
129         assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint",
130                 "components");
131         assertThat(loopTestDeserialized.getComponent("DCAE").getState())
132                 .isEqualToComparingFieldByField(loopTest.getComponent("DCAE").getState());
133         assertThat(loopTestDeserialized.getComponent("POLICY").getState()).isEqualToComparingOnlyGivenFields(
134                 loopTest.getComponent("POLICY").getState(), "stateName", "description");
135         // svg and blueprint not exposed so wont be deserialized
136         assertThat(loopTestDeserialized.getBlueprint()).isEqualTo(null);
137         assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
138
139         assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
140         assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
141         assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
142         assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,
143                 "loop");
144
145         // Verify the loop template
146         assertThat(loopTestDeserialized.getLoopTemplate()).isEqualTo(loopTemplate);
147     }
148
149     @Test
150     public void loopServiceTest() throws IOException {
151         Loop loopTest2 = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
152                 "123456789", "https://dcaetest.org", "UUID-blueprint");
153
154         JsonObject jsonModel = new GsonBuilder().create()
155                 .fromJson(ResourceFileUtil.getResourceAsString("tosca/model-properties.json"), JsonObject.class);
156         Service service = new Service(jsonModel.get("serviceDetails").getAsJsonObject(),
157                 jsonModel.get("resourceDetails").getAsJsonObject(), "1.0");
158         loopTest2.setModelService(service);
159         String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest2);
160         assertThat(jsonSerialized).isNotNull().isNotEmpty();
161         System.out.println(jsonSerialized);
162
163         Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
164         assertNotNull(loopTestDeserialized);
165         assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest2, "modelService", "svgRepresentation",
166                 "blueprint", "components");
167     }
168
169     @Test
170     public void createPoliciesPayloadPdpGroupTest() throws IOException {
171         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
172                 "123456789", "https://dcaetest.org", "UUID-blueprint");
173         OperationalPolicy opPolicy = this.getOperationalPolicy(
174                 ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
175         loopTest.addOperationalPolicy(opPolicy);
176         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
177                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
178                 "{\"param1\":\"value1\"}", true);
179         loopTest.addMicroServicePolicy(microServicePolicy);
180
181         JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json"),
182                 PolicyComponent.createPoliciesPayloadPdpGroup(loopTest), false);
183     }
184 }