Remove more duplicate code from models
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / authorative / concepts / ToscaNameVersionTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.authorative.concepts;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.models.base.PfConceptKey;
31 import org.onap.policy.models.base.PfKey;
32
33 public class ToscaNameVersionTest {
34
35     private static final String MY_NAME = "MyName";
36     private static final String MY_VERSION = "1.2.3";
37
38     private ToscaNameVersion tosca;
39
40     @Before
41     public void setUp() {
42         tosca = new ToscaNameVersion(MY_NAME, MY_VERSION);
43     }
44
45     @Test
46     public void testToscaNameVersionPfKey() {
47         tosca = new ToscaNameVersion(new PfConceptKey(MY_NAME, MY_VERSION));
48         assertEquals(MY_NAME, tosca.getName());
49         assertEquals(MY_VERSION, tosca.getVersion());
50
51         assertThatThrownBy(() -> new ToscaNameVersion((PfKey) null)).isInstanceOf(NullPointerException.class)
52                         .hasMessageContaining("key").hasMessageContaining("is null");
53     }
54
55     @Test
56     public void testToscaNameVersionToscaNameVersion() {
57         tosca = new ToscaNameVersion(tosca);
58         assertEquals(MY_NAME, tosca.getName());
59         assertEquals(MY_VERSION, tosca.getVersion());
60     }
61
62     @Test
63     public void testAsConceptKey() {
64         assertEquals(new PfConceptKey(MY_NAME, MY_VERSION), tosca.asConceptKey());
65     }
66
67     @Test
68     public void testCommonCompareTo() {
69         assertThat(tosca.commonCompareTo(tosca)).isZero();
70         assertThat(tosca.commonCompareTo(null)).isNotZero();
71         assertThat(tosca.commonCompareTo(new MyNameVersion(MY_NAME, MY_VERSION))).isNotZero();
72         assertThat(tosca.commonCompareTo(new ToscaNameVersion(tosca))).isZero();
73         assertThat(tosca.commonCompareTo(new ToscaNameVersion(MY_NAME, null))).isNotZero();
74         assertThat(tosca.commonCompareTo(new ToscaNameVersion(null, MY_VERSION))).isNotZero();
75     }
76
77     @Test
78     public void testToString() {
79         assertEquals(MY_NAME + " " + MY_VERSION, tosca.toString());
80     }
81
82     @Test
83     public void testToscaNameVersion() {
84         tosca = new ToscaNameVersion();
85         assertNull(tosca.getName());
86         assertNull(tosca.getVersion());
87     }
88
89     @Test
90     public void testToscaNameVersionStringString() {
91         assertEquals(MY_NAME, tosca.getName());
92         assertEquals(MY_VERSION, tosca.getVersion());
93     }
94
95     private static class MyNameVersion extends ToscaNameVersion {
96         public MyNameVersion(String name, String version) {
97             super(name, version);
98         }
99     }
100 }