Improve tests
[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
31 import java.util.Map;
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, Map.class));
43         return opPolicy;
44     }
45
46     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
47         String dcaeId, String dcaeUrl) {
48         Loop loop = new Loop();
49         loop.setName(name);
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);
56         return loop;
57     }
58
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);
66
67         µService.setName(name);
68         return µService;
69     }
70
71     private LoopLog getLoopLog(LogType type, String message) {
72         LoopLog log = new LoopLog();
73         log.setLogType(type);
74         log.setMessage(message);
75         log.setId(Long.valueOf(new Random().nextInt()));
76         return log;
77     }
78
79     @Test
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);
90
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,
101             "loop");
102     }
103 }