8c4eea5a054b85137ed015886cd0dc55c6707b6b
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019 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
32 import lombok.AccessLevel;
33 import lombok.Setter;
34
35 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
36
37 /**
38  * JacksonJsonProvider that tracks activities.
39  */
40 public class MyJacksonProvider extends JacksonJsonProvider {
41
42     @Setter(AccessLevel.PRIVATE)
43     private static boolean readSome = false;
44
45     @Setter(AccessLevel.PRIVATE)
46     private static boolean wroteSome = false;
47
48     /**
49      * Constructs the object and resets the variables to indicate that no activity has
50      * occurred yet.
51      */
52     public MyJacksonProvider() {
53         super();
54     }
55
56     @Override
57     public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
58                     MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
59
60         setReadSome(true);
61         return super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream);
62     }
63
64     @Override
65     public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
66                     MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
67
68         setWroteSome(true);
69         super.writeTo(object, type, genericType, annotations, mediaType, httpHeaders, entityStream);
70     }
71
72     public static boolean hasReadSome() {
73         return readSome;
74     }
75
76     public static boolean hasWrittenSome() {
77         return wroteSome;
78     }
79
80     public static void resetSome() {
81         MyJacksonProvider.readSome = false;
82         MyJacksonProvider.wroteSome = false;
83     }
84
85 }