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