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().getLocalName()).isEqualTo(PfKey.NULL_KEY_NAME);
47 assertEquals(PfKey.NULL_KEY_NAME, new PfReferenceTimestampKey(new PfConceptKey()).getParentKeyName());
48 assertNotNull(new PfReferenceTimestampKey(new PfReferenceTimestampKey()).getTimeStamp());
50 assertEquals(LOCAL_NAME,
51 new PfReferenceTimestampKey(new PfReferenceKey(), LOCAL_NAME, Instant.ofEpochSecond(timeStamp))
53 assertEquals(Timestamp.from(Instant.ofEpochSecond(timeStamp)), new PfReferenceTimestampKey(new PfConceptKey(),
54 PARENT_LOCAL_NAME, LOCAL_NAME, Instant.ofEpochSecond(timeStamp)).getTimeStamp());
56 assertThat(new PfReferenceTimestampKey("ParentKeyName", VERSION001, PARENT_LOCAL_NAME, LOCAL_NAME,
57 Instant.ofEpochSecond(timeStamp))).isInstanceOf(PfReferenceTimestampKey.class);
60 new PfReferenceTimestampKey("ParentKeyName", VERSION001, LOCAL_NAME, Instant.ofEpochSecond(timeStamp))
61 .getParentLocalName()).isEqualTo(PfKey.NULL_KEY_NAME);
63 assertEquals(PfReferenceTimestampKey.getNullKey().getKey(), PfReferenceTimestampKey.getNullKey());
64 assertEquals("NULL:0.0.0:NULL:NULL:" + Instant.EPOCH.getEpochSecond(),
65 PfReferenceTimestampKey.getNullKey().getId());
67 assertThatThrownBy(() -> new PfReferenceTimestampKey(new PfConceptKey(), null, null))
68 .hasMessage("parameter \"localName\" is null");
70 String id = "NULL:0.0.0:NULL:NULL:" + timeStamp;
71 assertThat(new PfReferenceTimestampKey(id).getTimeStamp().getTime()).isEqualTo(timeStamp);
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());
81 assertEquals(1, testReferenceKey.getKeys().size());
82 assertFalse(testReferenceKey.isNullKey());
84 testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", VERSION001, "LN"));
85 assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId());
87 testReferenceKey.setParentKeyName("PKN");
88 assertEquals("PKN", testReferenceKey.getParentKeyName());
90 testReferenceKey.setParentKeyVersion(VERSION001);
91 assertEquals(VERSION001, testReferenceKey.getParentKeyVersion());
93 testReferenceKey.setParentLocalName(PARENT_LOCAL_NAME);
94 assertEquals(PARENT_LOCAL_NAME, testReferenceKey.getParentLocalName());
96 testReferenceKey.setLocalName("LN");
97 assertEquals("LN", testReferenceKey.getLocalName());
99 testReferenceKey.setTimeStamp(Timestamp.from(DEFAULT_TIMESTAMP));
100 assertEquals(Timestamp.from(DEFAULT_TIMESTAMP), testReferenceKey.getTimeStamp());
103 assertThatThrownBy(() -> testReferenceKey.isCompatible(null))
104 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
106 assertFalse(testReferenceKey.isCompatible(PfConceptKey.getNullKey()));
107 assertFalse(testReferenceKey.isCompatible(PfReferenceKey.getNullKey()));
108 assertTrue(testReferenceKey.isCompatible(testReferenceKey));
110 assertTrue(testReferenceKey.validate("").isValid());
112 testReferenceKey.clean();
114 PfReferenceTimestampKey clonedReferenceKey = new PfReferenceTimestampKey(testReferenceKey);
116 assertEquals("PfReferenceTimestampKey(super=PfReferenceKey(parentKeyName=PKN, parentKeyVersion=0.0.1, "
117 + "parentLocalName=ParentLocalName, localName=LN), timeStamp=" + Timestamp.from(Instant.EPOCH) + ")",
118 clonedReferenceKey.toString());
120 assertNotEquals(0, testReferenceKey.hashCode());
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)));
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))));
135 assertThatThrownBy(() -> new PfReferenceTimestampKey((PfReferenceTimestampKey) null))
136 .isInstanceOf(NullPointerException.class);
138 assertEquals(testReferenceKey, new PfReferenceTimestampKey(testReferenceKey));
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$");
151 key2.setTimeStamp(Timestamp.from(Instant.ofEpochSecond(timeStamp).plusSeconds(80)));
152 assertTrue(key2.isNewerThan(key1));