Merge "Rework the hibernate adapter"
[clamp.git] / src / test / java / org / onap / clamp / dao / model / LoopToJsonTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.dao.model;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.junit.Assert.assertNotNull;
28
29 import com.google.gson.Gson;
30 import com.google.gson.JsonObject;
31
32 import java.util.Random;
33
34 import org.junit.Test;
35 import org.onap.clamp.clds.util.JsonUtils;
36
37 public class LoopToJsonTest {
38
39     private OperationalPolicy getOperationalPolicy(String configJson, String name) {
40         OperationalPolicy opPolicy = new OperationalPolicy();
41         opPolicy.setName(name);
42         opPolicy.setConfigurationsJson(new Gson().fromJson(configJson, JsonObject.class));
43         return opPolicy;
44     }
45
46     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
47         String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
48         Loop loop = new Loop();
49         loop.setName(name);
50         loop.setSvgRepresentation(svgRepresentation);
51         loop.setBlueprint(blueprint);
52         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
53         loop.setLastComputedState(LoopState.DESIGN);
54         loop.setDcaeDeploymentId(dcaeId);
55         loop.setDcaeDeploymentStatusUrl(dcaeUrl);
56         loop.setDcaeBlueprintId(dcaeBlueprintId);
57         return loop;
58     }
59
60     private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca,
61         String jsonProperties, boolean shared) {
62         MicroServicePolicy µService = new MicroServicePolicy();
63         µService.setJsonRepresentation(new Gson().fromJson(jsonRepresentation, JsonObject.class));
64         µService.setPolicyTosca(policyTosca);
65         µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
66         µService.setShared(shared);
67
68         µService.setName(name);
69         return µService;
70     }
71
72     private LoopLog getLoopLog(LogType type, String message) {
73         LoopLog log = new LoopLog();
74         log.setLogType(type);
75         log.setMessage(message);
76         log.setId(Long.valueOf(new Random().nextInt()));
77         return log;
78     }
79
80     @Test
81     public void LoopGsonTest() {
82         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
83             "123456789", "https://dcaetest.org", "UUID-blueprint");
84         OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest");
85         loopTest.addOperationalPolicy(opPolicy);
86         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}",
87             "YamlContent", "{\"param1\":\"value1\"}", true);
88         loopTest.addMicroServicePolicy(microServicePolicy);
89         LoopLog loopLog = getLoopLog(LogType.INFO, "test message");
90         loopTest.addLog(loopLog);
91
92         String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest);
93         assertThat(jsonSerialized).isNotNull().isNotEmpty();
94         System.out.println(jsonSerialized);
95         Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
96         assertNotNull(loopTestDeserialized);
97         assertThat(loopTestDeserialized).isEqualToComparingFieldByField(loopTest);
98         assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
99         assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
100         assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
101         assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,
102             "loop");
103     }
104 }