Merge "Rework the loop state"
[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  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.junit.Assert.assertNotNull;
28
29 import com.google.gson.Gson;
30 import com.google.gson.JsonObject;
31
32 import java.io.IOException;
33 import java.util.HashSet;
34 import java.util.Random;
35
36 import org.junit.Test;
37 import org.onap.clamp.clds.util.JsonUtils;
38 import org.onap.clamp.clds.util.ResourceFileUtil;
39 import org.onap.clamp.loop.components.external.PolicyComponent;
40 import org.onap.clamp.loop.log.LogType;
41 import org.onap.clamp.loop.log.LoopLog;
42 import org.onap.clamp.policy.microservice.MicroServicePolicy;
43 import org.onap.clamp.policy.operational.OperationalPolicy;
44 import org.skyscreamer.jsonassert.JSONAssert;
45
46 public class LoopToJsonTest {
47
48     private Gson gson = new Gson();
49
50     private OperationalPolicy getOperationalPolicy(String configJson, String name) {
51         return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class));
52     }
53
54     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
55         String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
56         Loop loop = new Loop(name, blueprint, svgRepresentation);
57         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
58         loop.setLastComputedState(LoopState.DESIGN);
59         loop.setDcaeDeploymentId(dcaeId);
60         loop.setDcaeDeploymentStatusUrl(dcaeUrl);
61         loop.setDcaeBlueprintId(dcaeBlueprintId);
62         return loop;
63     }
64
65     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
66         String policyTosca, String jsonProperties, boolean shared) {
67         MicroServicePolicy µService = new MicroServicePolicy(name, modelType, policyTosca, shared,
68             gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
69         µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
70
71         return µService;
72     }
73
74     private LoopLog getLoopLog(LogType type, String message, Loop loop) {
75         LoopLog log = new LoopLog(message, type, "CLAMP", loop);
76         log.setId(Long.valueOf(new Random().nextInt()));
77         return log;
78     }
79
80     @Test
81     public void LoopGsonTest() throws IOException {
82         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
83             "123456789", "https://dcaetest.org", "UUID-blueprint");
84         OperationalPolicy opPolicy = this.getOperationalPolicy(
85             ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
86         loopTest.addOperationalPolicy(opPolicy);
87         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
88             "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
89             "{\"param1\":\"value1\"}", true);
90         loopTest.addMicroServicePolicy(microServicePolicy);
91         LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
92         loopTest.addLog(loopLog);
93
94         String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest);
95         assertThat(jsonSerialized).isNotNull().isNotEmpty();
96         System.out.println(jsonSerialized);
97         Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
98         assertNotNull(loopTestDeserialized);
99         assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint",
100             "components");
101         assertThat(loopTestDeserialized.getComponent("DCAE").getState())
102             .isEqualToComparingFieldByField(loopTest.getComponent("DCAE").getState());
103         assertThat(loopTestDeserialized.getComponent("POLICY").getState())
104             .isEqualToComparingFieldByField(loopTest.getComponent("POLICY").getState());
105         // svg and blueprint not exposed so wont be deserialized
106         assertThat(loopTestDeserialized.getBlueprint()).isEqualTo(null);
107         assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
108
109         assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
110         assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
111         assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
112         assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,
113             "loop");
114     }
115
116     @Test
117     public void createPoliciesPayloadPdpGroupTest() throws IOException {
118         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
119             "123456789", "https://dcaetest.org", "UUID-blueprint");
120         OperationalPolicy opPolicy = this.getOperationalPolicy(
121             ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
122         loopTest.addOperationalPolicy(opPolicy);
123         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
124             "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
125             "{\"param1\":\"value1\"}", true);
126         loopTest.addMicroServicePolicy(microServicePolicy);
127
128         JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json"),
129             PolicyComponent.createPoliciesPayloadPdpGroup(loopTest), false);
130     }
131 }