2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
19 package org.onap.policy.models.pap.concepts;
21 import static org.assertj.core.api.Assertions.assertThatThrownBy;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
26 import java.io.IOException;
27 import org.junit.Test;
28 import org.onap.policy.common.utils.coder.CoderException;
29 import org.onap.policy.common.utils.coder.StandardCoder;
30 import org.onap.policy.common.utils.resources.TextFileUtils;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
35 * This only tests the methods that aren't already tested via TestModels.
37 public class PapPolicyIdentifierTest {
40 public void testPapPolicyIdentifier() throws CoderException {
41 assertNotNull(new PapPolicyIdentifier("Name", "Version"));
42 assertNotNull(new PapPolicyIdentifier("Name", null));
43 assertNotNull(new PapPolicyIdentifier(null, null));
45 assertNotNull(new PapPolicyIdentifier(new ToscaConceptIdentifier("Name", "Version")));
46 assertNotNull(new PapPolicyIdentifier(new ToscaConceptIdentifierOptVersion("Name", "Version")));
48 assertThatThrownBy(() -> new PapPolicyIdentifier((ToscaConceptIdentifier) null))
49 .isInstanceOf(NullPointerException.class);
51 assertThatThrownBy(() -> new PapPolicyIdentifier((ToscaConceptIdentifierOptVersion) null))
52 .isInstanceOf(NullPointerException.class);
54 PapPolicyIdentifier ppi = new PapPolicyIdentifier("myname", "1.2.3");
56 assertEquals("myname", ppi.getGenericIdentifier().getName());
57 assertEquals("1.2.3", ppi.getGenericIdentifier().getVersion());
59 PapPolicyIdentifier ppi2 = new PapPolicyIdentifier(null, null);
60 assertNull(ppi2.getGenericIdentifier().getName());
61 assertNull(ppi2.getGenericIdentifier().getVersion());
65 public void testPapPolicyIdentifierSerialization() throws CoderException, IOException {
66 String idString = TextFileUtils.getTextFileAsString("src/test/resources/json/PapPolicyIdentifier.json");
68 StandardCoder coder = new StandardCoder();
70 PapPolicyIdentifier ppi = coder.decode(idString, PapPolicyIdentifier.class);
72 assertEquals("MyPolicy", ppi.getGenericIdentifier().getName());
73 assertEquals("1.2.6", ppi.getGenericIdentifier().getVersion());
75 String idStringBack = coder.encode(ppi);
76 assertEquals(idString.replaceAll("\\s+", ""), idStringBack.replaceAll("\\s+", ""));