2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actor.so;
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;
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;
35 public class RestManagerResponseTest {
36 private static final Coder coder = new StandardCoder();
38 private static final int MY_STATUS = 200;
39 private static final String MY_TEXT = "{'text': 'hello'}".replace('\'', '"');
41 private RestManagerResponse resp;
45 resp = new RestManagerResponse(MY_STATUS, MY_TEXT, coder);
49 public void testGetStatus() {
50 assertEquals(MY_STATUS, resp.getStatus());
54 public void testClose() {
55 assertThatCode(() -> resp.close()).doesNotThrowAnyException();
59 public void testReadEntityClassOfT() {
61 MyObject obj = resp.readEntity(MyObject.class);
63 assertEquals("hello", obj.text);
66 resp = new RestManagerResponse(MY_STATUS, "some text", coder);
67 assertEquals("some text", resp.readEntity(String.class));
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");
76 @SuppressWarnings("unchecked")
77 public void testUnsupported() {
78 GenericType<String> generic = GenericType.forInstance(String.class);
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);
107 private static class MyObject {