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
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.base;
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;
31 import java.sql.Timestamp;
32 import java.time.Instant;
33 import org.junit.Test;
35 public class PfReferenceTimestampKeyTest {
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;
45 public void testPfReferenceTimestampKeyConstruct() {
46 assertThat(new PfReferenceTimestampKey().getReferenceKey().getLocalName()).isEqualTo(PfKey.NULL_KEY_NAME);
47 assertEquals(PfKey.NULL_KEY_NAME, new PfReferenceTimestampKey(new PfConceptKey()).getReferenceKey()
49 assertNotNull(new PfReferenceTimestampKey(new PfReferenceTimestampKey()).getTimeStamp());
51 assertEquals(LOCAL_NAME,
52 new PfReferenceTimestampKey(new PfReferenceKey(), LOCAL_NAME, Instant.ofEpochSecond(timeStamp))
53 .getReferenceKey().getLocalName());
54 assertEquals(Timestamp.from(Instant.ofEpochSecond(timeStamp)), new PfReferenceTimestampKey(new PfConceptKey(),
55 PARENT_LOCAL_NAME, LOCAL_NAME, Instant.ofEpochSecond(timeStamp)).getTimeStamp());
57 assertThat(new PfReferenceTimestampKey("ParentKeyName", VERSION001, PARENT_LOCAL_NAME, LOCAL_NAME,
58 Instant.ofEpochSecond(timeStamp))).isInstanceOf(PfReferenceTimestampKey.class);
61 new PfReferenceTimestampKey("ParentKeyName", VERSION001, LOCAL_NAME, Instant.ofEpochSecond(timeStamp))
62 .getReferenceKey().getParentLocalName()).isEqualTo(PfKey.NULL_KEY_NAME);
64 assertEquals(PfReferenceTimestampKey.getNullKey().getKey(), PfReferenceTimestampKey.getNullKey());
65 assertEquals("NULL:0.0.0:NULL:NULL:" + Instant.EPOCH.getEpochSecond(),
66 PfReferenceTimestampKey.getNullKey().getId());
68 assertThatThrownBy(() -> new PfReferenceTimestampKey(new PfConceptKey(), null, null))
69 .hasMessage("parameter \"localName\" is null");
71 PfReferenceTimestampKey testNullKey = new PfReferenceTimestampKey();
72 assertTrue(testNullKey.isNullKey());
74 String id = "NULL:0.0.0:NULL:NULL:" + timeStamp;
75 assertThat(new PfReferenceTimestampKey(id).getTimeStamp().getTime()).isEqualTo(timeStamp);
79 public void testPfReferenceTimestampKey() {
80 PfReferenceTimestampKey testReferenceKey = new PfReferenceTimestampKey();
81 testReferenceKey.setReferenceKey(new PfReferenceKey(new PfConceptKey("PN", VERSION001)));
82 assertEquals("PN:0.0.1", testReferenceKey.getReferenceKey().getParentConceptKey().getId());
84 assertEquals(1, testReferenceKey.getKeys().size());
85 assertFalse(testReferenceKey.isNullKey());
87 testReferenceKey.setReferenceKey(new PfReferenceKey("PN", VERSION001, "LN"));
88 assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getReferenceKey().getId());
90 testReferenceKey.getReferenceKey().setParentKeyName("PKN");
91 assertEquals("PKN", testReferenceKey.getReferenceKey().getParentKeyName());
93 testReferenceKey.getReferenceKey().setParentKeyVersion(VERSION001);
94 assertEquals(VERSION001, testReferenceKey.getReferenceKey().getParentKeyVersion());
96 testReferenceKey.getReferenceKey().setParentLocalName(PARENT_LOCAL_NAME);
97 assertEquals(PARENT_LOCAL_NAME, testReferenceKey.getReferenceKey().getParentLocalName());
99 testReferenceKey.getReferenceKey().setLocalName("LN");
100 assertEquals("LN", testReferenceKey.getReferenceKey().getLocalName());
102 testReferenceKey.setTimeStamp(Timestamp.from(DEFAULT_TIMESTAMP));
103 assertEquals(Timestamp.from(DEFAULT_TIMESTAMP), testReferenceKey.getTimeStamp());
106 assertThatThrownBy(() -> testReferenceKey.isCompatible(null))
107 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
109 assertFalse(testReferenceKey.isCompatible(PfConceptKey.getNullKey()));
110 assertFalse(testReferenceKey.isCompatible(PfReferenceKey.getNullKey()));
111 assertTrue(testReferenceKey.isCompatible(testReferenceKey));
113 assertTrue(testReferenceKey.validate("").isValid());
115 testReferenceKey.clean();
117 PfReferenceTimestampKey clonedReferenceKey = new PfReferenceTimestampKey(testReferenceKey);
119 assertEquals("PfReferenceTimestampKey(timeStamp=" + Timestamp.from(Instant.EPOCH) + ","
120 + " referenceKey=PfReferenceKey(parentKeyName=PKN, parentKeyVersion=0.0.1, "
121 + "parentLocalName=ParentLocalName, localName=LN))", clonedReferenceKey.toString());
123 assertNotEquals(0, testReferenceKey.hashCode());
125 assertEquals(testReferenceKey, clonedReferenceKey);
126 assertNotEquals(testReferenceKey, new PfReferenceTimestampKey("PKN", VERSION001, "PLN",
127 "LN", Instant.ofEpochSecond(timeStamp)));
128 testReferenceKey.setTimeStamp(Timestamp.from(Instant.ofEpochSecond(timeStamp)));
129 assertEquals(testReferenceKey, new PfReferenceTimestampKey("PKN", VERSION001, PARENT_LOCAL_NAME, "LN",
130 Instant.ofEpochSecond(timeStamp)));
132 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceTimestampKey()));
133 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceTimestampKey("PKN", VERSION002,
134 "PLN", "LN", Instant.ofEpochSecond(timeStamp))));
136 assertEquals(0, testReferenceKey.compareTo(new PfReferenceTimestampKey("PKN", VERSION001, PARENT_LOCAL_NAME,
137 "LN", Instant.ofEpochSecond(timeStamp))));
139 assertThatThrownBy(() -> new PfReferenceTimestampKey((PfReferenceTimestampKey) null))
140 .isInstanceOf(NullPointerException.class);
142 assertEquals(testReferenceKey, new PfReferenceTimestampKey(testReferenceKey));
147 public void testNewerKey() {
148 PfReferenceTimestampKey key1 = new PfReferenceTimestampKey("ParentKeyName", VERSION001, PARENT_LOCAL_NAME,
149 LOCAL_NAME, Instant.ofEpochSecond(timeStamp));
150 PfReferenceTimestampKey key2 = new PfReferenceTimestampKey(key1);
151 assertFalse(key2.isNewerThan(key1));
152 assertThatThrownBy(() -> key1.isNewerThan((PfKey) null)).isInstanceOf(NullPointerException.class)
153 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
155 key2.setTimeStamp(Timestamp.from(Instant.ofEpochSecond(timeStamp).plusSeconds(80)));
156 assertTrue(key2.isNewerThan(key1));