Refactor timestamp property in policy models to use Instant
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfReferenceTimestampKeyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 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.base;
22
23 import static org.assertj.core.api.Assertions.assertThat;
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
35 public class PfReferenceTimestampKeyTest {
36
37     private static final String PARENT_LOCAL_NAME = "ParentLocalName";
38     private static final String LOCAL_NAME = "LocalName";
39     private static final String VERSION002 = "0.0.2";
40     private static final String VERSION001 = "0.0.1";
41     private static final long timeStamp = 1613152081L;
42     private static final Instant DEFAULT_TIMESTAMP = Instant.EPOCH;
43
44     @Test
45     public void testPfReferenceTimestampKeyConstruct() {
46         assertThat(new PfReferenceTimestampKey().getLocalName()).isEqualTo(PfKey.NULL_KEY_NAME);
47         assertEquals(PfKey.NULL_KEY_NAME, new PfReferenceTimestampKey(new PfConceptKey()).getParentKeyName());
48         assertNotNull(new PfReferenceTimestampKey(new PfReferenceTimestampKey()).getTimeStamp());
49
50         assertEquals(LOCAL_NAME,
51                 new PfReferenceTimestampKey(new PfReferenceKey(), LOCAL_NAME, Instant.ofEpochSecond(timeStamp))
52                         .getLocalName());
53         assertEquals(Timestamp.from(Instant.ofEpochSecond(timeStamp)), new PfReferenceTimestampKey(new PfConceptKey(),
54                 PARENT_LOCAL_NAME, LOCAL_NAME, Instant.ofEpochSecond(timeStamp)).getTimeStamp());
55
56         assertThat(new PfReferenceTimestampKey("ParentKeyName", VERSION001, PARENT_LOCAL_NAME, LOCAL_NAME,
57                 Instant.ofEpochSecond(timeStamp))).isInstanceOf(PfReferenceTimestampKey.class);
58
59         assertThat(
60                 new PfReferenceTimestampKey("ParentKeyName", VERSION001, LOCAL_NAME, Instant.ofEpochSecond(timeStamp))
61                         .getParentLocalName()).isEqualTo(PfKey.NULL_KEY_NAME);
62
63         assertEquals(PfReferenceTimestampKey.getNullKey().getKey(), PfReferenceTimestampKey.getNullKey());
64         assertEquals("NULL:0.0.0:NULL:NULL:" + Instant.EPOCH.getEpochSecond(),
65                 PfReferenceTimestampKey.getNullKey().getId());
66
67         assertThatThrownBy(() -> new PfReferenceTimestampKey(new PfConceptKey(), null, null))
68                 .hasMessage("parameter \"localName\" is null");
69
70         String id = "NULL:0.0.0:NULL:NULL:" + timeStamp;
71         assertThat(new PfReferenceTimestampKey(id).getTimeStamp().getTime()).isEqualTo(timeStamp);
72
73     }
74
75     @Test
76     public void testPfReferenceTimestampKey() {
77         PfReferenceTimestampKey testReferenceKey = new PfReferenceTimestampKey();
78         testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
79         assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
80
81         assertEquals(1, testReferenceKey.getKeys().size());
82         assertFalse(testReferenceKey.isNullKey());
83
84         testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", VERSION001, "LN"));
85         assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId());
86
87         testReferenceKey.setParentKeyName("PKN");
88         assertEquals("PKN", testReferenceKey.getParentKeyName());
89
90         testReferenceKey.setParentKeyVersion(VERSION001);
91         assertEquals(VERSION001, testReferenceKey.getParentKeyVersion());
92
93         testReferenceKey.setParentLocalName(PARENT_LOCAL_NAME);
94         assertEquals(PARENT_LOCAL_NAME, testReferenceKey.getParentLocalName());
95
96         testReferenceKey.setLocalName("LN");
97         assertEquals("LN", testReferenceKey.getLocalName());
98
99         testReferenceKey.setTimeStamp(Timestamp.from(DEFAULT_TIMESTAMP));
100         assertEquals(Timestamp.from(DEFAULT_TIMESTAMP), testReferenceKey.getTimeStamp());
101
102
103         assertThatThrownBy(() -> testReferenceKey.isCompatible(null))
104                 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
105
106         assertFalse(testReferenceKey.isCompatible(PfConceptKey.getNullKey()));
107         assertFalse(testReferenceKey.isCompatible(PfReferenceKey.getNullKey()));
108         assertTrue(testReferenceKey.isCompatible(testReferenceKey));
109
110         assertTrue(testReferenceKey.validate("").isValid());
111
112         testReferenceKey.clean();
113
114         PfReferenceTimestampKey clonedReferenceKey = new PfReferenceTimestampKey(testReferenceKey);
115
116         assertEquals("PfReferenceTimestampKey(super=PfReferenceKey(parentKeyName=PKN, parentKeyVersion=0.0.1, "
117                 + "parentLocalName=ParentLocalName, localName=LN), timeStamp=" + Timestamp.from(Instant.EPOCH) + ")",
118                 clonedReferenceKey.toString());
119
120         assertNotEquals(0, testReferenceKey.hashCode());
121
122         assertEquals(testReferenceKey, clonedReferenceKey);
123         assertNotEquals(testReferenceKey,
124                 new PfReferenceTimestampKey("PKN", VERSION001, "PLN", "LN", Instant.ofEpochSecond(timeStamp)));
125         testReferenceKey.setTimeStamp(Timestamp.from(Instant.ofEpochSecond(timeStamp)));
126         assertEquals(testReferenceKey, new PfReferenceTimestampKey("PKN", VERSION001, PARENT_LOCAL_NAME, "LN",
127                 Instant.ofEpochSecond(timeStamp)));
128
129         assertNotEquals(0, testReferenceKey.compareTo(new PfConceptKey()));
130         assertNotEquals(0, testReferenceKey.compareTo(
131                 new PfReferenceTimestampKey("PKN", VERSION002, "PLN", "LN", Instant.ofEpochSecond(timeStamp))));
132         assertEquals(0, testReferenceKey.compareTo(new PfReferenceTimestampKey("PKN", VERSION001, PARENT_LOCAL_NAME,
133                 "LN", Instant.ofEpochSecond(timeStamp))));
134
135         assertThatThrownBy(() -> new PfReferenceTimestampKey((PfReferenceTimestampKey) null))
136                 .isInstanceOf(NullPointerException.class);
137
138         assertEquals(testReferenceKey, new PfReferenceTimestampKey(testReferenceKey));
139
140     }
141
142     @Test
143     public void testNewerKey() {
144         PfReferenceTimestampKey key1 = new PfReferenceTimestampKey("ParentKeyName", VERSION001, PARENT_LOCAL_NAME,
145                 LOCAL_NAME, Instant.ofEpochSecond(timeStamp));
146         PfReferenceTimestampKey key2 = new PfReferenceTimestampKey(key1);
147         assertFalse(key2.isNewerThan(key1));
148         assertThatThrownBy(() -> key1.isNewerThan((PfKey) null)).isInstanceOf(NullPointerException.class)
149                 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
150
151         key2.setTimeStamp(Timestamp.from(Instant.ofEpochSecond(timeStamp).plusSeconds(80)));
152         assertTrue(key2.isNewerThan(key1));
153     }
154
155 }