a29858393a71b25c750f25eb02926b939419b3b7
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfConceptKeyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
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 PfConceptKeyTest {
33
34     private static final String VERSION001 = "0.0.1";
35     private static final String ID_IS_NULL = "id is marked @NonNull but is null";
36
37     @Test
38     public void testConceptKey() {
39         PfConceptKey someKey0 = new PfConceptKey();
40         assertEquals(PfConceptKey.getNullKey(), someKey0);
41         assertTrue(someKey0.isNullKey());
42         assertEquals("PfConceptKey(name=NULL, version=0.0.0)", someKey0.toString());
43
44         PfConceptKey someKey1 = new PfConceptKey("my-name", VERSION001);
45         PfConceptKey someKey2 = new PfConceptKey(someKey1);
46         PfConceptKey someKey3 = new PfConceptKey(someKey1.getId());
47         assertEquals(someKey1, someKey2);
48         assertEquals(someKey1, someKey3);
49         assertFalse(someKey1.isNullVersion());
50         assertEquals("PfConceptKey(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
59         PfConcept pfc = new DummyPfConcept();
60         assertEquals(PfConceptKey.getNullKey().getId(), pfc.getId());
61
62         assertTrue(PfConceptKey.getNullKey().matchesId(pfc.getId()));
63
64         assertTrue(PfConceptKey.getNullKey().isNullKey());
65
66         assertThatThrownBy(() -> PfConceptKey.getNullKey().matchesId(null)).hasMessage(ID_IS_NULL);
67
68         assertThatThrownBy(() -> someKey0.setName(null)).isInstanceOf(NullPointerException.class)
69                         .hasMessage("name is marked @NonNull but is null");
70
71         assertThatThrownBy(() -> someKey0.setVersion(null)).isInstanceOf(NullPointerException.class)
72                         .hasMessage("version is marked @NonNull but is null");
73
74         assertThatIllegalArgumentException().isThrownBy(() -> new PfConceptKey("my-name.*", VERSION001)).withMessage(
75                         "parameter 'name': value 'my-name.*', does not match regular expression '^[A-Za-z0-9\\-_\\.]+$'"
76                                         .replace('\'', '"'));
77     }
78 }