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.dao.model;
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.junit.Assert.assertNotNull;
29 import com.google.gson.Gson;
32 import java.util.Random;
34 import org.junit.Test;
35 import org.onap.clamp.clds.util.JsonUtils;
37 public class LoopToJsonTest {
39 private OperationalPolicy getOperationalPolicy(String configJson, String name) {
40 OperationalPolicy opPolicy = new OperationalPolicy();
41 opPolicy.setName(name);
42 opPolicy.setConfigurationsJson(new Gson().fromJson(configJson, Map.class));
46 private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
47 String dcaeId, String dcaeUrl) {
48 Loop loop = new Loop();
50 loop.setSvgRepresentation(svgRepresentation);
51 loop.setBlueprint(blueprint);
52 loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, Map.class));
53 loop.setLastComputedState(LoopState.DESIGN);
54 loop.setDcaeDeploymentId(dcaeId);
55 loop.setDcaeDeploymentStatusUrl(dcaeUrl);
59 private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca,
60 String jsonProperties, boolean shared) {
61 MicroServicePolicy µService = new MicroServicePolicy();
62 µService.setJsonRepresentation(new Gson().fromJson(jsonRepresentation, Map.class));
63 µService.setPolicyTosca(policyTosca);
64 µService.setProperties(new Gson().fromJson(jsonProperties, Map.class));
65 µService.setShared(shared);
67 µService.setName(name);
71 private LoopLog getLoopLog(LogType type, String message) {
72 LoopLog log = new LoopLog();
74 log.setMessage(message);
75 log.setId(Long.valueOf(new Random().nextInt()));
80 public void LoopGsonTest() {
81 Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
82 "123456789", "https://dcaetest.org");
83 OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest");
84 loopTest.addOperationalPolicy(opPolicy);
85 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}",
86 "YamlContent", "{\"param1\":\"value1\"}", true);
87 loopTest.addMicroServicePolicy(microServicePolicy);
88 LoopLog loopLog = getLoopLog(LogType.INFO, "test message");
89 loopTest.addLog(loopLog);
91 String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest);
92 assertThat(jsonSerialized).isNotNull().isNotEmpty();
93 System.out.println(jsonSerialized);
94 Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
95 assertNotNull(loopTestDeserialized);
96 assertThat(loopTestDeserialized).isEqualToComparingFieldByField(loopTest);
97 assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
98 assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
99 assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
100 assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,