2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
31 import java.util.Date;
32 import org.junit.Test;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfReferenceKey;
37 * DAO test for ToscaTimeInterval.
39 * @author Liam Fallon (liam.fallon@est.tech)
41 public class JpaToscaTimeIntervalTest {
43 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
46 public void testTimeIntervalPojo() {
47 assertNotNull(new JpaToscaTimeInterval());
48 assertNotNull(new JpaToscaTimeInterval(new PfReferenceKey()));
49 assertNotNull(new JpaToscaTimeInterval(new PfReferenceKey(), new Date(), new Date()));
50 assertNotNull(new JpaToscaTimeInterval(new JpaToscaTimeInterval()));
52 assertThatThrownBy(() -> new JpaToscaTimeInterval((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL);
54 assertThatThrownBy(() -> new JpaToscaTimeInterval(null, null, null)).hasMessageMatching(KEY_IS_NULL);
56 assertThatThrownBy(() -> new JpaToscaTimeInterval(null, null, new Date())).hasMessageMatching(KEY_IS_NULL);
58 assertThatThrownBy(() -> new JpaToscaTimeInterval(null, new Date(), null)).hasMessageMatching(KEY_IS_NULL);
60 assertThatThrownBy(() -> new JpaToscaTimeInterval(null, new Date(), new Date()))
61 .hasMessageMatching(KEY_IS_NULL);
63 assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), null, null))
64 .hasMessageMatching("startTime is marked .*on.*ull but is null");
66 assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), null, new Date()))
67 .hasMessageMatching("startTime is marked .*on.*ull but is null");
69 assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), new Date(), null))
70 .hasMessageMatching("endTime is marked .*on.*ull but is null");
72 assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
73 .isInstanceOf(NullPointerException.class);
75 PfConceptKey ttiParentKey = new PfConceptKey("tParentKey", "0.0.1");
76 PfReferenceKey ttiKey = new PfReferenceKey(ttiParentKey, "trigger0");
77 Date startTime = new Date(1000);
78 Date endTime = new Date(2000);
79 JpaToscaTimeInterval tti = new JpaToscaTimeInterval(ttiKey, startTime, endTime);
81 JpaToscaTimeInterval tdtClone0 = new JpaToscaTimeInterval(tti);
82 assertEquals(tti, tdtClone0);
83 assertEquals(0, tti.compareTo(tdtClone0));
85 JpaToscaTimeInterval tdtClone1 = new JpaToscaTimeInterval(tti);
86 assertEquals(tti, tdtClone1);
87 assertEquals(0, tti.compareTo(tdtClone1));
89 assertEquals(-1, tti.compareTo(null));
90 assertEquals(0, tti.compareTo(tti));
91 assertNotEquals(0, tti.compareTo(tti.getKey()));
93 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherTimeInterval");
94 JpaToscaTimeInterval otherDt = new JpaToscaTimeInterval(otherDtKey);
96 assertNotEquals(0, tti.compareTo(otherDt));
97 otherDt.setKey(ttiKey);
98 assertNotEquals(0, tti.compareTo(otherDt));
99 otherDt.setStartTime(startTime);
100 assertNotEquals(0, tti.compareTo(otherDt));
101 otherDt.setEndTime(endTime);
102 assertEquals(0, tti.compareTo(otherDt));
104 assertEquals(1, tti.getKeys().size());
105 assertEquals(1, new JpaToscaTimeInterval().getKeys().size());
107 new JpaToscaTimeInterval().clean();
109 assertEquals(tdtClone0, tti);
111 assertFalse(new JpaToscaTimeInterval().validate("").isValid());
112 assertTrue(tti.validate("").isValid());
114 tti.setStartTime(null);
115 assertFalse(tti.validate("").isValid());
116 tti.setStartTime(new Date(endTime.getTime() + 1));
117 assertFalse(tti.validate("").isValid());
118 tti.setStartTime(startTime);
119 assertTrue(tti.validate("").isValid());
121 tti.setEndTime(null);
122 assertFalse(tti.validate("").isValid());
123 tti.setEndTime(new Date(startTime.getTime() - 1));
124 assertFalse(tti.validate("").isValid());
125 tti.setEndTime(endTime);
126 assertTrue(tti.validate("").isValid());
128 assertThatThrownBy(() -> tti.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");