Clover icon

Magnolia REST Integration 2.1.5

  1. Project Clover database Tue Jan 28 2020 16:36:47 CET
  2. Package info.magnolia.rest

File RuntimeRepositoryExceptionMapperTest.java

 

Code metrics

0
19
4
1
104
48
4
0.21
4.75
4
1

Classes

Class Line # Actions
RuntimeRepositoryExceptionMapperTest 52 19 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /**
2    * This file Copyright (c) 2017-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.INTERNAL_SERVER_ERROR;
37    import static org.junit.Assert.*;
38    import static org.mockito.Mockito.when;
39   
40    import info.magnolia.jcr.RuntimeRepositoryException;
41   
42    import javax.jcr.RepositoryException;
43    import javax.ws.rs.core.Response;
44    import javax.ws.rs.ext.Providers;
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 RuntimeRepositoryExceptionMapperTest {
53   
54    @InjectMocks
55    private final RuntimeRepositoryExceptionMapper runtimeRepositoryExceptionMapper = new RuntimeRepositoryExceptionMapper();
56   
57    @Mock
58    private Providers providers;
59   
 
60  2 toggle @Before
61    public void setUp() {
62    // Mock providers and repositoryExceptionMapper to delegate later.
63  2 MockitoAnnotations.initMocks(this);
64  2 RepositoryExceptionMapper repositoryExceptionMapper = new RepositoryExceptionMapper();
65  2 when(providers.getExceptionMapper(RepositoryException.class)).thenReturn(repositoryExceptionMapper);
66    }
67   
 
68  1 toggle @Test
69    public void runtimeRepositoryExceptionHasCause() {
70    // GIVEN
71  1 String errorMessage = "Repository exception";
72  1 RepositoryException repositoryException = new RepositoryException(errorMessage);
73  1 RuntimeRepositoryException runtimeRepositoryException = new RuntimeRepositoryException(repositoryException);
74   
75    // WHEN
76  1 Response response = runtimeRepositoryExceptionMapper.toResponse(runtimeRepositoryException);
77   
78    // THEN
79  1 assertErrorResponse(response, INTERNAL_SERVER_ERROR, errorMessage, "unknown");
80    }
81   
 
82  1 toggle @Test
83    public void runtimeRepositoryExceptionDoesNotHaveCause() {
84    // GIVEN
85  1 String errorMessage = "Runtime repository exception";
86  1 RuntimeRepositoryException runtimeRepositoryException = new RuntimeRepositoryException(errorMessage, null);
87   
88    // WHEN
89  1 Response response = runtimeRepositoryExceptionMapper.toResponse(runtimeRepositoryException);
90   
91    // THEN
92  1 assertErrorResponse(response, INTERNAL_SERVER_ERROR, errorMessage, "unknown");
93    }
94   
 
95  2 toggle private void assertErrorResponse(Response response, Response.Status expectedStatusCode, String expectedErrorMessage, String expectedErrorCode) {
96  2 assertEquals(expectedStatusCode, response.getStatusInfo());
97  2 assertNotNull(response.getEntity());
98  2 assertTrue(response.getEntity() instanceof RestError);
99  2 RestError restError = (RestError) response.getEntity();
100  2 assertNotNull(restError.getErrorContainer().getCode());
101  2 assertEquals(expectedErrorCode, restError.getErrorContainer().getCode());
102  2 assertEquals(expectedErrorMessage, restError.getErrorContainer().getMessage());
103    }
104    }