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.BufferedReader;
29 import java.io.ByteArrayInputStream;
30 import java.io.ByteArrayOutputStream;
32 import java.io.FileInputStream;
33 import java.io.FileReader;
34 import java.io.IOException;
35 import java.lang.reflect.Field;
37 import javax.xml.bind.JAXBException;
38 import javax.xml.bind.PropertyException;
39 import javax.xml.bind.Unmarshaller;
40 import javax.xml.transform.stream.StreamSource;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
48 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
49 import org.onap.policy.apex.model.basicmodel.handling.ApexModelWriter;
51 public class ApexModelReaderTest {
53 private Unmarshaller unmarshallerMock;
56 public void testModelReader() throws IOException, ApexException {
57 AxModel model = new DummyApexBasicModelCreator().getModel();
58 AxModel invalidModel = new DummyApexBasicModelCreator().getInvalidModel();
60 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
61 modelWriter.setValidateFlag(true);
62 modelWriter.setJsonOutput(true);
64 ByteArrayOutputStream baos = new ByteArrayOutputStream();
65 modelWriter.write(model, baos);
67 ByteArrayOutputStream baosInvalid = new ByteArrayOutputStream();
68 modelWriter.setValidateFlag(false);
69 modelWriter.write(invalidModel, baosInvalid);
71 ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
73 modelReader.setValidateFlag(true);
74 assertTrue(modelReader.getValidateFlag());
76 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
77 AxModel readModel = modelReader.read(bais);
78 assertEquals(model, readModel);
80 ByteArrayInputStream baisInvalid = new ByteArrayInputStream(baosInvalid.toByteArray());
82 modelReader.read(baisInvalid);
83 fail("test should throw an exceptino here");
86 assertTrue(e.getMessage().startsWith("Apex concept validation failed"));
89 modelReader.setValidateFlag(false);
90 assertFalse(modelReader.getValidateFlag());
92 ByteArrayInputStream bais2 = new ByteArrayInputStream(baos.toByteArray());
93 AxModel readModel2 = modelReader.read(bais2);
94 assertEquals(model, readModel2);
96 modelWriter.setJsonOutput(false);
98 ByteArrayOutputStream baosXml = new ByteArrayOutputStream();
99 modelWriter.write(model, baosXml);
101 ByteArrayInputStream baisXml = new ByteArrayInputStream(baosXml.toByteArray());
102 AxModel readModelXml = modelReader.read(baisXml);
103 assertEquals(model, readModelXml);
105 String dummyString = "SomeDummyText";
106 ByteArrayInputStream baisDummy = new ByteArrayInputStream(dummyString.getBytes());
108 modelReader.read(baisDummy);
109 fail("test should throw an exception here");
111 catch (Exception e) {
112 assertEquals("format of input for Apex concept is neither JSON nor XML", e.getMessage());
116 ByteArrayInputStream nullBais = null;
117 modelReader.read(nullBais);
118 fail("test should throw an exception here");
120 catch (Exception e) {
121 assertEquals("concept stream may not be null", e.getMessage());
125 FileInputStream fis = new FileInputStream(new File("somewhere/over/the/rainbow"));
126 modelReader.read(fis);
127 fail("test should throw an exception here");
129 catch (Exception e) {
130 assertTrue(e.getMessage().contains("rainbow"));
133 File tempFile = File.createTempFile("Apex", "Dummy");
135 BufferedReader br = new BufferedReader(new FileReader(tempFile));
137 modelReader.read(br);
138 fail("test should throw an exception here");
140 catch (Exception e) {
141 assertEquals("Unable to read Apex concept ", e.getMessage());
147 modelReader.setSchema(null);
149 tempFile = File.createTempFile("Apex", "Dummy");
151 modelReader.setSchema(tempFile.getCanonicalPath());
152 fail("test should throw an exception here");
154 catch (Exception e) {
155 assertEquals("Unable to load schema", e.getMessage());
161 modelReader.setSchema("xml/example.xsd");
165 public void testSetInputTypeError() throws ApexModelException {
166 MockitoAnnotations.initMocks(this);
168 ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
171 Field marshallerField = modelReader.getClass().getDeclaredField("unmarshaller");
172 marshallerField.setAccessible(true);
173 marshallerField.set(modelReader, unmarshallerMock);
174 marshallerField.setAccessible(false);
175 } catch (Exception validationException) {
176 fail("test should not throw an exception");
180 Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(unmarshallerMock)
181 .unmarshal((StreamSource)Mockito.anyObject(), Mockito.anyObject());
183 modelReader.read("{Hello}");
184 fail("Test should throw an exception here");
185 } catch (Exception jaxbe) {
186 assertEquals("Unable to unmarshal Apex concept ", jaxbe.getMessage());
190 Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(unmarshallerMock)
191 .setProperty(Mockito.anyString(), Mockito.anyString());
192 modelReader.read("{Hello}");
193 fail("Test should throw an exception here");
194 } catch (Exception jaxbe) {
195 assertEquals("JAXB error setting unmarshaller for JSON input", jaxbe.getMessage());
199 Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(unmarshallerMock)
200 .setProperty(Mockito.anyString(), Mockito.anyString());
201 modelReader.read("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
202 fail("Test should throw an exception here");
203 } catch (Exception jaxbe) {
204 assertEquals("JAXB error setting unmarshaller for XML input", jaxbe.getMessage());