cd922ebaa1d946fce44ea6406abe188be9c95433
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.http.server.test;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.lang.annotation.Annotation;
28 import java.lang.reflect.Type;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.MultivaluedMap;
31 import lombok.AccessLevel;
32 import lombok.Setter;
33 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
34
35 /**
36  * JacksonJsonProvider that tracks activities.
37  */
38 public class MyJacksonProvider extends JacksonJsonProvider {
39
40     @Setter(AccessLevel.PRIVATE)
41     private static boolean readSome = false;
42
43     @Setter(AccessLevel.PRIVATE)
44     private static boolean wroteSome = false;
45
46     /**
47      * Constructs the object and resets the variables to indicate that no activity has
48      * occurred yet.
49      */
50     public MyJacksonProvider() {
51         super();
52     }
53
54     @Override
55     public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
56                     MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
57
58         setReadSome(true);
59         return super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream);
60     }
61
62     @Override
63     public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
64                     MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
65
66         setWroteSome(true);
67         super.writeTo(object, type, genericType, annotations, mediaType, httpHeaders, entityStream);
68     }
69
70     public static boolean hasReadSome() {
71         return readSome;
72     }
73
74     public static boolean hasWrittenSome() {
75         return wroteSome;
76     }
77
78     public static void resetSome() {
79         MyJacksonProvider.readSome = false;
80         MyJacksonProvider.wroteSome = false;
81     }
82
83 }