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 RestErrorTest.java

 

Code metrics

0
31
5
1
129
63
5
0.16
6.2
5
1

Classes

Class Line # Actions
RestErrorTest 52 31 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 4 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 org.hamcrest.core.StringContains.containsString;
37    import static org.junit.Assert.*;
38   
39    import java.io.ByteArrayOutputStream;
40    import java.io.StringReader;
41    import java.io.StringWriter;
42   
43    import javax.xml.bind.JAXBContext;
44    import javax.xml.bind.Marshaller;
45    import javax.xml.bind.Unmarshaller;
46   
47    import org.junit.Before;
48    import org.junit.Test;
49   
50    import com.fasterxml.jackson.databind.ObjectMapper;
51   
 
52    public class RestErrorTest {
53   
54    private Marshaller marshaller;
55    private Unmarshaller unmarshaller;
56    private ObjectMapper mapper;
57    private ByteArrayOutputStream baos;
58   
 
59  4 toggle @Before
60    public void setUp() throws Exception {
61  4 JAXBContext context = JAXBContext.newInstance(RestError.class);
62  4 marshaller = context.createMarshaller();
63  4 unmarshaller = context.createUnmarshaller();
64  4 mapper = new ObjectMapper();
65  4 baos = new ByteArrayOutputStream();
66    }
67   
 
68  1 toggle @Test
69    public void marshallObjectIntoXml() throws Exception {
70    // GIVEN
71  1 String errorCode = "thisIsCode";
72  1 String errorMessage = "thisIsMessage";
73  1 RestError error = new RestError(errorCode, errorMessage);
74  1 StringWriter writer = new StringWriter();
75   
76    // WHEN
77  1 marshaller.marshal(error, writer);
78  1 String errorXml = writer.toString();
79   
80    // THEN
81  1 assertThat(errorXml, containsString(errorCode));
82  1 assertThat(errorXml, containsString(errorMessage));
83    }
84   
 
85  1 toggle @Test
86    public void unmarshallXmlIntoObject() throws Exception {
87    // GIVEN
88  1 String errorCode = "thisIsCode";
89  1 String errorMessage = "thisIsMessage";
90  1 String errorXml = String.format("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><restError><error><code>%s</code><message>%s</message></error></restError>", errorCode, errorMessage);
91   
92    // WHEN
93  1 RestError restError = (RestError) unmarshaller.unmarshal(new StringReader(errorXml));
94   
95    // THEN
96  1 assertEquals(errorCode, restError.getErrorContainer().getCode());
97  1 assertEquals(errorMessage, restError.getErrorContainer().getMessage());
98    }
99   
 
100  1 toggle @Test
101    public void writeObjectIntoJson() throws Exception {
102    // GIVEN
103  1 String errorCode = "thisIsCode";
104  1 String errorMessage = "thisIsMessage";
105  1 RestError restError = new RestError(errorCode, errorMessage);
106   
107    // WHEN
108  1 String errorJson = mapper.writeValueAsString(restError);
109   
110    // THEN
111  1 assertThat(errorJson, containsString(errorCode));
112  1 assertThat(errorJson, containsString(errorMessage));
113    }
114   
 
115  1 toggle @Test
116    public void readJsonIntoObject() throws Exception {
117    // GIVEN
118  1 String errorCode = "thisIsCode";
119  1 String errorMessage = "thisIsMessage";
120  1 String errorJson = String.format("{\"error\":{\"code\":\"%s\",\"message\":\"%s\"}}", errorCode, errorMessage);
121   
122    // WHEN
123  1 RestError restError = mapper.readValue(errorJson, RestError.class);
124   
125    // THEN
126  1 assertEquals(errorCode, restError.getErrorContainer().getCode());
127  1 assertEquals(errorMessage, restError.getErrorContainer().getMessage());
128    }
129    }