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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.base;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
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;
31 import org.onap.policy.models.base.testconcepts.DummyPfConcept;
33 public class PfConceptKeyTest {
35 private static final String VERSION001 = "0.0.1";
36 private static final String ID_IS_NULL = "id is marked .*on.*ull but is null$";
39 public void testConceptKey() {
40 PfConceptKey someKey0 = new PfConceptKey();
41 assertEquals(PfConceptKey.getNullKey(), someKey0);
42 assertTrue(someKey0.isNullKey());
43 assertEquals("PfConceptKey(name=NULL, version=0.0.0)", someKey0.toString());
45 PfConceptKey someKey1 = new PfConceptKey("my-name", VERSION001);
46 PfConceptKey someKey2 = new PfConceptKey(someKey1);
47 PfConceptKey someKey3 = new PfConceptKey(someKey1.getId());
48 assertEquals(someKey1, someKey2);
49 assertEquals(someKey1, someKey3);
50 assertFalse(someKey1.isNullVersion());
51 assertFalse(someKey1.isNullName());
52 assertEquals("PfConceptKey(name=my-name, version=0.0.1)", someKey1.toString());
54 assertEquals("my-name", someKey1.getName());
55 assertEquals(VERSION001, someKey1.getVersion());
57 assertEquals(someKey2, someKey1.getKey());
58 assertEquals(1, someKey1.getKeys().size());
60 PfConcept pfc = new DummyPfConcept();
61 assertEquals(PfConceptKey.getNullKey().getId(), pfc.getId());
63 assertTrue(PfConceptKey.getNullKey().matchesId(pfc.getId()));
65 assertTrue(PfConceptKey.getNullKey().isNullKey());
67 assertThatThrownBy(() -> PfConceptKey.getNullKey().matchesId(null)).hasMessageMatching(ID_IS_NULL);
69 assertThatThrownBy(() -> someKey0.setName(null)).isInstanceOf(NullPointerException.class)
70 .hasMessageMatching("^name is marked .*on.*ull but is null$");
72 assertThatThrownBy(() -> someKey0.setVersion(null)).isInstanceOf(NullPointerException.class)
73 .hasMessageMatching("^version is marked .*on.*ull but is null$");
75 assertThatIllegalArgumentException().isThrownBy(() -> new PfConceptKey("my-name.*", VERSION001)).withMessage(
76 "parameter 'name': value 'my-name.*', does not match regular expression '^[A-Za-z0-9\\-_\\.]+$'"