Introduce new entities
[clamp.git] / src / test / java / org / onap / clamp / it / dao / model / LoopItCase.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.it.dao.model;
25
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31
32 import java.util.Map;
33
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.onap.clamp.clds.Application;
37 import org.onap.clamp.dao.LoopsRepository;
38 import org.onap.clamp.dao.model.LogType;
39 import org.onap.clamp.dao.model.Loop;
40 import org.onap.clamp.dao.model.LoopLog;
41 import org.onap.clamp.dao.model.LoopState;
42 import org.onap.clamp.dao.model.MicroServicePolicy;
43 import org.onap.clamp.dao.model.OperationalPolicy;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.test.context.SpringBootTest;
46 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47
48 @RunWith(SpringJUnit4ClassRunner.class)
49 @SpringBootTest(classes = Application.class)
50 public class LoopItCase {
51
52     @Autowired
53     private LoopsRepository loopRepository;
54
55     @Test
56     public void DecodeEncodeTest() {
57         Loop loopTest = new Loop();
58         loopTest.setName("ClosedLoopTest");
59         loopTest.setSvgRepresentation("representation");
60         loopTest.setBlueprint("blueprint");
61         loopTest.setGlobalPropertiesJson(new Gson().fromJson("{\"testName\":\"testValue\"}", Map.class));
62         loopTest.setLastComputedState(LoopState.DESIGN);
63         loopTest.setBlueprint("yaml");
64
65         OperationalPolicy opPolicy = new OperationalPolicy();
66         opPolicy.setName("OpPolicyTest");
67         opPolicy.setConfigurationsJson(new Gson().fromJson("{\"testname\":\"testvalue\"}", Map.class));
68         opPolicy.setLoop(loopTest);
69         loopTest.addOperationalPolicy(opPolicy);
70
71         MicroServicePolicy µService = new MicroServicePolicy();
72         µService.setJsonRepresentation(new Gson().fromJson("{\"testrepresentation\":\"value\"}", Map.class));
73         µService.setPolicyTosca("tosca");
74         µService.setProperties(new Gson().fromJson("{\"testparam\":\"testvalue\"}", Map.class));
75         µService.setShared(true);
76
77         µService.setName("ConfigPolicyTest");
78         loopTest.addMicroServicePolicy(µService);
79
80         LoopLog log = new LoopLog();
81         log.setLogType(LogType.INFO);
82         log.setMessage("test message");
83
84         loopTest.addLog(log);
85
86         Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
87         String json = gson.toJson(loopTest);
88         assertNotNull(json);
89         Loop loopTestDecoded = gson.fromJson(json, Loop.class);
90         assertNotNull(loopTestDecoded);
91         Loop loop = gson.fromJson(json, Loop.class);
92         assertNotNull(loop);
93
94         Loop loopInDb = loopRepository.save(loopTest);
95         assertNotNull(loopInDb);
96         assertTrue(loopRepository.findById(loopInDb.getName()).get().getName().equals("ClosedLoopTest"));
97     }
98 }