3c16885824b8bad83306c1b287613ad353e2d8dd
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.common.exception;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26
27 import java.io.IOException;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.Response.Status;
30 import org.junit.Test;
31 import org.onap.policy.models.errors.concepts.ErrorResponse;
32
33 public class ExceptionsTest {
34
35     private static final String STRING_TEXT = "String";
36     private static final String MESSAGE = "Message";
37
38     @Test
39     public void testExceptions() {
40         assertNotNull(new ControlLoopException(Response.Status.OK, MESSAGE));
41         assertNotNull(new ControlLoopException(Response.Status.OK, MESSAGE, STRING_TEXT));
42         assertNotNull(new ControlLoopException(Response.Status.OK, MESSAGE, new IOException()));
43         assertNotNull(new ControlLoopException(Response.Status.OK, MESSAGE, new IOException(), STRING_TEXT));
44
45         String key = "A String";
46         ControlLoopException ae =
47                 new ControlLoopException(Response.Status.OK, MESSAGE, new IOException("IO exception message"), key);
48         ErrorResponse errorResponse = ae.getErrorResponse();
49         assertEquals("Message\nIO exception message", String.join("\n", errorResponse.getErrorDetails()));
50         assertEquals(key, ae.getObject());
51
52         assertNotNull(new ControlLoopRuntimeException(Response.Status.OK, MESSAGE));
53         assertNotNull(new ControlLoopRuntimeException(Response.Status.OK, MESSAGE, STRING_TEXT));
54         assertNotNull(new ControlLoopRuntimeException(Response.Status.OK, MESSAGE, new IOException()));
55         assertNotNull(new ControlLoopRuntimeException(Response.Status.OK, MESSAGE, new IOException(), STRING_TEXT));
56
57         String rkey = "A String";
58         ControlLoopRuntimeException re = new ControlLoopRuntimeException(Response.Status.OK, "Runtime Message",
59                 new IOException("IO runtime exception message"), rkey);
60         errorResponse = re.getErrorResponse();
61         assertEquals("Runtime Message\nIO runtime exception message",
62                 String.join("\n", errorResponse.getErrorDetails()));
63         assertEquals(key, re.getObject());
64
65         ControlLoopRuntimeException clre = new ControlLoopRuntimeException(ae);
66         assertEquals(ae.getErrorResponse().getResponseCode(), clre.getErrorResponse().getResponseCode());
67         assertEquals(ae.getMessage(), clre.getMessage());
68
69         try {
70             try {
71                 throw new ControlLoopException(Status.BAD_GATEWAY, "An Exception");
72             } catch (ControlLoopException cle) {
73                 throw new ControlLoopRuntimeException(cle);
74             }
75         } catch (ControlLoopRuntimeException clred) {
76             assertEquals(Status.BAD_GATEWAY, clred.getErrorResponse().getResponseCode());
77             assertEquals("An Exception", clred.getMessage());
78             assertEquals(ControlLoopException.class.getName(), clred.getCause().getClass().getName());
79         }
80
81         assertThat(ae.toString()).contains("A String");
82         assertThat(re.toString()).contains("A String");
83     }
84 }