51a4fc7aeeb8f34245e1a4796f5b762e81c5289e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.models.controlloop.concepts;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29
30 import java.time.Instant;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.UUID;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.models.base.PfKey;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
37
38 class ControlLoopTest {
39     @Test
40     void testControlLoop() {
41         var cl0 = new ControlLoop();
42         cl0.setDefinition(new ToscaConceptIdentifier("dfName", "1.2.3"));
43         assertEquals("dfName", cl0.getType());
44         assertEquals("1.2.3", cl0.getTypeVersion());
45
46         var cl1 = new ControlLoop(cl0);
47         assertEquals(cl0, cl1);
48
49         assertEquals(0, cl0.compareTo(cl1));
50     }
51
52     @Test
53     void testControlLoopLombok() {
54         assertNotNull(new ControlLoop());
55         var cl0 = new ControlLoop();
56         cl0.setElements(new LinkedHashMap<>());
57
58         assertThat(cl0.toString()).contains("ControlLoop(");
59         assertThat(cl0.hashCode()).isNotZero();
60         assertEquals(true, cl0.equals(cl0));
61         assertEquals(false, cl0.equals(null));
62
63         var cl1 = new ControlLoop();
64
65         cl1.setDefinition(new ToscaConceptIdentifier("defName", "0.0.1"));
66         cl1.setDescription("Description");
67         cl1.setElements(new LinkedHashMap<>());
68         cl1.setName("Name");
69         cl1.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
70         cl1.setState(ControlLoopState.UNINITIALISED);
71         cl1.setVersion("0.0.1");
72
73         assertThat(cl1.toString()).contains("ControlLoop(");
74         assertEquals(false, cl1.hashCode() == 0);
75         assertEquals(false, cl1.equals(cl0));
76         assertEquals(false, cl1.equals(null));
77
78         assertNotEquals(cl1, cl0);
79
80         var cl2 = new ControlLoop();
81         cl2.setElements(new LinkedHashMap<>());
82
83         // @formatter:off
84         assertThatThrownBy(() -> cl2.setDefinition(null)).  isInstanceOf(NullPointerException.class);
85         assertThatThrownBy(() -> cl2.setOrderedState(null)).isInstanceOf(NullPointerException.class);
86         assertThatThrownBy(() -> cl2.setState(null)).       isInstanceOf(NullPointerException.class);
87         // @formatter:on
88
89         assertEquals(cl2, cl0);
90
91         cl1.setCascadedOrderedState(ControlLoopOrderedState.PASSIVE);
92         assertEquals(ControlLoopOrderedState.PASSIVE, cl1.getOrderedState());
93
94         cl1.getElements().put(UUID.randomUUID(), new ControlLoopElement());
95         cl1.setCascadedOrderedState(ControlLoopOrderedState.RUNNING);
96         assertEquals(ControlLoopOrderedState.RUNNING, cl1.getOrderedState());
97         assertEquals(ControlLoopOrderedState.RUNNING, cl1.getElements().values().iterator().next().getOrderedState());
98
99         assertNull(cl0.getElements().get(UUID.randomUUID()));
100         assertNull(cl1.getElements().get(UUID.randomUUID()));
101
102         assertEquals(PfKey.NULL_KEY_NAME, cl0.getDefinition().getName());
103
104     }
105
106     @Test
107     void testControlLoopElementStatisticsList() {
108         var cl = new ControlLoop();
109         List<ClElementStatistics> emptylist = cl.getControlLoopElementStatisticsList(cl);
110         assertNull(emptylist);
111
112         var cl1 = getControlLoopTest();
113         List<ClElementStatistics> list = cl1.getControlLoopElementStatisticsList(cl1);
114         assertNotNull(list);
115         assertEquals(2, list.size());
116         assertEquals(ControlLoopState.UNINITIALISED, list.get(0).getControlLoopState());
117     }
118
119     private ControlLoop getControlLoopTest() {
120         var cl = new ControlLoop();
121         cl.setDefinition(new ToscaConceptIdentifier("defName", "1.2.3"));
122         cl.setDescription("Description");
123         cl.setElements(new LinkedHashMap<>());
124         cl.setName("Name");
125         cl.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
126         cl.setState(ControlLoopState.UNINITIALISED);
127         cl.setVersion("0.0.1");
128
129         var uuid = UUID.randomUUID();
130         var id = new ToscaConceptIdentifier(
131                 "org.onap.policy.controlloop.PolicyControlLoopParticipant", "1.0.1");
132         var clElement = getControlLoopElementTest(uuid, id);
133
134         var uuid2 = UUID.randomUUID();
135         var id2 = new ToscaConceptIdentifier(
136                 "org.onap.policy.controlloop.PolicyControlLoopParticipantIntermediary", "0.0.1");
137         var clElement2 = getControlLoopElementTest(uuid2, id2);
138
139         cl.getElements().put(uuid, clElement);
140         cl.getElements().put(uuid2, clElement2);
141         return cl;
142     }
143
144     private ControlLoopElement getControlLoopElementTest(UUID uuid, ToscaConceptIdentifier id) {
145         var clElement = new ControlLoopElement();
146         clElement.setId(uuid);
147         clElement.setParticipantId(id);
148         clElement.setDefinition(id);
149         clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
150
151         var clElementStatistics = new ClElementStatistics();
152         clElementStatistics.setParticipantId(id);
153         clElementStatistics.setControlLoopState(ControlLoopState.UNINITIALISED);
154         clElementStatistics.setTimeStamp(Instant.now());
155
156         clElement.setClElementStatistics(clElementStatistics);
157
158         return clElement;
159     }
160 }