Replace copyTo methods with copy constructors
[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 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 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.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.Date;
31 import org.junit.Test;
32 import org.onap.policy.models.base.PfConceptKey;
33 import org.onap.policy.models.base.PfReferenceKey;
34 import org.onap.policy.models.base.PfValidationResult;
35
36 /**
37  * DAO test for ToscaTimeInterval.
38  *
39  * @author Liam Fallon (liam.fallon@est.tech)
40  */
41 public class JpaToscaTimeIntervalTest {
42
43     private static final String KEY_IS_NULL = "key is marked @NonNull but is null";
44
45     @Test
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()));
51
52         assertThatThrownBy(() -> new JpaToscaTimeInterval((PfReferenceKey) null)).hasMessage(KEY_IS_NULL);
53
54         assertThatThrownBy(() -> new JpaToscaTimeInterval(null, null, null)).hasMessage(KEY_IS_NULL);
55
56         assertThatThrownBy(() -> new JpaToscaTimeInterval(null, null, new Date())).hasMessage(KEY_IS_NULL);
57
58         assertThatThrownBy(() -> new JpaToscaTimeInterval(null, new Date(), null)).hasMessage(KEY_IS_NULL);
59
60         assertThatThrownBy(() -> new JpaToscaTimeInterval(null, new Date(), new Date())).hasMessage(KEY_IS_NULL);
61
62         assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), null, null))
63                         .hasMessage("startTime is marked @NonNull but is null");
64
65         assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), null, new Date()))
66                         .hasMessage("startTime is marked @NonNull but is null");
67
68         assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), new Date(), null))
69                         .hasMessage("endTime is marked @NonNull but is null");
70
71         assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
72                         .isInstanceOf(NullPointerException.class);
73
74         PfConceptKey ttiParentKey = new PfConceptKey("tParentKey", "0.0.1");
75         PfReferenceKey ttiKey = new PfReferenceKey(ttiParentKey, "trigger0");
76         Date startTime = new Date(1000);
77         Date endTime = new Date(2000);
78         JpaToscaTimeInterval tti = new JpaToscaTimeInterval(ttiKey, startTime, endTime);
79
80         JpaToscaTimeInterval tdtClone0 = new JpaToscaTimeInterval(tti);
81         assertEquals(tti, tdtClone0);
82         assertEquals(0, tti.compareTo(tdtClone0));
83
84         JpaToscaTimeInterval tdtClone1 = new JpaToscaTimeInterval(tti);
85         assertEquals(tti, tdtClone1);
86         assertEquals(0, tti.compareTo(tdtClone1));
87
88         assertEquals(-1, tti.compareTo(null));
89         assertEquals(0, tti.compareTo(tti));
90         assertFalse(tti.compareTo(tti.getKey()) == 0);
91
92         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherTimeInterval");
93         JpaToscaTimeInterval otherDt = new JpaToscaTimeInterval(otherDtKey);
94
95         assertFalse(tti.compareTo(otherDt) == 0);
96         otherDt.setKey(ttiKey);
97         assertFalse(tti.compareTo(otherDt) == 0);
98         otherDt.setStartTime(startTime);
99         assertFalse(tti.compareTo(otherDt) == 0);
100         otherDt.setEndTime(endTime);
101         assertEquals(0, tti.compareTo(otherDt));
102
103         assertEquals(1, tti.getKeys().size());
104         assertEquals(1, new JpaToscaTimeInterval().getKeys().size());
105
106         new JpaToscaTimeInterval().clean();
107         tti.clean();
108         assertEquals(tdtClone0, tti);
109
110         assertFalse(new JpaToscaTimeInterval().validate(new PfValidationResult()).isValid());
111         assertTrue(tti.validate(new PfValidationResult()).isValid());
112
113         tti.setStartTime(null);
114         assertFalse(tti.validate(new PfValidationResult()).isValid());
115         tti.setStartTime(new Date(endTime.getTime() + 1));
116         assertFalse(tti.validate(new PfValidationResult()).isValid());
117         tti.setStartTime(startTime);
118         assertTrue(tti.validate(new PfValidationResult()).isValid());
119
120         tti.setEndTime(null);
121         assertFalse(tti.validate(new PfValidationResult()).isValid());
122         tti.setEndTime(new Date(startTime.getTime() - 1));
123         assertFalse(tti.validate(new PfValidationResult()).isValid());
124         tti.setEndTime(endTime);
125         assertTrue(tti.validate(new PfValidationResult()).isValid());
126
127         assertThatThrownBy(() -> tti.validate(null)).hasMessage("resultIn is marked @NonNull but is null");
128     }
129 }