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