2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019, 2021 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.base;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
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;
33 public class ExceptionsTest {
35 private static final String STRING_TEXT = "String";
36 private static final String MESSAGE = "Message";
40 assertNotNull(new PfModelException(Response.Status.OK, MESSAGE));
41 assertNotNull(new PfModelException(Response.Status.OK, MESSAGE, STRING_TEXT));
42 assertNotNull(new PfModelException(Response.Status.OK, MESSAGE, new IOException()));
43 assertNotNull(new PfModelException(Response.Status.OK, MESSAGE, new IOException(), STRING_TEXT));
45 String key = "A String";
47 new PfModelException(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());
52 assertNotNull(new PfModelRuntimeException(Response.Status.OK, MESSAGE));
53 assertNotNull(new PfModelRuntimeException(Response.Status.OK, MESSAGE, STRING_TEXT));
54 assertNotNull(new PfModelRuntimeException(Response.Status.OK, MESSAGE, new IOException()));
55 assertNotNull(new PfModelRuntimeException(Response.Status.OK, MESSAGE, new IOException(), STRING_TEXT));
57 String rkey = "A String";
58 PfModelRuntimeException re = new PfModelRuntimeException(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());
65 PfModelRuntimeException pfre = new PfModelRuntimeException(ae);
66 assertEquals(ae.getErrorResponse().getResponseCode(), pfre.getErrorResponse().getResponseCode());
67 assertEquals(ae.getMessage(), pfre.getMessage());
71 throw new PfModelException(Status.BAD_GATEWAY, "An Exception");
72 } catch (PfModelException pfme) {
73 throw new PfModelRuntimeException(pfme);
75 } catch (PfModelRuntimeException pfmre) {
76 assertEquals(Status.BAD_GATEWAY, pfmre.getErrorResponse().getResponseCode());
77 assertEquals("An Exception", pfmre.getMessage());
78 assertEquals(PfModelException.class.getName(), pfmre.getCause().getClass().getName());