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.loop.log.LogType;
37 import org.onap.clamp.loop.log.LoopLog;
38 import org.onap.clamp.policy.microservice.MicroServicePolicy;
39 import org.onap.clamp.policy.operational.OperationalPolicy;
40 import org.onap.clamp.clds.util.JsonUtils;
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 jsonRepresentation, String policyTosca,
62 String jsonProperties, boolean shared) {
63 MicroServicePolicy µService = new MicroServicePolicy(name, 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) {
71 LoopLog log = new LoopLog();
73 log.setMessage(message);
74 log.setId(Long.valueOf(new Random().nextInt()));
79 public void LoopGsonTest() {
80 Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
81 "123456789", "https://dcaetest.org", "UUID-blueprint");
82 OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest");
83 loopTest.addOperationalPolicy(opPolicy);
84 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}",
85 "YamlContent", "{\"param1\":\"value1\"}", true);
86 loopTest.addMicroServicePolicy(microServicePolicy);
87 LoopLog loopLog = getLoopLog(LogType.INFO, "test message");
88 loopTest.addLog(loopLog);
90 String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest);
91 assertThat(jsonSerialized).isNotNull().isNotEmpty();
92 System.out.println(jsonSerialized);
93 Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
94 assertNotNull(loopTestDeserialized);
95 assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint");
97 //svg and blueprint not exposed so wont be deserialized
98 assertThat(loopTestDeserialized.getBlueprint()).isEqualTo(null);
99 assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
101 assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
102 assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
103 assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
104 assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,