ebd31469ec9cb2d0d8419cd035ff7576d105d4bc
[policy/clamp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
4  * ================================================================================
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.authorative.concepts;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26
27 import org.onap.policy.common.utils.coder.Coder;
28 import org.onap.policy.common.utils.coder.CoderException;
29 import org.onap.policy.common.utils.coder.StandardCoder;
30
31 /**
32  * Super class to test identity keys.
33  *
34  * @param <T> type of key being tested
35  */
36 class ToscaIdentifierTestBase<T extends Comparable<T>> {
37     public static final String NAME = "my-name";
38     public static final String VERSION = "1.2.3";
39
40     private static final Coder coder = new StandardCoder();
41
42     private final Class<T> clazz;
43     private final String nameField;
44     private final String versionField;
45
46
47     /**
48      * Constructs the object.
49      *
50      * @param clazz the type of class being tested
51      * @param nameField name of the field containing the "name"
52      * @param versionField name of the field containing the "version"
53      */
54     public ToscaIdentifierTestBase(Class<T> clazz, String nameField, String versionField) {
55         this.clazz = clazz;
56         this.nameField = nameField;
57         this.versionField = versionField;
58     }
59
60     /**
61      * Tests the compareTo() method.
62      *
63      * @throws Exception if an error occurs
64      */
65     void testCompareTo() throws Exception {
66         T ident = makeIdent(NAME, VERSION);
67         assertEquals(0, ident.compareTo(ident));
68
69         assertTrue(ident.compareTo(null) > 0);
70
71         assertEquals(0, ident.compareTo(makeIdent(NAME, VERSION)));
72         assertTrue(ident.compareTo(makeIdent(NAME, null)) > 0);
73         assertTrue(ident.compareTo(makeIdent(null, VERSION)) > 0);
74         assertTrue(ident.compareTo(makeIdent(NAME, VERSION + "a")) < 0);
75         assertTrue(ident.compareTo(makeIdent(NAME + "a", VERSION)) < 0);
76
77         // name takes precedence over version
78         assertTrue(makeIdent(NAME, VERSION + "a").compareTo(makeIdent(NAME + "a", VERSION)) < 0);
79     }
80
81     /**
82      * Makes an identifier. Uses JSON which does no error checking.
83      *
84      * @param name name to put into the identifier
85      * @param version version to put into the identifier
86      * @return a new identifier
87      * @throws CoderException if the JSON cannot be decoded
88      */
89     public T makeIdent(String name, String version) throws CoderException {
90         StringBuilder bldr = new StringBuilder();
91         bldr.append("{");
92
93         if (name != null) {
94             bldr.append("'");
95             bldr.append(nameField);
96             bldr.append("':'");
97             bldr.append(name);
98             bldr.append("'");
99         }
100
101         if (version != null) {
102             if (name != null) {
103                 bldr.append(',');
104             }
105
106             bldr.append("'");
107             bldr.append(versionField);
108             bldr.append("':'");
109             bldr.append(version);
110             bldr.append("'");
111         }
112
113         bldr.append("}");
114
115         String json = bldr.toString().replace('\'', '"');
116
117         return coder.decode(json, clazz);
118     }
119 }