Fix Sonar Issues models-tosca-simple
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaTimeIntervalTest.java
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  * ================================================================================
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
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;
30
31 import java.sql.Timestamp;
32 import java.time.Instant;
33 import org.junit.Test;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfReferenceKey;
36
37 /**
38  * DAO test for ToscaTimeInterval.
39  *
40  * @author Liam Fallon (liam.fallon@est.tech)
41  */
42 public class JpaToscaTimeIntervalTest {
43
44     private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
45
46     @Test
47     public void testTimeIntervalPojo() {
48         assertNotNull(new JpaToscaTimeInterval());
49         assertNotNull(new JpaToscaTimeInterval(new PfReferenceKey()));
50         assertNotNull(new JpaToscaTimeInterval(new PfReferenceKey(), Instant.now(), Instant.now()));
51         assertNotNull(new JpaToscaTimeInterval(new JpaToscaTimeInterval()));
52
53         assertThatThrownBy(() -> new JpaToscaTimeInterval((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL);
54
55         assertThatThrownBy(() -> new JpaToscaTimeInterval(null, null, null)).hasMessageMatching(KEY_IS_NULL);
56
57         assertThatThrownBy(() -> new JpaToscaTimeInterval(null, null, Instant.now())).hasMessageMatching(KEY_IS_NULL);
58
59         assertThatThrownBy(() -> new JpaToscaTimeInterval(null, Instant.now(), null)).hasMessageMatching(KEY_IS_NULL);
60
61         assertThatThrownBy(() -> new JpaToscaTimeInterval(null, Instant.now(), Instant.now()))
62                 .hasMessageMatching(KEY_IS_NULL);
63
64         assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), null, null))
65                 .hasMessageMatching("startTime is marked .*on.*ull but is null");
66
67         assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), null, Instant.now()))
68                 .hasMessageMatching("startTime is marked .*on.*ull but is null");
69
70         assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), Instant.now(), null))
71                 .hasMessageMatching("endTime is marked .*on.*ull but is null");
72
73         assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
74                 .isInstanceOf(NullPointerException.class);
75     }
76
77     @Test
78     public void testTimeInterval() {
79         PfConceptKey ttiParentKey = new PfConceptKey("tParentKey", "0.0.1");
80         PfReferenceKey ttiKey = new PfReferenceKey(ttiParentKey, "trigger0");
81         Instant startTime = Instant.ofEpochSecond(1000);
82         Instant endTime = Instant.ofEpochSecond(2000);
83         JpaToscaTimeInterval tti = new JpaToscaTimeInterval(ttiKey, startTime, endTime);
84
85         JpaToscaTimeInterval tdtClone0 = new JpaToscaTimeInterval(tti);
86         assertEquals(tti, tdtClone0);
87         assertEquals(0, tti.compareTo(tdtClone0));
88
89         JpaToscaTimeInterval tdtClone1 = new JpaToscaTimeInterval(tti);
90         assertEquals(tti, tdtClone1);
91         assertEquals(0, tti.compareTo(tdtClone1));
92
93         assertEquals(-1, tti.compareTo(null));
94         assertEquals(0, tti.compareTo(tti));
95         assertNotEquals(0, tti.compareTo(tti.getKey()));
96
97         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherTimeInterval");
98         JpaToscaTimeInterval otherDt = new JpaToscaTimeInterval(otherDtKey);
99
100         assertNotEquals(0, tti.compareTo(otherDt));
101         otherDt.setKey(ttiKey);
102         assertNotEquals(0, tti.compareTo(otherDt));
103         otherDt.setStartTime(Timestamp.from(startTime));
104         assertNotEquals(0, tti.compareTo(otherDt));
105         otherDt.setEndTime(Timestamp.from(endTime));
106         assertEquals(0, tti.compareTo(otherDt));
107
108         assertEquals(1, tti.getKeys().size());
109         assertEquals(1, new JpaToscaTimeInterval().getKeys().size());
110
111         new JpaToscaTimeInterval().clean();
112         tti.clean();
113         assertEquals(tdtClone0, tti);
114     }
115
116     @Test
117     public void testTimeIntervalValidation() {
118         Instant startTime = Instant.ofEpochSecond(1000);
119         Instant endTime = Instant.ofEpochSecond(2000);
120         JpaToscaTimeInterval tti = setUpJpaToscaTimeInterval(startTime, endTime);
121         assertFalse(new JpaToscaTimeInterval().validate("").isValid());
122         assertTrue(tti.validate("").isValid());
123
124         tti.setStartTime(null);
125         assertFalse(tti.validate("").isValid());
126
127         tti.setStartTime(Timestamp.from(endTime.plusSeconds(1)));
128         assertFalse(tti.validate("").isValid());
129         tti.setStartTime(Timestamp.from(startTime));
130         assertTrue(tti.validate("").isValid());
131
132         tti.setEndTime(null);
133         assertFalse(tti.validate("").isValid());
134         tti.setEndTime(Timestamp.from(startTime.minusSeconds(1)));
135         assertFalse(tti.validate("").isValid());
136         tti.setEndTime(Timestamp.from(endTime));
137         assertTrue(tti.validate("").isValid());
138
139         assertThatThrownBy(() -> tti.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
140     }
141
142     private JpaToscaTimeInterval setUpJpaToscaTimeInterval(Instant startTime, Instant endTime) {
143         PfConceptKey ttiParentKey = new PfConceptKey("tParentKey", "0.0.1");
144         PfReferenceKey ttiKey = new PfReferenceKey(ttiParentKey, "trigger0");
145         JpaToscaTimeInterval tti = new JpaToscaTimeInterval(ttiKey, startTime, endTime);
146
147         return tti;
148     }
149 }