d25e191a85d5b8bc3c1aa94550b995d1435dcee6
[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
33 import javax.xml.bind.JAXBException;
34 import javax.xml.bind.Marshaller;
35 import javax.xml.bind.PropertyException;
36
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
43 import org.onap.policy.apex.model.basicmodel.handling.ApexModelWriter;
44 import org.w3c.dom.Document;
45
46 public class ApexModelWriterTest {
47     @Mock
48     private Marshaller marshallerMock;
49
50     @Test
51     public void testModelWriter() throws IOException, ApexException {
52         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
53
54         modelWriter.setValidateFlag(true);
55         assertTrue(modelWriter.getValidateFlag());
56         assertEquals(0, modelWriter.getCDataFieldSet().size());
57
58         assertFalse(modelWriter.isJsonOutput());
59         modelWriter.setJsonOutput(true);
60         assertTrue(modelWriter.isJsonOutput());
61         modelWriter.setJsonOutput(false);
62         assertFalse(modelWriter.isJsonOutput());
63
64         ByteArrayOutputStream baos = new ByteArrayOutputStream();
65
66         AxModel model = new DummyApexBasicModelCreator().getModel();
67
68         modelWriter.write(model, baos);
69         modelWriter.setJsonOutput(true);
70         modelWriter.write(model, baos);
71         modelWriter.setJsonOutput(false);
72
73         modelWriter.setValidateFlag(false);
74         modelWriter.write(model, baos);
75         modelWriter.setJsonOutput(true);
76         modelWriter.write(model, baos);
77         modelWriter.setJsonOutput(false);
78
79         modelWriter.setValidateFlag(true);
80         model.getKeyInformation().getKeyInfoMap().clear();
81         try {
82             modelWriter.write(model, baos);
83             fail("Test should throw an exception here");
84         } catch (Exception e) {
85             assertEquals("Apex concept xml (BasicModel:0.0.1) validation failed", e.getMessage().substring(0, 53));
86         }
87         model.getKeyInformation().generateKeyInfo(model);
88
89         try {
90             modelWriter.write(null, baos);
91             fail("Test should throw an exception here");
92         } catch (Exception e) {
93             assertEquals("concept may not be null", e.getMessage());
94         }
95
96         try {
97             ByteArrayOutputStream nullBaos = null;
98             modelWriter.write(model, nullBaos);
99             fail("Test should throw an exception here");
100         } catch (Exception e) {
101             assertEquals("concept stream may not be null", e.getMessage());
102         }
103     }
104
105     @Test
106     public void testSetOutputTypeError() throws ApexModelException {
107         MockitoAnnotations.initMocks(this);
108
109         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
110
111         try {
112             Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller");
113             marshallerField.setAccessible(true);
114             marshallerField.set(modelWriter, marshallerMock);
115             marshallerField.setAccessible(false);
116         } catch (Exception validationException) {
117             fail("test should not throw an exception");
118         }
119
120         try {
121             Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock)
122                 .setProperty(Mockito.anyString(), Mockito.anyString());
123             modelWriter.setJsonOutput(true);
124             fail("Test should throw an exception here");
125         } catch (Exception jaxbe) {
126             assertEquals("JAXB error setting marshaller for JSON output", jaxbe.getMessage());
127         }
128
129         try {
130             Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock)
131                 .setProperty(Mockito.anyString(), Mockito.anyString());
132             modelWriter.setJsonOutput(false);
133             fail("Test should throw an exception here");
134         } catch (Exception jaxbe) {
135             assertEquals("JAXB error setting marshaller for XML output", jaxbe.getMessage());
136         }
137     }
138
139     @Test
140     public void testOutputJsonError() throws ApexModelException {
141         MockitoAnnotations.initMocks(this);
142
143         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
144
145         try {
146             Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller");
147             marshallerField.setAccessible(true);
148             marshallerField.set(modelWriter, marshallerMock);
149             marshallerField.setAccessible(false);
150         } catch (Exception validationException) {
151             fail("test should not throw an exception");
152         }
153
154         modelWriter.setValidateFlag(false);
155         modelWriter.setJsonOutput(true);
156
157         try {
158             ByteArrayOutputStream baos = new ByteArrayOutputStream();
159             AxModel model = new DummyApexBasicModelCreator().getModel();
160
161             Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock)
162                 .marshal((AxModel)Mockito.anyObject(), (Writer)Mockito.anyObject());
163
164             modelWriter.write(model, baos);
165             fail("Test should throw an exception here");
166         } catch (Exception jaxbe) {
167             assertEquals("Unable to marshal Apex concept to JSON", jaxbe.getMessage());
168         }
169     }
170
171     @Test
172     public void testOutputXmlError() throws ApexModelException {
173         MockitoAnnotations.initMocks(this);
174
175         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
176         modelWriter.setJsonOutput(false);
177
178         try {
179             Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller");
180             marshallerField.setAccessible(true);
181             marshallerField.set(modelWriter, marshallerMock);
182             marshallerField.setAccessible(false);
183         } catch (Exception validationException) {
184             fail("test should not throw an exception");
185         }
186
187         modelWriter.setValidateFlag(false);
188         modelWriter.setJsonOutput(false);
189
190         try {
191             ByteArrayOutputStream baos = new ByteArrayOutputStream();
192             AxModel model = new DummyApexBasicModelCreator().getModel();
193
194             Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock)
195                 .marshal((AxModel)Mockito.anyObject(), (Document)Mockito.anyObject());
196
197             modelWriter.write(model, baos);
198             fail("Test should throw an exception here");
199         } catch (Exception jaxbe) {
200             assertEquals("Unable to marshal Apex concept to XML", jaxbe.getMessage());
201         }
202     }
203 }