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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.models.tosca.simple.concepts;
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;
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;
39 * DAO test for ToscaTimeInterval.
41 * @author Liam Fallon (liam.fallon@est.tech)
43 class JpaToscaTimeIntervalTest {
45 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
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()));
54 assertThatThrownBy(() -> new JpaToscaTimeInterval((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL);
56 assertThatThrownBy(() -> new JpaToscaTimeInterval(null, null, null)).hasMessageMatching(KEY_IS_NULL);
58 assertThatThrownBy(() -> new JpaToscaTimeInterval(null, null, Instant.now())).hasMessageMatching(KEY_IS_NULL);
60 assertThatThrownBy(() -> new JpaToscaTimeInterval(null, Instant.now(), null)).hasMessageMatching(KEY_IS_NULL);
62 assertThatThrownBy(() -> new JpaToscaTimeInterval(null, Instant.now(), Instant.now()))
63 .hasMessageMatching(KEY_IS_NULL);
65 assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), null, null))
66 .hasMessageMatching("startTime is marked .*on.*ull but is null");
68 assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), null, Instant.now()))
69 .hasMessageMatching("startTime is marked .*on.*ull but is null");
71 assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), Instant.now(), null))
72 .hasMessageMatching("endTime is marked .*on.*ull but is null");
74 assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
75 .isInstanceOf(NullPointerException.class);
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);
86 JpaToscaTimeInterval tdtClone0 = new JpaToscaTimeInterval(tti);
87 assertEquals(tti, tdtClone0);
88 assertEquals(0, tti.compareTo(tdtClone0));
90 JpaToscaTimeInterval tdtClone1 = new JpaToscaTimeInterval(tti);
91 assertEquals(tti, tdtClone1);
92 assertEquals(0, tti.compareTo(tdtClone1));
94 assertEquals(-1, tti.compareTo(null));
95 assertEquals(0, tti.compareTo(tti));
96 assertNotEquals(0, tti.compareTo(tti.getKey()));
98 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherTimeInterval");
99 JpaToscaTimeInterval otherDt = new JpaToscaTimeInterval(otherDtKey);
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));
109 assertEquals(1, tti.getKeys().size());
110 assertEquals(1, new JpaToscaTimeInterval().getKeys().size());
112 new JpaToscaTimeInterval().clean();
114 assertEquals(tdtClone0, tti);
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());
125 tti.setStartTime(null);
126 assertFalse(tti.validate("").isValid());
128 tti.setStartTime(Timestamp.from(endTime.plusSeconds(1)));
129 assertFalse(tti.validate("").isValid());
130 tti.setStartTime(Timestamp.from(startTime));
131 assertTrue(tti.validate("").isValid());
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());
140 assertThatThrownBy(() -> tti.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
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);