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=========================================================
22 package org.onap.policy.models.base;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertTrue;
30 import org.junit.Test;
32 public class PfGeneratedIdKeyTest {
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 generatedId = 10001L;
40 public void testGeneratedIdKey() {
41 PfGeneratedIdKey someKey0 = new PfGeneratedIdKey();
42 assertEquals(PfGeneratedIdKey.getNullKey(), someKey0);
43 assertTrue(someKey0.isNullKey());
44 assertEquals("PfGeneratedIdKey(name=NULL, version=0.0.0, generatedId=null)",
47 PfGeneratedIdKey someKey1 = new PfGeneratedIdKey("my-name", VERSION001, generatedId);
48 PfGeneratedIdKey someKey2 = new PfGeneratedIdKey(someKey1);
49 PfGeneratedIdKey someKey3 = new PfGeneratedIdKey(someKey1.getId());
50 assertEquals(someKey1, someKey2);
51 assertEquals(someKey1, someKey3);
52 assertFalse(someKey1.isNullVersion());
53 assertEquals("PfGeneratedIdKey(name=my-name, version=0.0.1, generatedId="
54 + generatedId + ")", someKey1.toString());
56 assertEquals("my-name", someKey1.getName());
57 assertEquals(VERSION001, someKey1.getVersion());
59 assertEquals(someKey2, someKey1.getKey());
60 assertEquals(1, someKey1.getKeys().size());
61 assertThatThrownBy(() -> someKey0.setName(null)).isInstanceOf(NullPointerException.class)
62 .hasMessageMatching(NAME_IS_NULL);
63 assertThatThrownBy(() -> someKey0.setVersion(null)).isInstanceOf(NullPointerException.class)
64 .hasMessageMatching(VERSION_IS_NULL);
65 assertThatCode(() -> someKey0.setGeneratedId(null)).doesNotThrowAnyException();
67 assertFalse(someKey1.isNewerThan(someKey2));
68 assertThatThrownBy(() -> someKey1.isNewerThan((PfKey) null)).isInstanceOf(NullPointerException.class)
69 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
70 someKey2.setGeneratedId(generatedId + 1);
71 assertTrue(someKey2.isNewerThan(someKey1));
72 someKey3.setName("my-name3");
73 assertTrue(someKey3.isNewerThan(someKey1));
75 assertEquals(-1, someKey1.compareTo(someKey2));
76 assertEquals(-1, someKey1.compareTo(someKey3));
77 assertThatThrownBy(() -> someKey1.compareTo((PfConcept) null)).isInstanceOf(NullPointerException.class)
78 .hasMessageMatching("^otherObj is marked .*on.*ull but is null$");
80 PfGeneratedIdKey someKey4 = new PfGeneratedIdKey("NULL", "0.0.0", generatedId);
81 assertFalse(someKey4.isNullKey());
82 assertFalse(someKey1.isNullKey());
86 public void testTimestampKeyErrors() {
87 assertThatThrownBy(() -> new PfGeneratedIdKey((PfGeneratedIdKey) null)).isInstanceOf(NullPointerException.class)
88 .hasMessageMatching(CONCEPT_IS_NULL);
89 assertThatThrownBy(() -> new PfGeneratedIdKey(null, null, null)).isInstanceOf(NullPointerException.class)
90 .hasMessageMatching(NAME_IS_NULL);
91 assertThatThrownBy(() -> new PfGeneratedIdKey("my-name", null, null)).isInstanceOf(NullPointerException.class)
92 .hasMessageMatching(VERSION_IS_NULL);
93 assertThatCode(() -> new PfGeneratedIdKey("my-name", VERSION001, null))
94 .doesNotThrowAnyException();