Clover icon

Magnolia REST Integration 2.1

  1. Project Clover database Fri Mar 16 2018 18:18:02 CET
  2. Package info.magnolia.rest

File RestExceptionMapperTest.java

 

Code metrics

0
31
8
1
150
74
8
0.26
3.88
8
1

Classes

Class Line # Actions
RestExceptionMapperTest 52 31 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.rest;
35   
36    import static javax.ws.rs.core.Response.Status.*;
37    import static org.junit.Assert.*;
38    import static org.mockito.Mockito.when;
39   
40    import javax.jcr.RepositoryException;
41    import javax.ws.rs.NotAllowedException;
42    import javax.ws.rs.NotFoundException;
43    import javax.ws.rs.container.ResourceInfo;
44    import javax.ws.rs.core.Response;
45   
46    import org.junit.Before;
47    import org.junit.Test;
48    import org.mockito.InjectMocks;
49    import org.mockito.Mock;
50    import org.mockito.MockitoAnnotations;
51   
 
52    public class RestExceptionMapperTest {
53   
54    @InjectMocks
55    private final RestExceptionMapper restExceptionMapper = new RestExceptionMapper();
56   
57    @Mock
58    private ResourceInfo resourceInfo;
59   
 
60  6 toggle @Before
61    public void setUp() {
62    // Init mock resource info.
63  6 MockitoAnnotations.initMocks(this);
64    }
65   
 
66  1 toggle @Test
67    public void testThatRestExceptionMapperDoesNotProvideAStackTrace() {
68    // GIVEN
69  1 String errorMessage = "This is a test-exception.";
70  1 Exception exception = new Exception(errorMessage);
71   
72    // WHEN
73  1 Response response = restExceptionMapper.toResponse(exception);
74   
75    // THEN
76  1 assertErrorResponse(response, INTERNAL_SERVER_ERROR, errorMessage, "unknown");
77    }
78   
 
79  5 toggle private void assertErrorResponse(Response response, Response.Status expectedStatusCode, String expectedErrorMessage, String expectedErrorCode) {
80  5 assertEquals(expectedStatusCode, response.getStatusInfo());
81  5 assertNotNull(response.getEntity());
82  5 assertTrue(response.getEntity() instanceof RestError);
83  5 RestError restError = (RestError) response.getEntity();
84  5 assertNotNull(restError.getErrorContainer().getCode());
85  5 assertEquals(expectedErrorCode, restError.getErrorContainer().getCode());
86  5 assertEquals(expectedErrorMessage, restError.getErrorContainer().getMessage());
87    }
88   
 
89  1 toggle @Test
90    public void testMapsRepositoryExceptionToInternalServerError() {
91    // WHEN
92  1 Response response = restExceptionMapper.toResponse(new RepositoryException(""));
93   
94    // THEN
95  1 assertEquals(INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
96    }
97   
 
98  1 toggle @Test
99    public void notFoundException() {
100    // GIVEN
101  1 String errorMessage = "Not found";
102  1 NotFoundException exception = new NotFoundException(errorMessage);
103   
104    // WHEN
105  1 Response response = restExceptionMapper.toResponse(exception);
106   
107    // THEN
108  1 assertErrorResponse(response, NOT_FOUND, errorMessage, "notFound");
109    }
110   
 
111  1 toggle @Test
112    public void methodNotAllowedException() {
113    // GIVEN
114  1 String errorMessage = "Method not allowed";
115  1 NotAllowedException exception = new NotAllowedException(errorMessage, "GET", new String[] { "POST" });
116   
117    // WHEN
118  1 Response response = restExceptionMapper.toResponse(exception);
119   
120    // THEN
121  1 assertErrorResponse(response, METHOD_NOT_ALLOWED, errorMessage, "methodNotAllowed");
122    }
123   
 
124  1 toggle @Test
125    public void notFoundWhenNoResourceMethodCalled() {
126    // GIVEN
127  1 when(resourceInfo.getResourceMethod()).thenReturn(null);
128  1 String errorMessage = "Not found";
129  1 NotFoundException exception = new NotFoundException(errorMessage);
130   
131    // WHEN
132  1 Response response = restExceptionMapper.toResponse(exception);
133   
134    // THEN
135  1 assertErrorResponse(response, NOT_FOUND, errorMessage, "notFound");
136    }
137   
 
138  1 toggle @Test
139    public void trimJaxRsInformation() {
140    // GIVEN
141  1 String errorMessage = "RESTEASY003210: Could not find resource for full path: http://localhost:8080/magnoliaAuthor/.rest/delivery/tours";
142  1 NotFoundException exception = new NotFoundException(errorMessage);
143   
144    // WHEN
145  1 Response response = restExceptionMapper.toResponse(exception);
146   
147    // THEN
148  1 assertErrorResponse(response, NOT_FOUND, "Could not find resource for full path: http://localhost:8080/magnoliaAuthor/.rest/delivery/tours", "notFound");
149    }
150    }