2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.models.tosca.authorative.concepts;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
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;
32 * Super class to test identity keys.
34 * @param <T> type of key being tested
36 class ToscaIdentifierTestBase<T extends Comparable<T>> {
37 public static final String NAME = "my-name";
38 public static final String VERSION = "1.2.3";
40 private static final Coder coder = new StandardCoder();
42 private final Class<T> clazz;
43 private final String nameField;
44 private final String versionField;
48 * Constructs the object.
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"
54 public ToscaIdentifierTestBase(Class<T> clazz, String nameField, String versionField) {
56 this.nameField = nameField;
57 this.versionField = versionField;
61 * Tests the compareTo() method.
63 * @throws Exception if an error occurs
65 void testCompareTo() throws Exception {
66 T ident = makeIdent(NAME, VERSION);
67 assertEquals(0, ident.compareTo(ident));
69 assertTrue(ident.compareTo(null) > 0);
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);
77 // name takes precedence over version
78 assertTrue(makeIdent(NAME, VERSION + "a").compareTo(makeIdent(NAME + "a", VERSION)) < 0);
82 * Makes an identifier. Uses JSON which does no error checking.
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
89 public T makeIdent(String name, String version) throws CoderException {
90 StringBuilder bldr = new StringBuilder();
95 bldr.append(nameField);
101 if (version != null) {
107 bldr.append(versionField);
109 bldr.append(version);
115 String json = bldr.toString().replace('\'', '"');
117 return coder.decode(json, clazz);