8343d02d8823352126fa9d9745cf78ffaaf6b0bd
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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.common.endpoints.http.server.test;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.lang.annotation.Annotation;
27 import java.lang.reflect.Type;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.MultivaluedMap;
30 import lombok.AccessLevel;
31 import lombok.Setter;
32 import org.onap.policy.common.gson.GsonMessageBodyHandler;
33
34 /**
35  * GsonMessageBodyHandler that tracks activities.
36  */
37 public class MyGsonProvider extends GsonMessageBodyHandler {
38
39     @Setter(AccessLevel.PRIVATE)
40     private static boolean readSome = false;
41
42     @Setter(AccessLevel.PRIVATE)
43     private static boolean wroteSome = false;
44
45     /**
46      * Constructs the object and resets the variables to indicate that no activity has
47      * occurred yet.
48      */
49     public MyGsonProvider() {
50         super();
51     }
52
53     @Override
54     public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
55                     MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
56
57         setReadSome(true);
58         return super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream);
59     }
60
61     @Override
62     public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
63                     MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
64
65         setWroteSome(true);
66         super.writeTo(object, type, genericType, annotations, mediaType, httpHeaders, entityStream);
67     }
68
69     public static boolean hasReadSome() {
70         return readSome;
71     }
72
73     public static boolean hasWrittenSome() {
74         return wroteSome;
75     }
76
77     public static void resetSome() {
78         MyGsonProvider.readSome = false;
79         MyGsonProvider.wroteSome = false;
80     }
81
82 }