Fixed Checkstyle issues
[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.JsonObject;
33
34 import java.io.IOException;
35 import java.util.HashSet;
36 import java.util.Random;
37
38 import org.junit.Test;
39 import org.onap.clamp.clds.util.JsonUtils;
40 import org.onap.clamp.clds.util.ResourceFileUtil;
41 import org.onap.clamp.loop.log.LogType;
42 import org.onap.clamp.loop.log.LoopLog;
43 import org.onap.clamp.policy.microservice.MicroServicePolicy;
44 import org.onap.clamp.policy.operational.OperationalPolicy;
45 import org.skyscreamer.jsonassert.JSONAssert;
46
47 public class LoopToJsonTest {
48
49     private Gson gson = new Gson();
50
51     private OperationalPolicy getOperationalPolicy(String configJson, String name) {
52         return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class));
53     }
54
55     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
56         String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
57         Loop loop = new Loop(name, blueprint, svgRepresentation);
58         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
59         loop.setLastComputedState(LoopState.DESIGN);
60         loop.setDcaeDeploymentId(dcaeId);
61         loop.setDcaeDeploymentStatusUrl(dcaeUrl);
62         loop.setDcaeBlueprintId(dcaeBlueprintId);
63         return loop;
64     }
65
66     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
67         String policyTosca, String jsonProperties, boolean shared) {
68         MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared,
69             gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
70         microService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
71
72         return microService;
73     }
74
75     private LoopLog getLoopLog(LogType type, String message, Loop loop) {
76         LoopLog log = new LoopLog(message, type, loop);
77         log.setId(Long.valueOf(new Random().nextInt()));
78         return log;
79     }
80
81     @Test
82     public void loopGsonTest() throws IOException {
83         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
84             "123456789", "https://dcaetest.org", "UUID-blueprint");
85         OperationalPolicy opPolicy = this.getOperationalPolicy(
86             ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
87         loopTest.addOperationalPolicy(opPolicy);
88         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
89             "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
90             "{\"param1\":\"value1\"}", true);
91         loopTest.addMicroServicePolicy(microServicePolicy);
92         LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
93         loopTest.addLog(loopLog);
94
95         String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest);
96         assertThat(jsonSerialized).isNotNull().isNotEmpty();
97         System.out.println(jsonSerialized);
98         Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
99         assertNotNull(loopTestDeserialized);
100         assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint");
101
102         // svg and blueprint not exposed so wont be deserialized
103         assertThat(loopTestDeserialized.getBlueprint()).isEqualTo(null);
104         assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
105
106         assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
107         assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
108         assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
109         assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,
110             "loop");
111     }
112
113     @Test
114     public void createPoliciesPayloadPdpGroupTest() throws IOException {
115         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
116             "123456789", "https://dcaetest.org", "UUID-blueprint");
117         OperationalPolicy opPolicy = this.getOperationalPolicy(
118             ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
119         loopTest.addOperationalPolicy(opPolicy);
120         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
121             "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
122             "{\"param1\":\"value1\"}", true);
123         loopTest.addMicroServicePolicy(microServicePolicy);
124
125         JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json"),
126             loopTest.createPoliciesPayloadPdpGroup(), false);
127     }
128 }