Add PolicyIdentOptVersion
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / concepts / IdentTestBase.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.pdp.concepts;
22
23 import org.onap.policy.common.utils.coder.Coder;
24 import org.onap.policy.common.utils.coder.CoderException;
25 import org.onap.policy.common.utils.coder.StandardCoder;
26
27 /**
28  * Super class to test identity keys.
29  *
30  * @param <T> type of key being tested
31  */
32 public class IdentTestBase<T> {
33
34     private static final Coder coder = new StandardCoder();
35
36     private final Class<T> clazz;
37
38
39     /**
40      * Constructs the object.
41      * @param clazz the type of class being tested
42      */
43     public IdentTestBase(Class<T> clazz) {
44         this.clazz = clazz;
45     }
46
47     /**
48      * Makes an identifier. Uses JSON which does no error checking.
49      *
50      * @param name name to put into the identifier
51      * @param version version to put into the identifier
52      * @return a new identifier
53      * @throws CoderException if the JSON cannot be decoded
54      */
55     public T makeIdent(String name, String version) throws CoderException {
56         StringBuilder bldr = new StringBuilder();
57         bldr.append("{");
58
59         if (name != null) {
60             bldr.append("'name':'");
61             bldr.append(name);
62             bldr.append("'");
63         }
64
65         if (version != null) {
66             if (name != null) {
67                 bldr.append(',');
68             }
69
70             bldr.append("'version':'");
71             bldr.append(version);
72             bldr.append("'");
73         }
74
75         bldr.append("}");
76
77         String json = bldr.toString().replace('\'', '"');
78
79         return coder.decode(json, clazz);
80     }
81 }