af9e61e2f0d3e69f8995374ad3330ae51857abb4
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / ExceptionsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26
27 import java.io.IOException;
28
29 import javax.ws.rs.core.Response;
30
31 import org.junit.Test;
32 import org.onap.policy.models.errors.concepts.ErrorResponse;
33
34 public class ExceptionsTest {
35
36     private static final String STRING_TEXT = "String";
37     private static final String MESSAGE = "Message";
38
39     @Test
40     public void test() {
41         assertNotNull(new PfModelException(Response.Status.OK, MESSAGE));
42         assertNotNull(new PfModelException(Response.Status.OK, MESSAGE, STRING_TEXT));
43         assertNotNull(new PfModelException(Response.Status.OK, MESSAGE, new IOException()));
44         assertNotNull(new PfModelException(Response.Status.OK, MESSAGE, new IOException(), STRING_TEXT));
45
46         String key = "A String";
47         PfModelException ae =
48                 new PfModelException(Response.Status.OK, MESSAGE, new IOException("IO exception message"), key);
49         ErrorResponse errorResponse = ae.getErrorResponse();
50         assertEquals("Message\nIO exception message", String.join("\n", errorResponse.getErrorDetails()));
51         assertEquals(key, ae.getObject());
52
53         assertNotNull(new PfModelRuntimeException(Response.Status.OK, MESSAGE));
54         assertNotNull(new PfModelRuntimeException(Response.Status.OK, MESSAGE, STRING_TEXT));
55         assertNotNull(new PfModelRuntimeException(Response.Status.OK, MESSAGE, new IOException()));
56         assertNotNull(new PfModelRuntimeException(Response.Status.OK, MESSAGE, new IOException(), STRING_TEXT));
57
58         String rkey = "A String";
59         PfModelRuntimeException re = new PfModelRuntimeException(Response.Status.OK, "Runtime Message",
60                 new IOException("IO runtime exception message"), rkey);
61         errorResponse = re.getErrorResponse();
62         assertEquals("Runtime Message\nIO runtime exception message",
63                 String.join("\n", errorResponse.getErrorDetails()));
64         assertEquals(key, re.getObject());
65     }
66 }