Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / basic-model / src / test / java / org / onap / policy / apex / model / basicmodel / handling / ApexModelWriterTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.basicmodel.handling;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.Writer;
31 import java.lang.reflect.Field;
32 import javax.xml.bind.JAXBException;
33 import javax.xml.bind.Marshaller;
34 import javax.xml.bind.PropertyException;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.MockitoAnnotations;
39 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
41 import org.w3c.dom.Document;
42
43 public class ApexModelWriterTest {
44     @Mock
45     private Marshaller marshallerMock;
46
47     @Test
48     public void testModelWriter() throws IOException, ApexException {
49         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
50
51         modelWriter.setValidateFlag(true);
52         assertTrue(modelWriter.getValidateFlag());
53         assertEquals(0, modelWriter.getCDataFieldSet().size());
54
55         assertFalse(modelWriter.isJsonOutput());
56         modelWriter.setJsonOutput(true);
57         assertTrue(modelWriter.isJsonOutput());
58         modelWriter.setJsonOutput(false);
59         assertFalse(modelWriter.isJsonOutput());
60
61         ByteArrayOutputStream baos = new ByteArrayOutputStream();
62
63         AxModel model = new DummyApexBasicModelCreator().getModel();
64
65         modelWriter.write(model, baos);
66         modelWriter.setJsonOutput(true);
67         modelWriter.write(model, baos);
68         modelWriter.setJsonOutput(false);
69
70         modelWriter.setValidateFlag(false);
71         modelWriter.write(model, baos);
72         modelWriter.setJsonOutput(true);
73         modelWriter.write(model, baos);
74         modelWriter.setJsonOutput(false);
75
76         modelWriter.setValidateFlag(true);
77         model.getKeyInformation().getKeyInfoMap().clear();
78         try {
79             modelWriter.write(model, baos);
80             fail("Test should throw an exception here");
81         } catch (Exception e) {
82             assertEquals("Apex concept xml (BasicModel:0.0.1) validation failed", e.getMessage().substring(0, 53));
83         }
84         model.getKeyInformation().generateKeyInfo(model);
85
86         try {
87             modelWriter.write(null, baos);
88             fail("Test should throw an exception here");
89         } catch (Exception e) {
90             assertEquals("concept may not be null", e.getMessage());
91         }
92
93         try {
94             ByteArrayOutputStream nullBaos = null;
95             modelWriter.write(model, nullBaos);
96             fail("Test should throw an exception here");
97         } catch (Exception e) {
98             assertEquals("concept stream may not be null", e.getMessage());
99         }
100     }
101
102     @Test
103     public void testSetOutputTypeError() throws ApexModelException {
104         MockitoAnnotations.initMocks(this);
105
106         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
107
108         try {
109             Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller");
110             marshallerField.setAccessible(true);
111             marshallerField.set(modelWriter, marshallerMock);
112             marshallerField.setAccessible(false);
113         } catch (Exception validationException) {
114             fail("test should not throw an exception");
115         }
116
117         try {
118             Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock)
119                 .setProperty(Mockito.anyString(), Mockito.anyString());
120             modelWriter.setJsonOutput(true);
121             fail("Test should throw an exception here");
122         } catch (Exception jaxbe) {
123             assertEquals("JAXB error setting marshaller for JSON output", jaxbe.getMessage());
124         }
125
126         try {
127             Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock)
128                 .setProperty(Mockito.anyString(), Mockito.anyString());
129             modelWriter.setJsonOutput(false);
130             fail("Test should throw an exception here");
131         } catch (Exception jaxbe) {
132             assertEquals("JAXB error setting marshaller for XML output", jaxbe.getMessage());
133         }
134     }
135
136     @Test
137     public void testOutputJsonError() throws ApexModelException {
138         MockitoAnnotations.initMocks(this);
139
140         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
141
142         try {
143             Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller");
144             marshallerField.setAccessible(true);
145             marshallerField.set(modelWriter, marshallerMock);
146             marshallerField.setAccessible(false);
147         } catch (Exception validationException) {
148             fail("test should not throw an exception");
149         }
150
151         modelWriter.setValidateFlag(false);
152         modelWriter.setJsonOutput(true);
153
154         try {
155             ByteArrayOutputStream baos = new ByteArrayOutputStream();
156             AxModel model = new DummyApexBasicModelCreator().getModel();
157
158             Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock)
159                 .marshal((AxModel) Mockito.anyObject(), (Writer) Mockito.anyObject());
160
161             modelWriter.write(model, baos);
162             fail("Test should throw an exception here");
163         } catch (Exception jaxbe) {
164             assertEquals("Unable to marshal Apex concept to JSON", jaxbe.getMessage());
165         }
166     }
167
168     @Test
169     public void testOutputXmlError() throws ApexModelException {
170         MockitoAnnotations.initMocks(this);
171
172         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
173         modelWriter.setJsonOutput(false);
174
175         try {
176             Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller");
177             marshallerField.setAccessible(true);
178             marshallerField.set(modelWriter, marshallerMock);
179             marshallerField.setAccessible(false);
180         } catch (Exception validationException) {
181             fail("test should not throw an exception");
182         }
183
184         modelWriter.setValidateFlag(false);
185         modelWriter.setJsonOutput(false);
186
187         try {
188             ByteArrayOutputStream baos = new ByteArrayOutputStream();
189             AxModel model = new DummyApexBasicModelCreator().getModel();
190
191             Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock)
192                 .marshal((AxModel) Mockito.anyObject(), (Document) Mockito.anyObject());
193
194             modelWriter.write(model, baos);
195             fail("Test should throw an exception here");
196         } catch (Exception jaxbe) {
197             assertEquals("Unable to marshal Apex concept to XML", jaxbe.getMessage());
198         }
199     }
200 }