Consistent returns on Service Template gets
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfSearchableKeyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 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 org.junit.Test;
30 import org.onap.policy.models.base.testconcepts.DummyPfConcept;
31
32 public class PfSearchableKeyTest {
33
34     private static final String VERSION001 = "0.0.1";
35     private static final String ID_IS_NULL = "^id is marked .*on.*ull but is null$";
36
37     @Test
38     public void testSearchableKey() {
39         PfSearchableKey someKey0 = new PfSearchableKey();
40         assertEquals(PfSearchableKey.getNullKey(), someKey0);
41         assertTrue(someKey0.isNullKey());
42         assertEquals("PfSearchableKey(name=NULL, version=0.0.0)", someKey0.toString());
43
44         PfSearchableKey someKey1 = new PfSearchableKey("my-name", VERSION001);
45         PfSearchableKey someKey2 = new PfSearchableKey(someKey1);
46         PfSearchableKey someKey3 = new PfSearchableKey(someKey1.getId());
47         assertEquals(someKey1, someKey2);
48         assertEquals(someKey1, someKey3);
49         assertFalse(someKey1.isNullVersion());
50         assertEquals("PfSearchableKey(name=my-name, version=0.0.1)", someKey1.toString());
51
52         assertEquals("my-name", someKey1.getName());
53         assertEquals(VERSION001, someKey1.getVersion());
54
55         assertEquals(someKey2, someKey1.getKey());
56         assertEquals(1, someKey1.getKeys().size());
57
58         PfConcept pfc = new DummyPfConcept();
59         assertEquals(PfSearchableKey.getNullKey().getId(), pfc.getId());
60
61         assertTrue(PfSearchableKey.getNullKey().matchesId(pfc.getId()));
62
63         assertTrue(PfSearchableKey.getNullKey().isNullKey());
64
65         assertThatThrownBy(() -> PfSearchableKey.getNullKey().matchesId(null)).hasMessageMatching(ID_IS_NULL);
66
67         assertThatThrownBy(() -> someKey0.setName(null)).isInstanceOf(NullPointerException.class)
68             .hasMessageMatching("^name is marked .*on.*ull but is null$");
69
70         assertThatThrownBy(() -> someKey0.setVersion(null)).isInstanceOf(NullPointerException.class)
71             .hasMessageMatching("^version is marked .*on.*ull but is null$");
72
73         PfSearchableKey someKey4 = new PfSearchableKey("my-name.*", VERSION001);
74         assertEquals("my-name.*", someKey4.getName());
75         assertEquals(VERSION001, someKey4.getVersion());
76     }
77 }