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