2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-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.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
28 import java.sql.Timestamp;
29 import java.time.Instant;
30 import org.junit.Test;
32 public class PfTimestampKeyTest {
33 private static final String VERSION001 = "0.0.1";
34 private static final String CONCEPT_IS_NULL = "^copyConcept is marked .*on.*ull but is null$";
35 private static final String NAME_IS_NULL = "^name is marked .*on.*ull but is null$";
36 private static final String VERSION_IS_NULL = "^version is marked .*on.*ull but is null$";
37 private static final long timeStamp = 1574832537641L;
40 public void testTimestampKey() {
41 PfTimestampKey someKey0 = new PfTimestampKey();
42 assertEquals(PfTimestampKey.getNullKey(), someKey0);
43 assertTrue(someKey0.isNullKey());
44 assertEquals("PfTimestampKey(name=NULL, version=0.0.0, timeStamp=" + Timestamp.from(Instant.EPOCH) + ")",
47 PfTimestampKey someKey1 = new PfTimestampKey("my-name", VERSION001, Instant.ofEpochSecond(timeStamp));
48 PfTimestampKey someKey2 = new PfTimestampKey(someKey1);
49 PfTimestampKey someKey3 = new PfTimestampKey(someKey1.getId());
50 assertEquals(someKey1, someKey2);
51 assertEquals(someKey1, someKey3);
52 assertFalse(someKey1.isNullVersion());
53 assertEquals("PfTimestampKey(name=my-name, version=0.0.1, timeStamp="
54 + Timestamp.from(Instant.ofEpochSecond(timeStamp)) + ")", someKey1.toString());
56 assertEquals("my-name", someKey1.getName());
57 assertEquals(VERSION001, someKey1.getVersion());
59 assertEquals(someKey2, someKey1.getKey());
60 assertEquals(1, someKey1.getKeys().size());
62 assertThatThrownBy(() -> new PfTimestampKey((PfTimestampKey) null)).isInstanceOf(NullPointerException.class)
63 .hasMessageMatching(CONCEPT_IS_NULL);
64 assertThatThrownBy(() -> new PfTimestampKey(null, null, null)).isInstanceOf(NullPointerException.class)
65 .hasMessageMatching(NAME_IS_NULL);
66 assertThatThrownBy(() -> new PfTimestampKey("my-name", null, null)).isInstanceOf(NullPointerException.class)
67 .hasMessageMatching(VERSION_IS_NULL);
68 assertThatThrownBy(() -> new PfTimestampKey("my-name", VERSION001, null))
69 .isInstanceOf(NullPointerException.class)
70 .hasMessageMatching("^instant is marked .*on.*ull but is null$");
72 assertThatThrownBy(() -> someKey0.setName(null)).isInstanceOf(NullPointerException.class)
73 .hasMessageMatching(NAME_IS_NULL);
74 assertThatThrownBy(() -> someKey0.setVersion(null)).isInstanceOf(NullPointerException.class)
75 .hasMessageMatching(VERSION_IS_NULL);
76 assertThatThrownBy(() -> someKey0.setTimeStamp(null)).isInstanceOf(NullPointerException.class)
77 .hasMessageMatching("^timeStamp is marked .*on.*ull but is null$");
79 assertFalse(someKey1.isNewerThan(someKey2));
80 assertThatThrownBy(() -> someKey1.isNewerThan((PfKey) null)).isInstanceOf(NullPointerException.class)
81 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
82 someKey2.setTimeStamp(Timestamp.from(Instant.ofEpochSecond(timeStamp).plusMillis(90)));
83 assertTrue(someKey2.isNewerThan(someKey1));
84 someKey3.setName("my-name3");
85 assertTrue(someKey3.isNewerThan(someKey1));
87 assertEquals(-1, someKey1.compareTo(someKey2));
88 assertEquals(-1, someKey1.compareTo(someKey3));
89 assertThatThrownBy(() -> someKey1.compareTo((PfConcept) null)).isInstanceOf(NullPointerException.class)
90 .hasMessageMatching("^otherObj is marked .*on.*ull but is null$");
92 PfTimestampKey someKey4 = new PfTimestampKey("NULL", "0.0.0", Instant.ofEpochSecond(timeStamp));
93 assertFalse(someKey4.isNullKey());
94 assertFalse(someKey1.isNullKey());