1b3cc209285d2117d3fdfe1e01f7f0daa364e96c
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / rest / serialization / TestXacmlJsonExceptionMapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.rest.serialization;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.IOException;
26 import javax.ws.rs.core.Response;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.policy.common.utils.coder.CoderException;
30 import org.onap.policy.common.utils.coder.StandardCoder;
31
32 public class TestXacmlJsonExceptionMapper {
33     private XacmlJsonExceptionMapper mapper;
34
35     @Before
36     public void setUp() {
37         mapper = new XacmlJsonExceptionMapper();
38     }
39
40     @Test
41     public void testToResponse() throws CoderException {
42         final IOException writeToEx = new IOException("failed to convert a json response to a string");
43         final IOException readFromEx = new IOException("failed to decode incoming request string to a json request");
44         final IOException unexpectedEx = new IOException("unexpected exception");
45         final Response writeToResp = mapper.toResponse(writeToEx);
46         final Response readFromResp = mapper.toResponse(readFromEx);
47         final Response unexpectedResp = mapper.toResponse(unexpectedEx);
48
49         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), writeToResp.getStatus());
50         assertEquals("{'errorDetails':'invalid JSON xacml response'}".replace('\'', '"'),
51                         new StandardCoder().encode(writeToResp.getEntity()));
52         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), readFromResp.getStatus());
53         assertEquals("{'errorDetails':'invalid JSON xacml request'}".replace('\'', '"'),
54                         new StandardCoder().encode(readFromResp.getEntity()));
55         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), unexpectedResp.getStatus());
56     }
57 }