Merge "Restructure for authorative models"
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.util.Date;
30
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 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTimeInterval;
36
37 /**
38  * DAO test for ToscaTimeInterval.
39  *
40  * @author Liam Fallon (liam.fallon@est.tech)
41  */
42 public class JpaToscaTimeIntervalTest {
43
44     @Test
45     public void testTimeIntervalPojo() {
46         assertNotNull(new JpaToscaTimeInterval());
47         assertNotNull(new JpaToscaTimeInterval(new PfReferenceKey()));
48         assertNotNull(new JpaToscaTimeInterval(new PfReferenceKey(), new Date(), new Date()));
49         assertNotNull(new JpaToscaTimeInterval(new JpaToscaTimeInterval()));
50
51         try {
52             new JpaToscaTimeInterval((PfReferenceKey) null);
53             fail("test should throw an exception");
54         } catch (Exception exc) {
55             assertEquals("key is marked @NonNull but is null", exc.getMessage());
56         }
57
58         try {
59             new JpaToscaTimeInterval(null, null, null);
60             fail("test should throw an exception");
61         } catch (Exception exc) {
62             assertEquals("key is marked @NonNull but is null", exc.getMessage());
63         }
64
65         try {
66             new JpaToscaTimeInterval(null, null, new Date());
67             fail("test should throw an exception");
68         } catch (Exception exc) {
69             assertEquals("key is marked @NonNull but is null", exc.getMessage());
70         }
71
72         try {
73             new JpaToscaTimeInterval(null, new Date(), null);
74             fail("test should throw an exception");
75         } catch (Exception exc) {
76             assertEquals("key is marked @NonNull but is null", exc.getMessage());
77         }
78
79         try {
80             new JpaToscaTimeInterval(null, new Date(), new Date());
81             fail("test should throw an exception");
82         } catch (Exception exc) {
83             assertEquals("key is marked @NonNull but is null", exc.getMessage());
84         }
85
86         try {
87             new JpaToscaTimeInterval(new PfReferenceKey(), null, null);
88             fail("test should throw an exception");
89         } catch (Exception exc) {
90             assertEquals("startTime is marked @NonNull but is null", exc.getMessage());
91         }
92
93         try {
94             new JpaToscaTimeInterval(new PfReferenceKey(), null, new Date());
95             fail("test should throw an exception");
96         } catch (Exception exc) {
97             assertEquals("startTime is marked @NonNull but is null", exc.getMessage());
98         }
99
100         try {
101             new JpaToscaTimeInterval(new PfReferenceKey(), new Date(), null);
102             fail("test should throw an exception");
103         } catch (Exception exc) {
104             assertEquals("endTime is marked @NonNull but is null", exc.getMessage());
105         }
106
107         try {
108             new JpaToscaTimeInterval((JpaToscaTimeInterval) null);
109             fail("test should throw an exception");
110         } catch (Exception exc) {
111             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
112         }
113
114         PfConceptKey ttiParentKey = new PfConceptKey("tParentKey", "0.0.1");
115         PfReferenceKey ttiKey = new PfReferenceKey(ttiParentKey, "trigger0");
116         Date startTime = new Date(1000);
117         Date endTime = new Date(2000);
118         JpaToscaTimeInterval tti = new JpaToscaTimeInterval(ttiKey, startTime, endTime);
119
120         JpaToscaTimeInterval tdtClone0 = new JpaToscaTimeInterval(tti);
121         assertEquals(tti, tdtClone0);
122         assertEquals(0, tti.compareTo(tdtClone0));
123
124         JpaToscaTimeInterval tdtClone1 = new JpaToscaTimeInterval();
125         tti.copyTo(tdtClone1);
126         assertEquals(tti, tdtClone1);
127         assertEquals(0, tti.compareTo(tdtClone1));
128
129         assertEquals(-1, tti.compareTo(null));
130         assertEquals(0, tti.compareTo(tti));
131         assertFalse(tti.compareTo(tti.getKey()) == 0);
132
133         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherTimeInterval");
134         JpaToscaTimeInterval otherDt = new JpaToscaTimeInterval(otherDtKey);
135
136         assertFalse(tti.compareTo(otherDt) == 0);
137         otherDt.setKey(ttiKey);
138         assertFalse(tti.compareTo(otherDt) == 0);
139         otherDt.setStartTime(startTime);
140         assertFalse(tti.compareTo(otherDt) == 0);
141         otherDt.setEndTime(endTime);
142         assertEquals(0, tti.compareTo(otherDt));
143
144         try {
145             tti.copyTo(null);
146             fail("test should throw an exception");
147         } catch (Exception exc) {
148             assertEquals("target is marked @NonNull but is null", exc.getMessage());
149         }
150
151         assertEquals(1, tti.getKeys().size());
152         assertEquals(1, new JpaToscaTimeInterval().getKeys().size());
153
154         new JpaToscaTimeInterval().clean();
155         tti.clean();
156         assertEquals(tdtClone0, tti);
157
158         assertFalse(new JpaToscaTimeInterval().validate(new PfValidationResult()).isValid());
159         assertTrue(tti.validate(new PfValidationResult()).isValid());
160
161         tti.setStartTime(null);
162         assertFalse(tti.validate(new PfValidationResult()).isValid());
163         tti.setStartTime(new Date(endTime.getTime() + 1));
164         assertFalse(tti.validate(new PfValidationResult()).isValid());
165         tti.setStartTime(startTime);
166         assertTrue(tti.validate(new PfValidationResult()).isValid());
167
168         tti.setEndTime(null);
169         assertFalse(tti.validate(new PfValidationResult()).isValid());
170         tti.setEndTime(new Date(startTime.getTime() - 1));
171         assertFalse(tti.validate(new PfValidationResult()).isValid());
172         tti.setEndTime(endTime);
173         assertTrue(tti.validate(new PfValidationResult()).isValid());
174
175         try {
176             tti.validate(null);
177             fail("test should throw an exception");
178         } catch (Exception exc) {
179             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
180         }
181     }
182 }