d7bca2808826bbe5972ad81fef01ae16845100c6
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / authorative / concepts / ToscaIdentifierTestBase.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
4  * ================================================================================
5  * Copyright (C) 2019 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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import org.onap.policy.common.utils.coder.Coder;
27 import org.onap.policy.common.utils.coder.CoderException;
28 import org.onap.policy.common.utils.coder.StandardCoder;
29
30 /**
31  * Super class to test identity keys.
32  *
33  * @param <T> type of key being tested
34  */
35 public class ToscaIdentifierTestBase<T extends Comparable<T>> {
36     public static final String NAME = "my-name";
37     public static final String VERSION = "1.2.3";
38
39     private static final Coder coder = new StandardCoder();
40
41     private final Class<T> clazz;
42     private final String nameField;
43     private final String versionField;
44
45
46     /**
47      * Constructs the object.
48      *
49      * @param clazz the type of class being tested
50      * @param nameField name of the field containing the "name"
51      * @param versionField name of the field containing the "version"
52      */
53     public ToscaIdentifierTestBase(Class<T> clazz, String nameField, String versionField) {
54         this.clazz = clazz;
55         this.nameField = nameField;
56         this.versionField = versionField;
57     }
58
59     /**
60      * Tests the compareTo() method.
61      *
62      * @throws Exception if an error occurs
63      */
64     public void testCompareTo() throws Exception {
65         T ident = makeIdent(NAME, VERSION);
66         assertEquals(0, ident.compareTo(ident));
67
68         assertTrue(ident.compareTo(null) > 0);
69
70         assertTrue(ident.compareTo(makeIdent(NAME, VERSION)) == 0);
71         assertTrue(ident.compareTo(makeIdent(NAME, null)) > 0);
72         assertTrue(ident.compareTo(makeIdent(null, VERSION)) > 0);
73         assertTrue(ident.compareTo(makeIdent(NAME, VERSION + "a")) < 0);
74         assertTrue(ident.compareTo(makeIdent(NAME + "a", VERSION)) < 0);
75
76         // name takes precedence over version
77         assertTrue(makeIdent(NAME, VERSION + "a").compareTo(makeIdent(NAME + "a", VERSION)) < 0);
78     }
79
80     /**
81      * Makes an identifier. Uses JSON which does no error checking.
82      *
83      * @param name name to put into the identifier
84      * @param version version to put into the identifier
85      * @return a new identifier
86      * @throws CoderException if the JSON cannot be decoded
87      */
88     public T makeIdent(String name, String version) throws CoderException {
89         StringBuilder bldr = new StringBuilder();
90         bldr.append("{");
91
92         if (name != null) {
93             bldr.append("'");
94             bldr.append(nameField);
95             bldr.append("':'");
96             bldr.append(name);
97             bldr.append("'");
98         }
99
100         if (version != null) {
101             if (name != null) {
102                 bldr.append(',');
103             }
104
105             bldr.append("'");
106             bldr.append(versionField);
107             bldr.append("':'");
108             bldr.append(version);
109             bldr.append("'");
110         }
111
112         bldr.append("}");
113
114         String json = bldr.toString().replace('\'', '"');
115
116         return coder.decode(json, clazz);
117     }
118 }