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;
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;
43 public class ApexModelWriterTest {
45 private Marshaller marshallerMock;
48 public void testModelWriter() throws IOException, ApexException {
49 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
51 modelWriter.setValidateFlag(true);
52 assertTrue(modelWriter.getValidateFlag());
53 assertEquals(0, modelWriter.getCDataFieldSet().size());
55 assertFalse(modelWriter.isJsonOutput());
56 modelWriter.setJsonOutput(true);
57 assertTrue(modelWriter.isJsonOutput());
58 modelWriter.setJsonOutput(false);
59 assertFalse(modelWriter.isJsonOutput());
61 ByteArrayOutputStream baos = new ByteArrayOutputStream();
63 AxModel model = new DummyApexBasicModelCreator().getModel();
65 modelWriter.write(model, baos);
66 modelWriter.setJsonOutput(true);
67 modelWriter.write(model, baos);
68 modelWriter.setJsonOutput(false);
70 modelWriter.setValidateFlag(false);
71 modelWriter.write(model, baos);
72 modelWriter.setJsonOutput(true);
73 modelWriter.write(model, baos);
74 modelWriter.setJsonOutput(false);
76 modelWriter.setValidateFlag(true);
77 model.getKeyInformation().getKeyInfoMap().clear();
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));
84 model.getKeyInformation().generateKeyInfo(model);
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());
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());
103 public void testSetOutputTypeError() throws ApexModelException {
104 MockitoAnnotations.initMocks(this);
106 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
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");
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());
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());
137 public void testOutputJsonError() throws ApexModelException {
138 MockitoAnnotations.initMocks(this);
140 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
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");
151 modelWriter.setValidateFlag(false);
152 modelWriter.setJsonOutput(true);
155 ByteArrayOutputStream baos = new ByteArrayOutputStream();
156 AxModel model = new DummyApexBasicModelCreator().getModel();
158 Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock)
159 .marshal((AxModel) Mockito.anyObject(), (Writer) Mockito.anyObject());
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());
169 public void testOutputXmlError() throws ApexModelException {
170 MockitoAnnotations.initMocks(this);
172 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
173 modelWriter.setJsonOutput(false);
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");
184 modelWriter.setValidateFlag(false);
185 modelWriter.setJsonOutput(false);
188 ByteArrayOutputStream baos = new ByteArrayOutputStream();
189 AxModel model = new DummyApexBasicModelCreator().getModel();
191 Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock)
192 .marshal((AxModel) Mockito.anyObject(), (Document) Mockito.anyObject());
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());