2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
26 package org.onap.clamp.loop;
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.junit.Assert.assertNotNull;
31 import com.google.gson.Gson;
32 import com.google.gson.JsonObject;
34 import java.io.IOException;
35 import java.util.HashSet;
36 import java.util.Random;
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;
47 public class LoopToJsonTest {
49 private Gson gson = new Gson();
51 private OperationalPolicy getOperationalPolicy(String configJson, String name) {
52 return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class));
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);
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));
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()));
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);
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");
102 // svg and blueprint not exposed so wont be deserialized
103 assertThat(loopTestDeserialized.getBlueprint()).isEqualTo(null);
104 assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
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,
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);
125 JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json"),
126 loopTest.createPoliciesPayloadPdpGroup(), false);