037f6c6f7692fba988f2daf025b29d0f4b207f5d
[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 org.onap.policy.common.endpoints.http.server.internal.GsonMessageBodyHandler;
31
32 /**
33  * GsonMessageBodyHandler that tracks activities.
34  */
35 public class MyGsonProvider extends GsonMessageBodyHandler {
36     private static boolean readSome = false;
37     private static boolean wroteSome = false;
38
39     /**
40      * Constructs the object and resets the variables to indicate that no activity has
41      * occurred yet.
42      */
43     public MyGsonProvider() {
44         super();
45     }
46
47     @Override
48     public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
49                     MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
50
51         readSome = true;
52         return super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream);
53     }
54
55     @Override
56     public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
57                     MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
58
59         wroteSome = true;
60         super.writeTo(object, type, genericType, annotations, mediaType, httpHeaders, entityStream);
61     }
62
63     public static boolean hasReadSome() {
64         return readSome;
65     }
66
67     public static boolean hasWrittenSome() {
68         return wroteSome;
69     }
70
71     public static void resetSome() {
72         MyGsonProvider.readSome = false;
73         MyGsonProvider.wroteSome = false;
74     }
75
76 }