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.BufferedReader;
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
33 import java.io.FileInputStream;
34 import java.io.FileReader;
35 import java.io.IOException;
36 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;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.mockito.Mockito;
44 import org.mockito.MockitoAnnotations;
45 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
48 public class ApexModelReaderTest {
50 private Unmarshaller unmarshallerMock;
53 public void testModelReader() throws IOException, ApexException {
54 AxModel model = new DummyApexBasicModelCreator().getModel();
55 AxModel invalidModel = new DummyApexBasicModelCreator().getInvalidModel();
57 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
58 modelWriter.setValidateFlag(true);
59 modelWriter.setJsonOutput(true);
61 ByteArrayOutputStream baos = new ByteArrayOutputStream();
62 modelWriter.write(model, baos);
64 ByteArrayOutputStream baosInvalid = new ByteArrayOutputStream();
65 modelWriter.setValidateFlag(false);
66 modelWriter.write(invalidModel, baosInvalid);
68 ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
70 modelReader.setValidateFlag(true);
71 assertTrue(modelReader.getValidateFlag());
73 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
74 AxModel readModel = modelReader.read(bais);
75 assertEquals(model, readModel);
77 ByteArrayInputStream baisInvalid = new ByteArrayInputStream(baosInvalid.toByteArray());
78 assertThatThrownBy(() -> modelReader.read(baisInvalid))
79 .hasMessageStartingWith("Apex concept validation failed");
80 modelReader.setValidateFlag(false);
81 assertFalse(modelReader.getValidateFlag());
83 ByteArrayInputStream bais2 = new ByteArrayInputStream(baos.toByteArray());
84 AxModel readModel2 = modelReader.read(bais2);
85 assertEquals(model, readModel2);
87 modelWriter.setJsonOutput(false);
89 ByteArrayOutputStream baosXml = new ByteArrayOutputStream();
90 modelWriter.write(model, baosXml);
92 ByteArrayInputStream baisXml = new ByteArrayInputStream(baosXml.toByteArray());
93 AxModel readModelXml = modelReader.read(baisXml);
94 assertEquals(model, readModelXml);
96 String dummyString = "SomeDummyText";
97 ByteArrayInputStream baisDummy = new ByteArrayInputStream(dummyString.getBytes());
98 assertThatThrownBy(() -> modelReader.read(baisDummy))
99 .hasMessage("format of input for Apex concept is neither JSON nor XML");
100 ByteArrayInputStream nullBais = null;
101 assertThatThrownBy(() -> modelReader.read(nullBais))
102 .hasMessage("concept stream may not be null");
104 assertThatThrownBy(() -> {
105 FileInputStream fis = new FileInputStream(new File("somewhere/over/the/rainbow"));
106 modelReader.read(fis);
107 }).hasMessageContaining("rainbow");
108 final File tempFile = File.createTempFile("Apex", "Dummy");
109 BufferedReader br = new BufferedReader(new FileReader(tempFile));
111 assertThatThrownBy(() -> modelReader.read(br))
112 .hasMessage("Unable to read Apex concept ");
114 modelReader.setSchema(null);
116 final File tempFileA = File.createTempFile("Apex", "Dummy");
117 assertThatThrownBy(() -> modelReader.setSchema(tempFileA.getCanonicalPath()))
118 .hasMessage("Unable to load schema");
120 modelReader.setSchema("xml/example.xsd");
124 public void testSetInputTypeError() throws ApexModelException,
125 NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
126 MockitoAnnotations.initMocks(this);
128 ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
130 Field marshallerField = modelReader.getClass().getDeclaredField("unmarshaller");
131 marshallerField.setAccessible(true);
132 marshallerField.set(modelReader, unmarshallerMock);
133 marshallerField.setAccessible(false);
135 assertThatThrownBy(() -> {
136 Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(unmarshallerMock)
137 .unmarshal((StreamSource) Mockito.anyObject(), Mockito.anyObject());
139 modelReader.read("{Hello}");
140 }).hasMessage("Unable to unmarshal Apex concept ");
141 assertThatThrownBy(() -> {
142 Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(unmarshallerMock)
143 .setProperty(Mockito.anyString(), Mockito.anyString());
144 modelReader.read("{Hello}");
145 }).hasMessage("JAXB error setting unmarshaller for JSON input");
146 assertThatThrownBy(() -> {
147 Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(unmarshallerMock)
148 .setProperty(Mockito.anyString(), Mockito.anyString());
149 modelReader.read("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
150 }).hasMessage("JAXB error setting unmarshaller for XML input");