Fix sonar issues for tests in policy-models
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfTimestampKeyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 Nordix Foundation.
4  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
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.assertTrue;
28
29 import java.time.Instant;
30 import java.util.Date;
31 import org.junit.Test;
32
33 public class PfTimestampKeyTest {
34     private static final String VERSION001 = "0.0.1";
35     private static final String CONCEPT_IS_NULL = "^copyConcept is marked .*on.*ull but is null$";
36     private static final String NAME_IS_NULL = "^name is marked .*on.*ull but is null$";
37     private static final String VERSION_IS_NULL = "^version is marked .*on.*ull but is null$";
38     private static final long timeStamp = 1574832537641L;
39
40     @Test
41     public void testTimestampKey() {
42         PfTimestampKey someKey0 = new PfTimestampKey();
43         assertEquals(PfTimestampKey.getNullKey(), someKey0);
44         assertTrue(someKey0.isNullKey());
45         assertEquals("PfTimestampKey(name=NULL, version=0.0.0, timeStamp=" + Date.from(Instant.EPOCH) + ")",
46                 someKey0.toString());
47
48         PfTimestampKey someKey1 = new PfTimestampKey("my-name", VERSION001, Instant.ofEpochSecond(timeStamp));
49         PfTimestampKey someKey2 = new PfTimestampKey(someKey1);
50         PfTimestampKey someKey3 = new PfTimestampKey(someKey1.getId());
51         assertEquals(someKey1, someKey2);
52         assertEquals(someKey1, someKey3);
53         assertFalse(someKey1.isNullVersion());
54         assertEquals("PfTimestampKey(name=my-name, version=0.0.1, timeStamp="
55                 + Date.from(Instant.ofEpochSecond(timeStamp)) + ")", someKey1.toString());
56
57         assertEquals("my-name", someKey1.getName());
58         assertEquals(VERSION001, someKey1.getVersion());
59
60         assertEquals(someKey2, someKey1.getKey());
61         assertEquals(1, someKey1.getKeys().size());
62         assertThatThrownBy(() -> someKey0.setName(null)).isInstanceOf(NullPointerException.class)
63                 .hasMessageMatching(NAME_IS_NULL);
64         assertThatThrownBy(() -> someKey0.setVersion(null)).isInstanceOf(NullPointerException.class)
65                 .hasMessageMatching(VERSION_IS_NULL);
66         assertThatThrownBy(() -> someKey0.setTimeStamp(null)).isInstanceOf(NullPointerException.class)
67                 .hasMessageMatching("^timeStamp is marked .*on.*ull but is null$");
68
69         assertFalse(someKey1.isNewerThan(someKey2));
70         assertThatThrownBy(() -> someKey1.isNewerThan((PfKey) null)).isInstanceOf(NullPointerException.class)
71                 .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
72         someKey2.setTimeStamp(Date.from(Instant.ofEpochSecond(timeStamp).plusMillis(90)));
73         assertTrue(someKey2.isNewerThan(someKey1));
74         someKey3.setName("my-name3");
75         assertTrue(someKey3.isNewerThan(someKey1));
76
77         assertEquals(-1, someKey1.compareTo(someKey2));
78         assertEquals(-1, someKey1.compareTo(someKey3));
79         assertThatThrownBy(() -> someKey1.compareTo((PfConcept) null)).isInstanceOf(NullPointerException.class)
80                 .hasMessageMatching("^otherObj is marked .*on.*ull but is null$");
81
82         PfTimestampKey someKey4 = new PfTimestampKey("NULL", "0.0.0", Instant.ofEpochSecond(timeStamp));
83         assertFalse(someKey4.isNullKey());
84         assertFalse(someKey1.isNullKey());
85     }
86
87     @Test
88     public void testTimestampKeyErrors() {
89         assertThatThrownBy(() -> new PfTimestampKey((PfTimestampKey) null)).isInstanceOf(NullPointerException.class)
90                 .hasMessageMatching(CONCEPT_IS_NULL);
91         assertThatThrownBy(() -> new PfTimestampKey(null, null, null)).isInstanceOf(NullPointerException.class)
92                 .hasMessageMatching(NAME_IS_NULL);
93         assertThatThrownBy(() -> new PfTimestampKey("my-name", null, null)).isInstanceOf(NullPointerException.class)
94                 .hasMessageMatching(VERSION_IS_NULL);
95         assertThatThrownBy(() -> new PfTimestampKey("my-name", VERSION001, null))
96                 .isInstanceOf(NullPointerException.class)
97                 .hasMessageMatching("^instant is marked .*on.*ull but is null$");
98     }
99 }