Changed identifiers to concept identifiers
[policy/models.git] / models-pap / src / test / java / org / onap / policy / models / pap / concepts / PapPolicyIdentifierTest.java
1 /*-
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
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  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.models.pap.concepts;
20
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;
25
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;
33
34 /**
35  * This only tests the methods that aren't already tested via TestModels.
36  */
37 public class PapPolicyIdentifierTest {
38
39     @Test
40     public void testPapPolicyIdentifier() throws CoderException {
41         assertNotNull(new PapPolicyIdentifier("Name", "Version"));
42         assertNotNull(new PapPolicyIdentifier("Name", null));
43         assertNotNull(new PapPolicyIdentifier(null, null));
44
45         assertNotNull(new PapPolicyIdentifier(new ToscaConceptIdentifier("Name", "Version")));
46         assertNotNull(new PapPolicyIdentifier(new ToscaConceptIdentifierOptVersion("Name", "Version")));
47
48         assertThatThrownBy(() -> new PapPolicyIdentifier((ToscaConceptIdentifier) null))
49                 .isInstanceOf(NullPointerException.class);
50
51         assertThatThrownBy(() -> new PapPolicyIdentifier((ToscaConceptIdentifierOptVersion) null))
52                 .isInstanceOf(NullPointerException.class);
53
54         PapPolicyIdentifier ppi = new PapPolicyIdentifier("myname", "1.2.3");
55
56         assertEquals("myname", ppi.getGenericIdentifier().getName());
57         assertEquals("1.2.3", ppi.getGenericIdentifier().getVersion());
58
59         PapPolicyIdentifier ppi2 = new PapPolicyIdentifier(null, null);
60         assertNull(ppi2.getGenericIdentifier().getName());
61         assertNull(ppi2.getGenericIdentifier().getVersion());
62     }
63
64     @Test
65     public void testPapPolicyIdentifierSerialization() throws CoderException, IOException {
66         String idString = TextFileUtils.getTextFileAsString("src/test/resources/json/PapPolicyIdentifier.json");
67
68         StandardCoder coder = new StandardCoder();
69
70         PapPolicyIdentifier ppi = coder.decode(idString, PapPolicyIdentifier.class);
71
72         assertEquals("MyPolicy", ppi.getGenericIdentifier().getName());
73         assertEquals("1.2.6", ppi.getGenericIdentifier().getVersion());
74
75         String idStringBack = coder.encode(ppi);
76         assertEquals(idString.replaceAll("\\s+", ""), idStringBack.replaceAll("\\s+", ""));
77     }
78 }