2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.basicmodel.handling;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.Writer;
32 import java.lang.reflect.Field;
33 import javax.xml.bind.JAXBException;
34 import javax.xml.bind.Marshaller;
35 import javax.xml.bind.PropertyException;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.MockitoAnnotations;
40 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
42 import org.w3c.dom.Document;
44 public class ApexModelWriterTest {
46 private Marshaller marshallerMock;
49 public void testModelWriter() throws IOException, ApexException {
50 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
52 modelWriter.setValidateFlag(true);
53 assertTrue(modelWriter.getValidateFlag());
54 assertEquals(0, modelWriter.getCDataFieldSet().size());
56 assertFalse(modelWriter.isJsonOutput());
57 modelWriter.setJsonOutput(true);
58 assertTrue(modelWriter.isJsonOutput());
59 modelWriter.setJsonOutput(false);
60 assertFalse(modelWriter.isJsonOutput());
62 ByteArrayOutputStream baos = new ByteArrayOutputStream();
64 AxModel model = new DummyApexBasicModelCreator().getModel();
66 modelWriter.write(model, baos);
67 modelWriter.setJsonOutput(true);
68 modelWriter.write(model, baos);
69 modelWriter.setJsonOutput(false);
71 modelWriter.setValidateFlag(false);
72 modelWriter.write(model, baos);
73 modelWriter.setJsonOutput(true);
74 modelWriter.write(model, baos);
75 modelWriter.setJsonOutput(false);
77 modelWriter.setValidateFlag(true);
78 model.getKeyInformation().getKeyInfoMap().clear();
79 assertThatThrownBy(() -> modelWriter.write(model, baos))
80 .hasMessageContaining("Apex concept xml (BasicModel:0.0.1) validation failed");
81 model.getKeyInformation().generateKeyInfo(model);
83 assertThatThrownBy(() -> modelWriter.write(null, baos))
84 .hasMessage("concept may not be null");
86 ByteArrayOutputStream nullBaos = null;
87 assertThatThrownBy(() -> modelWriter.write(model, nullBaos))
88 .hasMessage("concept stream may not be null");
92 public void testSetOutputTypeError() throws ApexModelException, NoSuchFieldException, SecurityException,
93 IllegalArgumentException, IllegalAccessException, PropertyException {
94 MockitoAnnotations.initMocks(this);
96 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
98 Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller");
99 marshallerField.setAccessible(true);
100 marshallerField.set(modelWriter, marshallerMock);
101 marshallerField.setAccessible(false);
102 Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock)
103 .setProperty(Mockito.anyString(), Mockito.anyString());
104 assertThatThrownBy(() -> modelWriter.setJsonOutput(true))
105 .hasMessage("JAXB error setting marshaller for JSON output");
106 Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock)
107 .setProperty(Mockito.anyString(), Mockito.anyString());
108 assertThatThrownBy(() -> modelWriter.setJsonOutput(false))
109 .hasMessage("JAXB error setting marshaller for XML output");
113 public void testOutputJsonError() throws ApexModelException, NoSuchFieldException, SecurityException,
114 IllegalArgumentException, IllegalAccessException, JAXBException {
115 MockitoAnnotations.initMocks(this);
117 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
119 Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller");
120 marshallerField.setAccessible(true);
121 marshallerField.set(modelWriter, marshallerMock);
122 marshallerField.setAccessible(false);
124 modelWriter.setValidateFlag(false);
125 modelWriter.setJsonOutput(true);
127 ByteArrayOutputStream baos = new ByteArrayOutputStream();
128 AxModel model = new DummyApexBasicModelCreator().getModel();
129 Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock)
130 .marshal((AxModel) Mockito.anyObject(), (Writer) Mockito.anyObject());
131 assertThatThrownBy(() -> modelWriter.write(model, baos)).hasMessage("Unable to marshal Apex concept to JSON");
135 public void testOutputXmlError() throws ApexModelException, NoSuchFieldException, SecurityException,
136 IllegalArgumentException, IllegalAccessException, JAXBException {
137 MockitoAnnotations.initMocks(this);
139 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
140 modelWriter.setJsonOutput(false);
142 Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller");
143 marshallerField.setAccessible(true);
144 marshallerField.set(modelWriter, marshallerMock);
145 marshallerField.setAccessible(false);
147 modelWriter.setValidateFlag(false);
148 modelWriter.setJsonOutput(false);
150 ByteArrayOutputStream baos = new ByteArrayOutputStream();
151 AxModel model = new DummyApexBasicModelCreator().getModel();
153 Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock)
154 .marshal((AxModel) Mockito.anyObject(), (Document) Mockito.anyObject());
156 assertThatThrownBy(() -> modelWriter.write(model, baos))
157 .hasMessage("Unable to marshal Apex concept to XML");