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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
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;
29 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 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTimeInterval;
38 * DAO test for ToscaTimeInterval.
40 * @author Liam Fallon (liam.fallon@est.tech)
42 public class JpaToscaTimeIntervalTest {
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()));
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());
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());
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());
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());
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());
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());
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());
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());
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());
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);
120 JpaToscaTimeInterval tdtClone0 = new JpaToscaTimeInterval(tti);
121 assertEquals(tti, tdtClone0);
122 assertEquals(0, tti.compareTo(tdtClone0));
124 JpaToscaTimeInterval tdtClone1 = new JpaToscaTimeInterval();
125 tti.copyTo(tdtClone1);
126 assertEquals(tti, tdtClone1);
127 assertEquals(0, tti.compareTo(tdtClone1));
129 assertEquals(-1, tti.compareTo(null));
130 assertEquals(0, tti.compareTo(tti));
131 assertFalse(tti.compareTo(tti.getKey()) == 0);
133 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherTimeInterval");
134 JpaToscaTimeInterval otherDt = new JpaToscaTimeInterval(otherDtKey);
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));
146 fail("test should throw an exception");
147 } catch (Exception exc) {
148 assertEquals("target is marked @NonNull but is null", exc.getMessage());
151 assertEquals(1, tti.getKeys().size());
152 assertEquals(1, new JpaToscaTimeInterval().getKeys().size());
154 new JpaToscaTimeInterval().clean();
156 assertEquals(tdtClone0, tti);
158 assertFalse(new JpaToscaTimeInterval().validate(new PfValidationResult()).isValid());
159 assertTrue(tti.validate(new PfValidationResult()).isValid());
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());
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());
177 fail("test should throw an exception");
178 } catch (Exception exc) {
179 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());