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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.basicmodel.handling;
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;
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.Writer;
31 import java.lang.reflect.Field;
33 import javax.xml.bind.JAXBException;
34 import javax.xml.bind.Marshaller;
35 import javax.xml.bind.PropertyException;
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;
46 public class ApexModelWriterTest {
48 private Marshaller marshallerMock;
51 public void testModelWriter() throws IOException, ApexException {
52 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
54 modelWriter.setValidateFlag(true);
55 assertTrue(modelWriter.getValidateFlag());
56 assertEquals(0, modelWriter.getCDataFieldSet().size());
58 assertFalse(modelWriter.isJsonOutput());
59 modelWriter.setJsonOutput(true);
60 assertTrue(modelWriter.isJsonOutput());
61 modelWriter.setJsonOutput(false);
62 assertFalse(modelWriter.isJsonOutput());
64 ByteArrayOutputStream baos = new ByteArrayOutputStream();
66 AxModel model = new DummyApexBasicModelCreator().getModel();
68 modelWriter.write(model, baos);
69 modelWriter.setJsonOutput(true);
70 modelWriter.write(model, baos);
71 modelWriter.setJsonOutput(false);
73 modelWriter.setValidateFlag(false);
74 modelWriter.write(model, baos);
75 modelWriter.setJsonOutput(true);
76 modelWriter.write(model, baos);
77 modelWriter.setJsonOutput(false);
79 modelWriter.setValidateFlag(true);
80 model.getKeyInformation().getKeyInfoMap().clear();
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));
87 model.getKeyInformation().generateKeyInfo(model);
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());
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());
106 public void testSetOutputTypeError() throws ApexModelException {
107 MockitoAnnotations.initMocks(this);
109 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
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");
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());
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());
140 public void testOutputJsonError() throws ApexModelException {
141 MockitoAnnotations.initMocks(this);
143 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
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");
154 modelWriter.setValidateFlag(false);
155 modelWriter.setJsonOutput(true);
158 ByteArrayOutputStream baos = new ByteArrayOutputStream();
159 AxModel model = new DummyApexBasicModelCreator().getModel();
161 Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock)
162 .marshal((AxModel)Mockito.anyObject(), (Writer)Mockito.anyObject());
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());
172 public void testOutputXmlError() throws ApexModelException {
173 MockitoAnnotations.initMocks(this);
175 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
176 modelWriter.setJsonOutput(false);
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");
187 modelWriter.setValidateFlag(false);
188 modelWriter.setJsonOutput(false);
191 ByteArrayOutputStream baos = new ByteArrayOutputStream();
192 AxModel model = new DummyApexBasicModelCreator().getModel();
194 Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock)
195 .marshal((AxModel)Mockito.anyObject(), (Document)Mockito.anyObject());
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());