92a59722f644c59d918891af0d444b53758e7ad7
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2024 Nordix Foundation
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.tosca.simple.concepts;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertFalse;
28 import static org.junit.jupiter.api.Assertions.assertNotEquals;
29 import static org.junit.jupiter.api.Assertions.assertNotNull;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
31
32 import java.sql.Timestamp;
33 import java.time.Instant;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfReferenceKey;
37
38 /**
39  * DAO test for ToscaTimeInterval.
40  *
41  * @author Liam Fallon (liam.fallon@est.tech)
42  */
43 class JpaToscaTimeIntervalTest {
44
45     private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
46
47     @Test
48     void testTimeIntervalPojo() {
49         assertNotNull(new JpaToscaTimeInterval());
50         assertNotNull(new JpaToscaTimeInterval(new PfReferenceKey()));
51         assertNotNull(new JpaToscaTimeInterval(new PfReferenceKey(), Instant.now(), Instant.now()));
52         assertNotNull(new JpaToscaTimeInterval(new JpaToscaTimeInterval()));
53
54         assertThatThrownBy(() -> new JpaToscaTimeInterval((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL);
55
56         assertThatThrownBy(() -> new JpaToscaTimeInterval(null, null, null)).hasMessageMatching(KEY_IS_NULL);
57
58         assertThatThrownBy(() -> new JpaToscaTimeInterval(null, null, Instant.now())).hasMessageMatching(KEY_IS_NULL);
59
60         assertThatThrownBy(() -> new JpaToscaTimeInterval(null, Instant.now(), null)).hasMessageMatching(KEY_IS_NULL);
61
62         assertThatThrownBy(() -> new JpaToscaTimeInterval(null, Instant.now(), Instant.now()))
63                 .hasMessageMatching(KEY_IS_NULL);
64
65         assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), null, null))
66                 .hasMessageMatching("startTime is marked .*on.*ull but is null");
67
68         assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), null, Instant.now()))
69                 .hasMessageMatching("startTime is marked .*on.*ull but is null");
70
71         assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), Instant.now(), null))
72                 .hasMessageMatching("endTime is marked .*on.*ull but is null");
73
74         assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
75                 .isInstanceOf(NullPointerException.class);
76     }
77
78     @Test
79     void testTimeInterval() {
80         PfConceptKey ttiParentKey = new PfConceptKey("tParentKey", "0.0.1");
81         PfReferenceKey ttiKey = new PfReferenceKey(ttiParentKey, "trigger0");
82         Instant startTime = Instant.ofEpochSecond(1000);
83         Instant endTime = Instant.ofEpochSecond(2000);
84         JpaToscaTimeInterval tti = new JpaToscaTimeInterval(ttiKey, startTime, endTime);
85
86         JpaToscaTimeInterval tdtClone0 = new JpaToscaTimeInterval(tti);
87         assertEquals(tti, tdtClone0);
88         assertEquals(0, tti.compareTo(tdtClone0));
89
90         JpaToscaTimeInterval tdtClone1 = new JpaToscaTimeInterval(tti);
91         assertEquals(tti, tdtClone1);
92         assertEquals(0, tti.compareTo(tdtClone1));
93
94         assertEquals(-1, tti.compareTo(null));
95         assertEquals(0, tti.compareTo(tti));
96         assertNotEquals(0, tti.compareTo(tti.getKey()));
97
98         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherTimeInterval");
99         JpaToscaTimeInterval otherDt = new JpaToscaTimeInterval(otherDtKey);
100
101         assertNotEquals(0, tti.compareTo(otherDt));
102         otherDt.setKey(ttiKey);
103         assertNotEquals(0, tti.compareTo(otherDt));
104         otherDt.setStartTime(Timestamp.from(startTime));
105         assertNotEquals(0, tti.compareTo(otherDt));
106         otherDt.setEndTime(Timestamp.from(endTime));
107         assertEquals(0, tti.compareTo(otherDt));
108
109         assertEquals(1, tti.getKeys().size());
110         assertEquals(1, new JpaToscaTimeInterval().getKeys().size());
111
112         new JpaToscaTimeInterval().clean();
113         tti.clean();
114         assertEquals(tdtClone0, tti);
115     }
116
117     @Test
118     void testTimeIntervalValidation() {
119         Instant startTime = Instant.ofEpochSecond(1000);
120         Instant endTime = Instant.ofEpochSecond(2000);
121         JpaToscaTimeInterval tti = setUpJpaToscaTimeInterval(startTime, endTime);
122         assertFalse(new JpaToscaTimeInterval().validate("").isValid());
123         assertTrue(tti.validate("").isValid());
124
125         tti.setStartTime(null);
126         assertFalse(tti.validate("").isValid());
127
128         tti.setStartTime(Timestamp.from(endTime.plusSeconds(1)));
129         assertFalse(tti.validate("").isValid());
130         tti.setStartTime(Timestamp.from(startTime));
131         assertTrue(tti.validate("").isValid());
132
133         tti.setEndTime(null);
134         assertFalse(tti.validate("").isValid());
135         tti.setEndTime(Timestamp.from(startTime.minusSeconds(1)));
136         assertFalse(tti.validate("").isValid());
137         tti.setEndTime(Timestamp.from(endTime));
138         assertTrue(tti.validate("").isValid());
139
140         assertThatThrownBy(() -> tti.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
141     }
142
143     private JpaToscaTimeInterval setUpJpaToscaTimeInterval(Instant startTime, Instant endTime) {
144         PfConceptKey ttiParentKey = new PfConceptKey("tParentKey", "0.0.1");
145         PfReferenceKey ttiKey = new PfReferenceKey(ttiParentKey, "trigger0");
146         JpaToscaTimeInterval tti = new JpaToscaTimeInterval(ttiKey, startTime, endTime);
147
148         return tti;
149     }
150 }