2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.models.tosca.authorative.concepts;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
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;
31 * Super class to test identity keys.
33 * @param <T> type of key being tested
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";
39 private static final Coder coder = new StandardCoder();
41 private final Class<T> clazz;
42 private final String nameField;
43 private final String versionField;
47 * Constructs the object.
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"
53 public ToscaIdentifierTestBase(Class<T> clazz, String nameField, String versionField) {
55 this.nameField = nameField;
56 this.versionField = versionField;
60 * Tests the compareTo() method.
62 * @throws Exception if an error occurs
64 public void testCompareTo() throws Exception {
65 T ident = makeIdent(NAME, VERSION);
66 assertEquals(0, ident.compareTo(ident));
68 assertTrue(ident.compareTo(null) > 0);
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);
76 // name takes precedence over version
77 assertTrue(makeIdent(NAME, VERSION + "a").compareTo(makeIdent(NAME + "a", VERSION)) < 0);
81 * Makes an identifier. Uses JSON which does no error checking.
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
88 public T makeIdent(String name, String version) throws CoderException {
89 StringBuilder bldr = new StringBuilder();
94 bldr.append(nameField);
100 if (version != null) {
106 bldr.append(versionField);
108 bldr.append(version);
114 String json = bldr.toString().replace('\'', '"');
116 return coder.decode(json, clazz);