Renaming vid-automation #6
[vid.git] / vid-automation / src / test / java / org / onap / vid / model / mso / RestObject.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 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.vid.model.mso;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.common.base.MoreObjects;
25
26 import javax.ws.rs.core.Response;
27 import java.text.DateFormat;
28 import java.text.SimpleDateFormat;
29
30 /**
31  * The Class RestObject.
32  *
33  * @param <T> the generic type
34  */
35 public class RestObject<T> {
36
37     final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
38
39     final static ObjectMapper objectMapper = new ObjectMapper();
40
41         /**
42          * Generic version of the RestObject class.
43          *
44          */
45     // T stands for "Type"
46     private T t;
47     
48     // The string source of t, if available
49     private String rawT;
50
51     /** The status code. */
52     private int statusCode= 0;
53
54     public RestObject() {
55     }
56
57     public RestObject(Response cres, Class<?> tClass) {
58
59         String rawEntity = null;
60         try {
61             cres.bufferEntity();
62             rawEntity = cres.readEntity(String.class);
63             T t = (T) objectMapper.readValue(rawEntity, tClass);
64             this.set(t);
65         }
66         catch ( Exception e ) {
67             try {
68                 this.setRaw(rawEntity);
69             } catch (Exception e2) {
70             }
71         }
72
73         int status = cres.getStatus();
74         this.setStatusCode (status);
75     }
76
77
78     /**
79      * Sets the.
80      *
81      * @param t the t
82      */
83     public void set(T t) { this.t = t; }
84     
85     /**
86      * Gets the.
87      *
88      * @return the t
89      */
90     public T get() { return t; }
91         
92     /**
93      * Sets the status code.
94      *
95      * @param v the new status code
96      */
97     public void setStatusCode(int v) { this.statusCode = v; }
98     
99     /**
100      * Gets the status code.
101      *
102      * @return the status code
103      */
104     public int getStatusCode() { return this.statusCode; }
105
106     public String getRaw() {
107         return rawT;
108     }
109
110     public void setRaw(String rawT) {
111         this.rawT = rawT;
112     }
113
114     @Override
115     public String toString() {
116         return MoreObjects.toStringHelper(this)
117                 .add("t", t)
118                 .add("rawT", rawT)
119                 .add("statusCode", statusCode)
120                 .toString();
121     }
122 }
123