6bd25bb9a87aae8bd515a8b91c999066f1dabf24
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2023,2025 OpenInfra Foundation Europe. All rights reserved.
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.assertDoesNotThrow;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotEquals;
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         assertDoesNotThrow(() -> new JpaParticipantSupportedElementType("key", "key"));
78         assertDoesNotThrow(() -> new JpaParticipantSupportedElementType("key", "key", "name",
79             "1.0.0"));
80     }
81
82     @Test
83     void testJpaParticipantSupportedElementType() {
84         var testJpaSupportElement = createJpaParticipantSupportedElementType();
85
86         var testSupportedElement = createParticipantSupportedElementType();
87         assertEquals(testSupportedElement.getId(), testJpaSupportElement.toAuthorative().getId());
88         assertEquals(testSupportedElement.getTypeName(), testJpaSupportElement.toAuthorative().getTypeName());
89         assertEquals(testSupportedElement.getTypeVersion(), testJpaSupportElement.toAuthorative().getTypeVersion());
90
91         assertThatThrownBy(() -> {
92             testJpaSupportElement.fromAuthorative(null);
93         }).hasMessageMatching("participantSupportedElementType is marked .*ull but is null");
94
95         assertThatThrownBy(() -> new JpaParticipantSupportedElementType((JpaParticipantSupportedElementType) null))
96             .isInstanceOf(NullPointerException.class);
97
98         var testJpaSupportElementFa =
99             new JpaParticipantSupportedElementType(testSupportedElement.getId().toString(),
100                 testJpaSupportElement.getParticipantId());
101         testJpaSupportElementFa.fromAuthorative(testSupportedElement);
102         assertEquals(testJpaSupportElement, testJpaSupportElementFa);
103
104         assertEquals(ID, testJpaSupportElement.getId());
105
106         var testJpaSupportElement2 = new JpaParticipantSupportedElementType(testJpaSupportElement);
107         assertEquals(testJpaSupportElement, testJpaSupportElement2);
108     }
109
110     @Test
111     void testJpaAutomationCompositionElementCompareTo() {
112         var testJpaSupportElement = createJpaParticipantSupportedElementType();
113
114         var otherJpaSupportElement =
115             new JpaParticipantSupportedElementType(testJpaSupportElement);
116         assertEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
117         assertEquals(-1, testJpaSupportElement.compareTo(null));
118         assertEquals(0, testJpaSupportElement.compareTo(testJpaSupportElement));
119
120         testJpaSupportElement.setId("BadValue");
121         assertNotEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
122         testJpaSupportElement.setId(ID);
123         assertEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
124
125         testJpaSupportElement.setParticipantId("BadValue");
126         assertNotEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
127         testJpaSupportElement.setParticipantId(PARTICIPANT_ID);
128         assertEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
129
130         testJpaSupportElement.setTypeName("BadName");
131         assertNotEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
132         testJpaSupportElement.setTypeName("type");
133         assertEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
134
135         testJpaSupportElement.setTypeVersion("BadVersion");
136         assertNotEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
137         testJpaSupportElement.setTypeVersion("1.0.0");
138         assertEquals(0, testJpaSupportElement.compareTo(otherJpaSupportElement));
139
140         assertEquals(testJpaSupportElement,
141             new JpaParticipantSupportedElementType(otherJpaSupportElement));
142     }
143
144     private JpaParticipantSupportedElementType createJpaParticipantSupportedElementType() {
145         var testSupportedElement = createParticipantSupportedElementType();
146         var testJpaSupportElement = new JpaParticipantSupportedElementType(testSupportedElement.getId().toString(),
147             PARTICIPANT_ID);
148
149         testJpaSupportElement.fromAuthorative(testSupportedElement);
150
151         return testJpaSupportElement;
152     }
153
154     private ParticipantSupportedElementType createParticipantSupportedElementType() {
155         var testSupportedElement = new ParticipantSupportedElementType();
156         testSupportedElement.setId(UUID.fromString(ID));
157         testSupportedElement.setTypeName("type");
158         testSupportedElement.setTypeVersion("1.0.0");
159
160         return testSupportedElement;
161     }
162 }