a544a62341ab60b118d627e5ec0e9577bf290023
[policy/models.git] / models-interactions / model-actors / actor.so / src / test / java / org / onap / policy / controlloop / actor / so / RestManagerResponseTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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.controlloop.actor.so;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28
29 import javax.ws.rs.core.GenericType;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.utils.coder.Coder;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34
35 public class RestManagerResponseTest {
36     private static final Coder coder = new StandardCoder();
37
38     private static final int MY_STATUS = 200;
39     private static final String MY_TEXT = "{'text': 'hello'}".replace('\'', '"');
40
41     private RestManagerResponse resp;
42
43     @Before
44     public void setUp() {
45         resp = new RestManagerResponse(MY_STATUS, MY_TEXT, coder);
46     }
47
48     @Test
49     public void testGetStatus() {
50         assertEquals(MY_STATUS, resp.getStatus());
51     }
52
53     @Test
54     public void testClose() {
55         assertThatCode(() -> resp.close()).doesNotThrowAnyException();
56     }
57
58     @Test
59     public void testReadEntityClassOfT() {
60         // try with JSON
61         MyObject obj = resp.readEntity(MyObject.class);
62         assertNotNull(obj);
63         assertEquals("hello", obj.text);
64
65         // try plain string
66         resp = new RestManagerResponse(MY_STATUS, "some text", coder);
67         assertEquals("some text", resp.readEntity(String.class));
68
69         // coder throws an exception
70         resp = new RestManagerResponse(MY_STATUS, "{invalid-json", coder);
71         assertThatIllegalArgumentException().isThrownBy(() -> resp.readEntity(MyObject.class))
72                         .withMessage("cannot decode response");
73     }
74
75     @Test
76     @SuppressWarnings("unchecked")
77     public void testUnsupported() {
78         GenericType<String> generic = GenericType.forInstance(String.class);
79
80         assertThatThrownBy(() -> resp.hasEntity()).isInstanceOf(UnsupportedOperationException.class);
81         assertThatThrownBy(() -> resp.bufferEntity()).isInstanceOf(UnsupportedOperationException.class);
82         assertThatThrownBy(() -> resp.getLength()).isInstanceOf(UnsupportedOperationException.class);
83         assertThatThrownBy(() -> resp.readEntity(generic)).isInstanceOf(UnsupportedOperationException.class);
84         assertThatThrownBy(() -> resp.readEntity(String.class, null)).isInstanceOf(UnsupportedOperationException.class);
85         assertThatThrownBy(() -> resp.readEntity(generic, null)).isInstanceOf(UnsupportedOperationException.class);
86         assertThatThrownBy(() -> resp.getStatusInfo()).isInstanceOf(UnsupportedOperationException.class);
87         assertThatThrownBy(() -> resp.getEntity()).isInstanceOf(UnsupportedOperationException.class);
88         assertThatThrownBy(() -> resp.getMediaType()).isInstanceOf(UnsupportedOperationException.class);
89         assertThatThrownBy(() -> resp.getLanguage()).isInstanceOf(UnsupportedOperationException.class);
90         assertThatThrownBy(() -> resp.getAllowedMethods()).isInstanceOf(UnsupportedOperationException.class);
91         assertThatThrownBy(() -> resp.getCookies()).isInstanceOf(UnsupportedOperationException.class);
92         assertThatThrownBy(() -> resp.getEntityTag()).isInstanceOf(UnsupportedOperationException.class);
93         assertThatThrownBy(() -> resp.getDate()).isInstanceOf(UnsupportedOperationException.class);
94         assertThatThrownBy(() -> resp.getLanguage()).isInstanceOf(UnsupportedOperationException.class);
95         assertThatThrownBy(() -> resp.getLastModified()).isInstanceOf(UnsupportedOperationException.class);
96         assertThatThrownBy(() -> resp.getLocation()).isInstanceOf(UnsupportedOperationException.class);
97         assertThatThrownBy(() -> resp.getLinks()).isInstanceOf(UnsupportedOperationException.class);
98         assertThatThrownBy(() -> resp.hasLink(null)).isInstanceOf(UnsupportedOperationException.class);
99         assertThatThrownBy(() -> resp.getLink(null)).isInstanceOf(UnsupportedOperationException.class);
100         assertThatThrownBy(() -> resp.getLinkBuilder(null)).isInstanceOf(UnsupportedOperationException.class);
101         assertThatThrownBy(() -> resp.getMetadata()).isInstanceOf(UnsupportedOperationException.class);
102         assertThatThrownBy(() -> resp.getStringHeaders()).isInstanceOf(UnsupportedOperationException.class);
103         assertThatThrownBy(() -> resp.getHeaderString(null)).isInstanceOf(UnsupportedOperationException.class);
104     }
105
106
107     private static class MyObject {
108         private String text;
109     }
110 }