8afb5aead7cf6c7bb64d056320c182540f385274
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2023 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.models.acm.persistence.concepts;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27
28 import java.util.UUID;
29 import org.junit.jupiter.api.Test;
30 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
31
32 /**
33  * Test the {@link JpaParticipantSupportedElementType} class.
34  */
35 class JpaParticipantSupportedElementTypeTest {
36
37     private static final String NULL_PARTICIPANT_ID_ERROR = "participantId is marked .*ull but is null";
38     private static final String NULL_ID_ERROR = "id is marked .*ull but is null";
39     private static final String NULL_ERROR = " is marked .*ull but is null";
40     private static final String ID = "a95757ba-b34a-4049-a2a8-46773abcbe5e";
41     private static final String PARTICIPANT_ID = "a78757co-b34a-8949-a2a8-46773abcbe2a";
42
43     @Test
44     void testJpaAutomationCompositionElementConstructor() {
45         assertThatThrownBy(() -> {
46             new JpaParticipantSupportedElementType((JpaParticipantSupportedElementType) null);
47         }).hasMessageMatching("copyConcept is marked .*ull but is null");
48
49         assertThatThrownBy(() -> {
50             new JpaParticipantSupportedElementType("key", null);
51         }).hasMessageMatching(NULL_PARTICIPANT_ID_ERROR);
52
53         assertThatThrownBy(() -> {
54             new JpaParticipantSupportedElementType(null, "key");
55         }).hasMessageMatching(NULL_ID_ERROR);
56
57         assertThatThrownBy(() -> {
58             new JpaParticipantSupportedElementType(null, null);
59         }).hasMessageMatching(NULL_ID_ERROR);
60
61         assertThatThrownBy(() -> {
62             new JpaParticipantSupportedElementType(null, null, null, null);
63         }).hasMessageMatching(NULL_ID_ERROR);
64
65         assertThatThrownBy(() -> {
66             new JpaParticipantSupportedElementType("key", null, null, null);
67         }).hasMessageMatching(NULL_PARTICIPANT_ID_ERROR);
68
69         assertThatThrownBy(() -> {
70             new JpaParticipantSupportedElementType("key", "key", null, "1.0.0");
71         }).hasMessageMatching("typeName" + NULL_ERROR);
72
73         assertThatThrownBy(() -> {
74             new JpaParticipantSupportedElementType("key", "key", "name", null);
75         }).hasMessageMatching("typeVersion" + NULL_ERROR);
76
77         assertNotNull(new JpaParticipantSupportedElementType());
78         assertNotNull(new JpaParticipantSupportedElementType("key", "key"));
79         assertNotNull(new JpaParticipantSupportedElementType("key", "key", "name",
80             "1.0.0"));
81     }
82
83     @Test
84     void testJpaParticipantSupportedElementType() {
85         var testJpaSupportElement = createJpaParticipantSupportedElementType();
86
87         var testSupportedElement = createParticipantSupportedElementType();
88         assertEquals(testSupportedElement.getId(), testJpaSupportElement.toAuthorative().getId());
89         assertEquals(testSupportedElement.getTypeName(), testJpaSupportElement.toAuthorative().getTypeName());
90         assertEquals(testSupportedElement.getTypeVersion(), testJpaSupportElement.toAuthorative().getTypeVersion());
91
92         assertThatThrownBy(() -> {
93             testJpaSupportElement.fromAuthorative(null);
94         }).hasMessageMatching("participantSupportedElementType is marked .*ull but is null");
95
96         assertThatThrownBy(() -> new JpaParticipantSupportedElementType((JpaParticipantSupportedElementType) null))
97             .isInstanceOf(NullPointerException.class);
98
99         var testJpaSupportElementFa =
100             new JpaParticipantSupportedElementType(testSupportedElement.getId().toString(),
101                 testJpaSupportElement.getParticipantId());
102         testJpaSupportElementFa.fromAuthorative(testSupportedElement);
103         assertEquals(testJpaSupportElement, testJpaSupportElementFa);
104
105         assertEquals(ID, testJpaSupportElement.getId());
106
107         var testJpaSupportElement2 = new JpaParticipantSupportedElementType(testJpaSupportElement);
108         assertEquals(testJpaSupportElement, testJpaSupportElement2);
109     }
110
111     @Test
112     void testJpaAutomationCompositionElementCompareTo() {
113         var testJpaSupportElement = createJpaParticipantSupportedElementType();
114
115         var otherJpaSupportElement =
116             new JpaParticipantSupportedElementType(testJpaSupportElement);
117         assertEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
118         assertEquals(-1, testJpaSupportElement.compareTo(null));
119         assertEquals(0, testJpaSupportElement.compareTo(testJpaSupportElement));
120
121         testJpaSupportElement.setId("BadValue");
122         assertNotEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
123         testJpaSupportElement.setId(ID);
124         assertEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
125
126         testJpaSupportElement.setParticipantId("BadValue");
127         assertNotEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
128         testJpaSupportElement.setParticipantId(PARTICIPANT_ID);
129         assertEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
130
131         testJpaSupportElement.setTypeName("BadName");
132         assertNotEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
133         testJpaSupportElement.setTypeName("type");
134         assertEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
135
136         testJpaSupportElement.setTypeVersion("BadVersion");
137         assertNotEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
138         testJpaSupportElement.setTypeVersion("1.0.0");
139         assertEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
140
141         assertEquals(testJpaSupportElement,
142             new JpaParticipantSupportedElementType(otherJpaSupportElement));
143     }
144
145     private JpaParticipantSupportedElementType createJpaParticipantSupportedElementType() {
146         var testSupportedElement = createParticipantSupportedElementType();
147         var testJpaSupportElement = new JpaParticipantSupportedElementType(testSupportedElement.getId().toString(),
148             PARTICIPANT_ID);
149
150         testJpaSupportElement.fromAuthorative(testSupportedElement);
151
152         return testJpaSupportElement;
153     }
154
155     private ParticipantSupportedElementType createParticipantSupportedElementType() {
156         var testSupportedElement = new ParticipantSupportedElementType();
157         testSupportedElement.setId(UUID.fromString(ID));
158         testSupportedElement.setTypeName("type");
159         testSupportedElement.setTypeVersion("1.0.0");
160
161         return testSupportedElement;
162     }
163 }