aboutsummaryrefslogtreecommitdiffstats
path: root/tosca-controlloop/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/concepts/ControlLoopTest.java
blob: c865b76abd796b224545b87a4b0ead1d5038dc0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.controlloop.models.controlloop.concepts;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.LinkedHashMap;
import java.util.UUID;
import org.junit.Test;
import org.onap.policy.models.base.PfKey;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;

public class ControlLoopTest {
    @Test
    public void testControlLoop() {
        ControlLoop cl0 = new ControlLoop();
        cl0.setDefinition(new ToscaConceptIdentifier("dfName", "1.2.3"));
        assertEquals("dfName", cl0.getType());
        assertEquals("1.2.3", cl0.getTypeVersion());

        ControlLoop cl1 = new ControlLoop(cl0);
        assertEquals(cl0, cl1);

        assertEquals(0, cl0.compareTo(cl1));
    }

    @Test
    public void testControlLoopLombok() {
        assertNotNull(new ControlLoop());
        ControlLoop cl0 = new ControlLoop();
        cl0.setElements(new LinkedHashMap<>());

        assertThat(cl0.toString()).contains("ControlLoop(");
        assertThat(cl0.hashCode()).isNotZero();
        assertEquals(true, cl0.equals(cl0));
        assertEquals(false, cl0.equals(null));

        ControlLoop cl1 = new ControlLoop();

        cl1.setDefinition(new ToscaConceptIdentifier("defName", "0.0.1"));
        cl1.setDescription("Description");
        cl1.setElements(new LinkedHashMap<>());
        cl1.setName("Name");
        cl1.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
        cl1.setState(ControlLoopState.UNINITIALISED);
        cl1.setVersion("0.0.1");

        assertThat(cl1.toString()).contains("ControlLoop(");
        assertEquals(false, cl1.hashCode() == 0);
        assertEquals(false, cl1.equals(cl0));
        assertEquals(false, cl1.equals(null));

        assertNotEquals(cl1, cl0);

        ControlLoop cl2 = new ControlLoop();
        cl2.setElements(new LinkedHashMap<>());

        // @formatter:off
        assertThatThrownBy(() -> cl2.setDefinition(null)).  isInstanceOf(NullPointerException.class);
        assertThatThrownBy(() -> cl2.setOrderedState(null)).isInstanceOf(NullPointerException.class);
        assertThatThrownBy(() -> cl2.setState(null)).       isInstanceOf(NullPointerException.class);
        // @formatter:on

        assertEquals(cl2, cl0);

        cl1.setCascadedOrderedState(ControlLoopOrderedState.PASSIVE);
        assertEquals(ControlLoopOrderedState.PASSIVE, cl1.getOrderedState());

        cl1.getElements().put(UUID.randomUUID(), new ControlLoopElement());
        cl1.setCascadedOrderedState(ControlLoopOrderedState.RUNNING);
        assertEquals(ControlLoopOrderedState.RUNNING, cl1.getOrderedState());
        assertEquals(ControlLoopOrderedState.RUNNING, cl1.getElements().values().iterator().next().getOrderedState());

        assertNull(cl0.getElements().get(UUID.randomUUID()));
        assertNull(cl1.getElements().get(UUID.randomUUID()));

        assertEquals(PfKey.NULL_KEY_NAME, cl0.getDefinition().getName());
    }
}