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;
36 import javax.xml.bind.JAXBException;
37 import javax.xml.bind.PropertyException;
38 import javax.xml.bind.Unmarshaller;
39 import javax.xml.transform.stream.StreamSource;
40 import org.junit.Test;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
47 public class ApexModelReaderTest {
49 private Unmarshaller unmarshallerMock;
52 public void testModelReader() throws IOException, ApexException {
53 AxModel model = new DummyApexBasicModelCreator().getModel();
54 AxModel invalidModel = new DummyApexBasicModelCreator().getInvalidModel();
56 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
57 modelWriter.setValidateFlag(true);
58 modelWriter.setJsonOutput(true);
60 ByteArrayOutputStream baos = new ByteArrayOutputStream();
61 modelWriter.write(model, baos);
63 ByteArrayOutputStream baosInvalid = new ByteArrayOutputStream();
64 modelWriter.setValidateFlag(false);
65 modelWriter.write(invalidModel, baosInvalid);
67 ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
69 modelReader.setValidateFlag(true);
70 assertTrue(modelReader.getValidateFlag());
72 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
73 AxModel readModel = modelReader.read(bais);
74 assertEquals(model, readModel);
76 ByteArrayInputStream baisInvalid = new ByteArrayInputStream(baosInvalid.toByteArray());
78 modelReader.read(baisInvalid);
79 fail("test should throw an exceptino here");
80 } catch (Exception e) {
81 assertTrue(e.getMessage().startsWith("Apex concept validation failed"));
84 modelReader.setValidateFlag(false);
85 assertFalse(modelReader.getValidateFlag());
87 ByteArrayInputStream bais2 = new ByteArrayInputStream(baos.toByteArray());
88 AxModel readModel2 = modelReader.read(bais2);
89 assertEquals(model, readModel2);
91 modelWriter.setJsonOutput(false);
93 ByteArrayOutputStream baosXml = new ByteArrayOutputStream();
94 modelWriter.write(model, baosXml);
96 ByteArrayInputStream baisXml = new ByteArrayInputStream(baosXml.toByteArray());
97 AxModel readModelXml = modelReader.read(baisXml);
98 assertEquals(model, readModelXml);
100 String dummyString = "SomeDummyText";
101 ByteArrayInputStream baisDummy = new ByteArrayInputStream(dummyString.getBytes());
103 modelReader.read(baisDummy);
104 fail("test should throw an exception here");
105 } catch (Exception e) {
106 assertEquals("format of input for Apex concept is neither JSON nor XML", e.getMessage());
110 ByteArrayInputStream nullBais = null;
111 modelReader.read(nullBais);
112 fail("test should throw an exception here");
113 } catch (Exception e) {
114 assertEquals("concept stream may not be null", e.getMessage());
118 FileInputStream fis = new FileInputStream(new File("somewhere/over/the/rainbow"));
119 modelReader.read(fis);
120 fail("test should throw an exception here");
121 } catch (Exception e) {
122 assertTrue(e.getMessage().contains("rainbow"));
125 File tempFile = File.createTempFile("Apex", "Dummy");
127 BufferedReader br = new BufferedReader(new FileReader(tempFile));
129 modelReader.read(br);
130 fail("test should throw an exception here");
131 } catch (Exception e) {
132 assertEquals("Unable to read Apex concept ", e.getMessage());
137 modelReader.setSchema(null);
139 tempFile = File.createTempFile("Apex", "Dummy");
141 modelReader.setSchema(tempFile.getCanonicalPath());
142 fail("test should throw an exception here");
143 } catch (Exception e) {
144 assertEquals("Unable to load schema", e.getMessage());
149 modelReader.setSchema("xml/example.xsd");
153 public void testSetInputTypeError() throws ApexModelException {
154 MockitoAnnotations.initMocks(this);
156 ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
159 Field marshallerField = modelReader.getClass().getDeclaredField("unmarshaller");
160 marshallerField.setAccessible(true);
161 marshallerField.set(modelReader, unmarshallerMock);
162 marshallerField.setAccessible(false);
163 } catch (Exception validationException) {
164 fail("test should not throw an exception");
168 Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(unmarshallerMock)
169 .unmarshal((StreamSource) Mockito.anyObject(), Mockito.anyObject());
171 modelReader.read("{Hello}");
172 fail("Test should throw an exception here");
173 } catch (Exception jaxbe) {
174 assertEquals("Unable to unmarshal Apex concept ", jaxbe.getMessage());
178 Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(unmarshallerMock)
179 .setProperty(Mockito.anyString(), Mockito.anyString());
180 modelReader.read("{Hello}");
181 fail("Test should throw an exception here");
182 } catch (Exception jaxbe) {
183 assertEquals("JAXB error setting unmarshaller for JSON input", jaxbe.getMessage());
187 Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(unmarshallerMock)
188 .setProperty(Mockito.anyString(), Mockito.anyString());
189 modelReader.read("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
190 fail("Test should throw an exception here");
191 } catch (Exception jaxbe) {
192 assertEquals("JAXB error setting unmarshaller for XML input", jaxbe.getMessage());