2c0a455416cfe9461d8f9f44d37c8eb2517e212b
[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.util.ArrayList;
31 import java.util.UUID;
32 import org.junit.Test;
33 import org.onap.policy.models.base.PfKey;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
35
36 public class ControlLoopTest {
37     @Test
38     public void testControlLoop() {
39         ControlLoop cl0 = new ControlLoop();
40         cl0.setDefinition(new ToscaConceptIdentifier("dfName", "1.2.3"));
41         assertEquals("dfName", cl0.getType());
42         assertEquals("1.2.3", cl0.getTypeVersion());
43
44         ControlLoop cl1 = new ControlLoop(cl0);
45         assertEquals(cl0, cl1);
46
47         assertEquals(0, cl0.compareTo(cl1));
48     }
49
50     @Test
51     public void testControlLoopLombok() {
52         assertNotNull(new ControlLoop());
53         ControlLoop cl0 = new ControlLoop();
54
55         assertThat(cl0.toString()).contains("ControlLoop(");
56         assertThat(cl0.hashCode()).isNotZero();
57         assertEquals(true, cl0.equals(cl0));
58         assertEquals(false, cl0.equals(null));
59
60         ControlLoop cl1 = new ControlLoop();
61
62         cl1.setDefinition(new ToscaConceptIdentifier("defName", "0.0.1"));
63         cl1.setDescription("Description");
64         cl1.setElements(new ArrayList<>());
65         cl1.setName("Name");
66         cl1.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
67         cl1.setState(ControlLoopState.UNINITIALISED);
68         cl1.setVersion("0.0.1");
69
70         assertThat(cl1.toString()).contains("ControlLoop(");
71         assertEquals(false, cl1.hashCode() == 0);
72         assertEquals(false, cl1.equals(cl0));
73         assertEquals(false, cl1.equals(null));
74
75         assertNotEquals(cl1, cl0);
76
77         ControlLoop cl2 = new ControlLoop();
78
79         // @formatter:off
80         assertThatThrownBy(() -> cl2.setDefinition(null)).  isInstanceOf(NullPointerException.class);
81         assertThatThrownBy(() -> cl2.setOrderedState(null)).isInstanceOf(NullPointerException.class);
82         assertThatThrownBy(() -> cl2.setState(null)).       isInstanceOf(NullPointerException.class);
83         // @formatter:on
84
85         assertEquals(cl2, cl0);
86
87         cl1.setCascadedOrderedState(ControlLoopOrderedState.PASSIVE);
88         assertEquals(ControlLoopOrderedState.PASSIVE, cl1.getOrderedState());
89
90         cl1.getElements().add(new ControlLoopElement());
91         cl1.setCascadedOrderedState(ControlLoopOrderedState.RUNNING);
92         assertEquals(ControlLoopOrderedState.RUNNING, cl1.getOrderedState());
93         assertEquals(ControlLoopOrderedState.RUNNING, cl1.getElements().get(0).getOrderedState());
94
95         assertNull(cl0.getElement(UUID.randomUUID()));
96         assertNull(cl1.getElement(UUID.randomUUID()));
97         assertEquals(cl1.getElements().get(0), cl1.getElement(cl1.getElements().get(0).getId()));
98
99         assertEquals(PfKey.NULL_KEY_NAME, cl0.getDefinition().getName());
100     }
101 }