Removing Named Query.
[policy/models.git] / models-interactions / model-yaml / src / test / java / org / onap / policy / controlloop / policy / ControlLoopTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2018 Ericsson. All rights reserved.
4  * Modifications Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
5  * Modifications Copyright (C) 2019 Nordix Foundation.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.policy;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.junit.Ignore;
32 import org.junit.Test;
33
34 import org.onap.aai.domain.yang.Pnf;
35 import org.onap.policy.common.utils.io.Serializer;
36 import org.onap.policy.sdc.Resource;
37 import org.onap.policy.sdc.ResourceType;
38 import org.onap.policy.sdc.Service;
39
40 public class ControlLoopTest {
41
42     private static final String SERVICE2 = "service2";
43     private static final String SERVICE1 = "service1";
44     private static final String RESOURCE2 = "resource2";
45     private static final String RESOURCE1 = "resource1";
46     private static final String PNF1 = "pnf 1";
47     private String controlLoopName = "control loop 1";
48     private String version = "1.0.1";
49     private String triggerPolicy = FinalResult.FINAL_OPENLOOP.toString();
50     private Integer timeout = 100;
51     private Boolean abatement = false;
52
53     @Test
54     public void testEqualsSameInstance() {
55         ControlLoop controlLoop1 = new ControlLoop();
56         assertTrue(controlLoop1.equals(controlLoop1));
57     }
58
59     @Test
60     public void testEqualsNull() {
61         ControlLoop controlLoop1 = new ControlLoop();
62         assertFalse(controlLoop1.equals(null));
63     }
64
65     @Test
66     public void testEqualsInstanceOfDiffClass() {
67         ControlLoop controlLoop1 = new ControlLoop();
68         assertFalse(controlLoop1.equals(""));
69     }
70
71     @Test
72     public void testEqualsNoServicesAndResourcesOrTimeout() {
73         final Pnf pnf = new Pnf();
74         pnf.setPnfName(PNF1);
75
76         ControlLoop controlLoop1 = new ControlLoop();
77         controlLoop1.setControlLoopName(controlLoopName);
78         controlLoop1.setVersion(version);
79         controlLoop1.setPnf(pnf);
80         controlLoop1.setTrigger_policy(triggerPolicy);
81         controlLoop1.setAbatement(abatement);
82
83         ControlLoop controlLoop2 = new ControlLoop();
84         controlLoop2.setControlLoopName(controlLoopName);
85         controlLoop2.setVersion(version);
86         controlLoop2.setPnf(pnf);
87         controlLoop2.setTrigger_policy(triggerPolicy);
88         controlLoop2.setAbatement(abatement);
89
90         assertTrue(controlLoop1.equals(controlLoop2));
91     }
92
93     @Test
94     public void testEquals() throws IOException {
95         final Pnf pnf = new Pnf();
96         pnf.setPnfName(PNF1);
97
98         ControlLoop controlLoop1 = new ControlLoop();
99         controlLoop1.setControlLoopName(controlLoopName);
100         controlLoop1.setVersion(version);
101         Service service1 = new Service(SERVICE1);
102         Service service2 = new Service(SERVICE2);
103         List<Service> services = new ArrayList<>();
104         services.add(service1);
105         services.add(service2);
106         controlLoop1.setServices(services);
107         Resource resource1 = new Resource(RESOURCE1, ResourceType.VF);
108         Resource resource2 = new Resource(RESOURCE2, ResourceType.VFC);
109         List<Resource> resources = new ArrayList<>();
110         resources.add(resource1);
111         resources.add(resource2);
112         controlLoop1.setResources(resources);
113         controlLoop1.setPnf(pnf);
114         controlLoop1.setTrigger_policy(triggerPolicy);
115         controlLoop1.setTimeout(timeout);
116         controlLoop1.setAbatement(abatement);
117
118         ControlLoop controlLoop2 = new ControlLoop();
119         controlLoop2.setControlLoopName(controlLoopName);
120         controlLoop2.setVersion(version);
121         Service controlLoop2Service1 = new Service(SERVICE1);
122         Service controlLoop2Service2 = new Service(SERVICE2);
123         List<Service> controlLoop2Services = new ArrayList<>();
124         controlLoop2Services.add(controlLoop2Service1);
125         controlLoop2Services.add(controlLoop2Service2);
126         controlLoop2.setServices(controlLoop2Services);
127         Resource controlLoop2Resource1 = new Resource(RESOURCE1, ResourceType.VF);
128         Resource controlLoop2Resource2 = new Resource(RESOURCE2, ResourceType.VFC);
129         List<Resource> controlLoop2Resources = new ArrayList<>();
130         controlLoop2Resources.add(controlLoop2Resource1);
131         controlLoop2Resources.add(controlLoop2Resource2);
132         controlLoop2.setResources(controlLoop2Resources);
133         controlLoop2.setPnf(pnf);
134         controlLoop2.setTrigger_policy(triggerPolicy);
135         controlLoop2.setTimeout(timeout);
136         controlLoop1.setAbatement(abatement);
137
138         assertTrue(controlLoop1.equals(controlLoop2));
139         assertEquals(controlLoop1.hashCode(), controlLoop2.hashCode());
140
141         controlLoop2 = Serializer.roundTrip(controlLoop1);
142         assertTrue(controlLoop1.equals(controlLoop2));
143         assertEquals(controlLoop1.hashCode(), controlLoop2.hashCode());
144     }
145
146     @Test
147     @Ignore
148     // I'VE MARKED THIS TEST CASE AS IGNORE BECAUSE THE TEST CASE FAILS
149     // This test case fails because the ControlLoop(ControlLoop controlLoop) constructor.
150     // does not copy the value of pnf and version into the newly created object
151     // PLEASE ADVISE IF THE EXISTING BEHAVIOUR IS CORRECT
152     public void testControlLoop() {
153         final Pnf pnf = new Pnf();
154         pnf.setPnfName(PNF1);
155
156         ControlLoop controlLoop1 = new ControlLoop();
157         controlLoop1.setControlLoopName(controlLoopName);
158         controlLoop1.setVersion(version);
159         Service service1 = new Service(SERVICE1);
160         Service service2 = new Service(SERVICE2);
161         List<Service> services = new ArrayList<>();
162         services.add(service1);
163         services.add(service2);
164         controlLoop1.setServices(services);
165         Resource resource1 = new Resource(RESOURCE1, ResourceType.VF);
166         Resource resource2 = new Resource(RESOURCE2, ResourceType.VFC);
167         List<Resource> resources = new ArrayList<>();
168         resources.add(resource1);
169         resources.add(resource2);
170         controlLoop1.setResources(resources);
171         controlLoop1.setPnf(pnf);
172         controlLoop1.setTrigger_policy(triggerPolicy);
173         controlLoop1.setAbatement(abatement);
174
175         ControlLoop controlLoop2 = new ControlLoop(controlLoop1);
176
177         assertEquals(controlLoop1.getControlLoopName(), controlLoop2.getControlLoopName());
178         assertEquals(controlLoop1.getVersion(), controlLoop2.getVersion());
179         assertEquals(controlLoop1.getServices(), controlLoop2.getServices());
180         assertEquals(controlLoop1.getResources(), controlLoop2.getResources());
181         assertEquals(controlLoop1.getPnf(), controlLoop2.getPnf());
182         assertEquals(controlLoop1.getTrigger_policy(), controlLoop2.getTrigger_policy());
183         assertEquals(controlLoop1.getAbatement(), controlLoop2.getAbatement());
184
185         assertTrue(controlLoop1.equals(controlLoop2));
186     }
187
188 }