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.time.Instant;
32 import org.junit.Test;
34 public class PfReferenceTimestampKeyTest {
36 private static final String PARENT_LOCAL_NAME = "ParentLocalName";
37 private static final String LOCAL_NAME = "LocalName";
38 private static final String VERSION002 = "0.0.2";
39 private static final String VERSION001 = "0.0.1";
40 private static final long timeStamp = 1613152081L;
41 private static final Instant DEFAULT_TIMESTAMP = Instant.EPOCH;
44 public void testPfReferenceTimestampKeyConstruct() {
45 assertThat(new PfReferenceTimestampKey().getLocalName()).isEqualTo(PfKey.NULL_KEY_NAME);
46 assertEquals(PfKey.NULL_KEY_NAME, new PfReferenceTimestampKey(new PfConceptKey()).getParentKeyName());
47 assertNotNull(new PfReferenceTimestampKey(new PfReferenceTimestampKey()).getTimeStamp());
49 assertEquals(LOCAL_NAME, new PfReferenceTimestampKey(new PfReferenceKey(), LOCAL_NAME,
50 Instant.ofEpochSecond(timeStamp)).getLocalName());
51 assertEquals(Instant.ofEpochSecond(timeStamp), new PfReferenceTimestampKey(new PfConceptKey(),
52 PARENT_LOCAL_NAME, LOCAL_NAME, Instant.ofEpochSecond(timeStamp)).getTimeStamp());
54 assertThat(new PfReferenceTimestampKey("ParentKeyName", VERSION001, PARENT_LOCAL_NAME,
55 LOCAL_NAME, Instant.ofEpochSecond(timeStamp))).isInstanceOf(PfReferenceTimestampKey.class);
57 assertThat(new PfReferenceTimestampKey("ParentKeyName", VERSION001, LOCAL_NAME,
58 Instant.ofEpochSecond(timeStamp)).getParentLocalName()).isEqualTo(PfKey.NULL_KEY_NAME);
60 assertEquals(PfReferenceTimestampKey.getNullKey().getKey(), PfReferenceTimestampKey.getNullKey());
61 assertEquals("NULL:0.0.0:NULL:NULL:" + Instant.EPOCH.getEpochSecond(),
62 PfReferenceTimestampKey.getNullKey().getId());
64 assertThatThrownBy(() -> new PfReferenceTimestampKey(new PfConceptKey(), null, null))
65 .hasMessage("parameter \"localName\" is null");
67 String id = "NULL:0.0.0:NULL:NULL:" + timeStamp;
68 assertThat(new PfReferenceTimestampKey(id).getTimeStamp().getEpochSecond()).isEqualTo(timeStamp);
73 public void testPfReferenceTimestampKey() {
74 PfReferenceTimestampKey testReferenceKey = new PfReferenceTimestampKey();
75 testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
76 assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
78 assertEquals(1, testReferenceKey.getKeys().size());
79 assertFalse(testReferenceKey.isNullKey());
81 testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", VERSION001,
83 assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId());
85 testReferenceKey.setParentKeyName("PKN");
86 assertEquals("PKN", testReferenceKey.getParentKeyName());
88 testReferenceKey.setParentKeyVersion(VERSION001);
89 assertEquals(VERSION001, testReferenceKey.getParentKeyVersion());
91 testReferenceKey.setParentLocalName(PARENT_LOCAL_NAME);
92 assertEquals(PARENT_LOCAL_NAME, testReferenceKey.getParentLocalName());
94 testReferenceKey.setLocalName("LN");
95 assertEquals("LN", testReferenceKey.getLocalName());
97 testReferenceKey.setTimeStamp(DEFAULT_TIMESTAMP);
98 assertEquals(DEFAULT_TIMESTAMP, testReferenceKey.getTimeStamp());
101 assertThatThrownBy(() -> testReferenceKey.isCompatible(null))
102 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
104 assertFalse(testReferenceKey.isCompatible(PfConceptKey.getNullKey()));
105 assertFalse(testReferenceKey.isCompatible(PfReferenceKey.getNullKey()));
106 assertTrue(testReferenceKey.isCompatible(testReferenceKey));
108 assertTrue(testReferenceKey.validate("").isValid());
110 testReferenceKey.clean();
112 PfReferenceTimestampKey clonedReferenceKey = new PfReferenceTimestampKey(testReferenceKey);
114 assertEquals("PfReferenceTimestampKey(super=PfReferenceKey(parentKeyName=PKN, parentKeyVersion=0.0.1, "
115 + "parentLocalName=ParentLocalName, localName=LN), timeStamp=" + Instant.EPOCH + ")",
116 clonedReferenceKey.toString());
118 assertNotEquals(0, testReferenceKey.hashCode());
120 assertEquals(testReferenceKey, clonedReferenceKey);
121 assertNotEquals(testReferenceKey, new PfReferenceTimestampKey("PKN", VERSION001,
122 "PLN", "LN", Instant.ofEpochSecond(timeStamp)));
123 testReferenceKey.setTimeStamp(Instant.ofEpochSecond(timeStamp));
124 assertEquals(testReferenceKey, new PfReferenceTimestampKey("PKN", VERSION001,
125 PARENT_LOCAL_NAME, "LN", Instant.ofEpochSecond(timeStamp)));
127 assertNotEquals(0, testReferenceKey.compareTo(new PfConceptKey()));
128 assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceTimestampKey("PKN",
129 VERSION002, "PLN", "LN", Instant.ofEpochSecond(timeStamp))));
130 assertEquals(0, testReferenceKey.compareTo(new PfReferenceTimestampKey("PKN",
131 VERSION001, PARENT_LOCAL_NAME, "LN", Instant.ofEpochSecond(timeStamp))));
133 assertThatThrownBy(() -> new PfReferenceTimestampKey((PfReferenceTimestampKey) null))
134 .isInstanceOf(NullPointerException.class);
136 assertEquals(testReferenceKey, new PfReferenceTimestampKey(testReferenceKey));
141 public void testNewerKey() {
142 PfReferenceTimestampKey key1 = new PfReferenceTimestampKey("ParentKeyName", VERSION001,
143 PARENT_LOCAL_NAME, LOCAL_NAME, Instant.ofEpochSecond(timeStamp));
144 PfReferenceTimestampKey key2 = new PfReferenceTimestampKey(key1);
145 assertFalse(key2.isNewerThan(key1));
146 assertThatThrownBy(() -> key1.isNewerThan((PfKey) null)).isInstanceOf(NullPointerException.class)
147 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
149 key2.setTimeStamp(Instant.ofEpochSecond(timeStamp).plusSeconds(80));
150 assertTrue(key2.isNewerThan(key1));