b68bf48a08a8d69ca2da029f18070005945856ae
[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.policy.microservice.MicroServicePolicy;
48 import org.onap.clamp.policy.operational.OperationalPolicy;
49 import org.skyscreamer.jsonassert.JSONAssert;
50
51 public class LoopToJsonTest {
52
53     private Gson gson = new Gson();
54
55     private OperationalPolicy getOperationalPolicy(String configJson, String name) {
56         return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class));
57     }
58
59     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
60             String dcaeId, String dcaeUrl, String dcaeBlueprintId) throws JsonSyntaxException, IOException {
61         Loop loop = new Loop(name, blueprint, svgRepresentation);
62         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
63         loop.setLastComputedState(LoopState.DESIGN);
64         loop.setDcaeDeploymentId(dcaeId);
65         loop.setDcaeDeploymentStatusUrl(dcaeUrl);
66         loop.setDcaeBlueprintId(dcaeBlueprintId);
67         return loop;
68     }
69
70     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
71             String policyTosca, String jsonProperties, boolean shared) {
72         MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared,
73                 gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
74         microService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
75
76         return microService;
77     }
78
79     private LoopLog getLoopLog(LogType type, String message, Loop loop) {
80         LoopLog log = new LoopLog(message, type, "CLAMP", loop);
81         log.setId(Long.valueOf(new Random().nextInt()));
82         return log;
83     }
84
85     @Test
86     public void loopGsonTest() throws IOException {
87         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
88                 "123456789", "https://dcaetest.org", "UUID-blueprint");
89         OperationalPolicy opPolicy = this.getOperationalPolicy(
90                 ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
91         loopTest.addOperationalPolicy(opPolicy);
92         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
93                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
94                 "{\"param1\":\"value1\"}", true);
95         loopTest.addMicroServicePolicy(microServicePolicy);
96         LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
97         loopTest.addLog(loopLog);
98
99         String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest);
100         assertThat(jsonSerialized).isNotNull().isNotEmpty();
101         System.out.println(jsonSerialized);
102         Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
103         assertNotNull(loopTestDeserialized);
104         assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint",
105                 "components");
106         assertThat(loopTestDeserialized.getComponent("DCAE").getState())
107                 .isEqualToComparingFieldByField(loopTest.getComponent("DCAE").getState());
108         assertThat(loopTestDeserialized.getComponent("POLICY").getState()).isEqualToComparingOnlyGivenFields(
109                 loopTest.getComponent("POLICY").getState(), "stateName", "description");
110         // svg and blueprint not exposed so wont be deserialized
111         assertThat(loopTestDeserialized.getBlueprint()).isEqualTo(null);
112         assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
113
114         assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
115         assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
116         assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
117         assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,
118                 "loop");
119     }
120
121     @Test
122     public void loopServiceTest() throws IOException {
123         Loop loopTest2 = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
124                 "123456789", "https://dcaetest.org", "UUID-blueprint");
125
126         JsonObject jsonModel = new GsonBuilder().create()
127                 .fromJson(ResourceFileUtil.getResourceAsString("tosca/model-properties.json"), JsonObject.class);
128         Service service = new Service(jsonModel.get("serviceDetails").getAsJsonObject(),
129                 jsonModel.get("resourceDetails").getAsJsonObject());
130         loopTest2.setModelService(service);
131
132         String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest2);
133         assertThat(jsonSerialized).isNotNull().isNotEmpty();
134         System.out.println(jsonSerialized);
135         JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/loop.json"),
136                 jsonSerialized, true);
137
138         Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
139         assertNotNull(loopTestDeserialized);
140         assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest2, "modelService", 
141             "svgRepresentation", "blueprint", "components");
142     }
143
144     @Test
145     public void createPoliciesPayloadPdpGroupTest() throws IOException {
146         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
147                 "123456789", "https://dcaetest.org", "UUID-blueprint");
148         OperationalPolicy opPolicy = this.getOperationalPolicy(
149                 ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
150         loopTest.addOperationalPolicy(opPolicy);
151         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
152                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
153                 "{\"param1\":\"value1\"}", true);
154         loopTest.addMicroServicePolicy(microServicePolicy);
155
156         JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json"),
157                 PolicyComponent.createPoliciesPayloadPdpGroup(loopTest), false);
158     }
159 }