Upgrade Java 17 in xacml-pdp
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / rest / serialization / CommonSerialization.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2023 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pdpx.main.rest.serialization;
23
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import com.att.research.xacml.api.Request;
28 import com.att.research.xacml.api.Response;
29 import jakarta.ws.rs.core.MediaType;
30 import java.lang.annotation.Annotation;
31 import java.lang.reflect.Type;
32
33 public class CommonSerialization {
34
35     @SuppressWarnings("rawtypes")
36     private static final Class REQUEST_CLASS = Request.class;
37     @SuppressWarnings("rawtypes")
38     private static final Class RESPONSE_CLASS = Response.class;
39     @SuppressWarnings("rawtypes")
40     private static final Class GENERAL_CLASS = MyObject.class;
41
42     public static void testIsWritableOrReadable(String primaryType, String subType,
43             MultiArgsFunction<Class<?>, Type, Annotation[], MediaType, Boolean> getter) {
44
45         // null media type
46         assertFalse(getter.apply(null, null, null, null));
47
48         // valid media type and class type
49         assertTrue("writeable " + subType, getter.apply(
50                 REQUEST_CLASS, null, null, new MediaType(primaryType, subType)));
51         assertTrue("writeable " + subType, getter.apply(
52                 RESPONSE_CLASS, null, null, new MediaType(primaryType, subType)));
53
54         // valid media type but invalid class type
55         assertFalse(getter.apply(
56                 GENERAL_CLASS, null, null, new MediaType(primaryType, subType)));
57
58         // null subtype or invalid media type
59         assertFalse(getter.apply(null, null, null, new MediaType(primaryType, null)));
60         assertFalse(getter.apply(null, null, null, MediaType.APPLICATION_JSON_TYPE));
61     }
62
63     public static class MyObject {
64         private int id;
65
66         public MyObject() {
67             super();
68         }
69
70         public MyObject(int id) {
71             this.id = id;
72         }
73
74         @Override
75         public String toString() {
76             return "MyObject [id=" + id + "]";
77         }
78     }
79
80     @FunctionalInterface
81     public interface MultiArgsFunction<T, U, V, W, R> {
82         public R apply(T value1, U value2, V value3, W value4);
83     }
84 }