2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.loop;
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.junit.Assert.assertNotNull;
29 import com.google.gson.Gson;
30 import com.google.gson.JsonObject;
32 import java.util.HashSet;
33 import java.util.Random;
35 import org.junit.Test;
36 import org.onap.clamp.clds.util.JsonUtils;
37 import org.onap.clamp.loop.log.LogType;
38 import org.onap.clamp.loop.log.LoopLog;
39 import org.onap.clamp.policy.microservice.MicroServicePolicy;
40 import org.onap.clamp.policy.operational.OperationalPolicy;
42 public class LoopToJsonTest {
44 private Gson gson = new Gson();
46 private OperationalPolicy getOperationalPolicy(String configJson, String name) {
47 return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class));
50 private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
51 String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
52 Loop loop = new Loop(name, blueprint, svgRepresentation);
53 loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
54 loop.setLastComputedState(LoopState.DESIGN);
55 loop.setDcaeDeploymentId(dcaeId);
56 loop.setDcaeDeploymentStatusUrl(dcaeUrl);
57 loop.setDcaeBlueprintId(dcaeBlueprintId);
61 private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
62 String policyTosca, String jsonProperties, boolean shared) {
63 MicroServicePolicy µService = new MicroServicePolicy(name, modelType, policyTosca, shared,
64 gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
65 µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
70 private LoopLog getLoopLog(LogType type, String message, Loop loop) {
71 LoopLog log = new LoopLog(message, type, loop);
72 log.setId(Long.valueOf(new Random().nextInt()));
77 public void LoopGsonTest() {
78 Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
79 "123456789", "https://dcaetest.org", "UUID-blueprint");
80 OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest");
81 loopTest.addOperationalPolicy(opPolicy);
82 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
83 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
84 "{\"param1\":\"value1\"}", true);
85 loopTest.addMicroServicePolicy(microServicePolicy);
86 LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
87 loopTest.addLog(loopLog);
89 String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest);
90 assertThat(jsonSerialized).isNotNull().isNotEmpty();
91 System.out.println(jsonSerialized);
92 Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
93 assertNotNull(loopTestDeserialized);
94 assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint");
96 // svg and blueprint not exposed so wont be deserialized
97 assertThat(loopTestDeserialized.getBlueprint()).isEqualTo(null);
98 assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
100 assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
101 assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
102 assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
103 assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,