2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.model.basicmodel.handling;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertTrue;
30 import java.io.BufferedReader;
31 import java.io.ByteArrayInputStream;
32 import java.io.ByteArrayOutputStream;
34 import java.io.FileInputStream;
35 import java.io.FileReader;
36 import java.io.IOException;
37 import java.lang.reflect.Field;
38 import javax.xml.bind.JAXBException;
39 import javax.xml.bind.PropertyException;
40 import javax.xml.bind.Unmarshaller;
41 import javax.xml.transform.stream.StreamSource;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.Mockito;
46 import org.mockito.MockitoAnnotations;
47 import org.mockito.runners.MockitoJUnitRunner;
48 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
51 @RunWith(MockitoJUnitRunner.class)
52 public class ApexModelReaderTest {
54 private Unmarshaller unmarshallerMock;
57 public void testModelReader() throws IOException, ApexException {
58 AxModel model = new DummyApexBasicModelCreator().getModel();
59 AxModel invalidModel = new DummyApexBasicModelCreator().getInvalidModel();
61 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
62 modelWriter.setValidateFlag(true);
63 modelWriter.setJsonOutput(true);
65 ByteArrayOutputStream baos = new ByteArrayOutputStream();
66 modelWriter.write(model, baos);
68 ByteArrayOutputStream baosInvalid = new ByteArrayOutputStream();
69 modelWriter.setValidateFlag(false);
70 modelWriter.write(invalidModel, baosInvalid);
72 ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
74 modelReader.setValidateFlag(true);
75 assertTrue(modelReader.getValidateFlag());
77 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
78 AxModel readModel = modelReader.read(bais);
79 assertEquals(model, readModel);
81 ByteArrayInputStream baisInvalid = new ByteArrayInputStream(baosInvalid.toByteArray());
82 assertThatThrownBy(() -> modelReader.read(baisInvalid))
83 .hasMessageStartingWith("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());
102 assertThatThrownBy(() -> modelReader.read(baisDummy))
103 .hasMessage("format of input for Apex concept is neither JSON nor XML");
104 ByteArrayInputStream nullBais = null;
105 assertThatThrownBy(() -> modelReader.read(nullBais))
106 .hasMessage("concept stream may not be null");
108 assertThatThrownBy(() -> {
109 FileInputStream fis = new FileInputStream(new File("somewhere/over/the/rainbow"));
110 modelReader.read(fis);
111 }).hasMessageContaining("rainbow");
112 final File tempFile = File.createTempFile("Apex", "Dummy");
113 BufferedReader br = new BufferedReader(new FileReader(tempFile));
115 assertThatThrownBy(() -> modelReader.read(br))
116 .hasMessage("Unable to read Apex concept ");
118 modelReader.setSchema(null);
120 final File tempFileA = File.createTempFile("Apex", "Dummy");
121 assertThatThrownBy(() -> modelReader.setSchema(tempFileA.getCanonicalPath()))
122 .hasMessage("Unable to load schema");
124 modelReader.setSchema("xml/example.xsd");
128 public void testSetInputTypeError() throws ApexModelException,
129 NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
130 MockitoAnnotations.initMocks(this);
132 ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
134 Field marshallerField = modelReader.getClass().getDeclaredField("unmarshaller");
135 marshallerField.setAccessible(true);
136 marshallerField.set(modelReader, unmarshallerMock);
137 marshallerField.setAccessible(false);
139 assertThatThrownBy(() -> {
140 Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(unmarshallerMock)
141 .unmarshal((StreamSource) Mockito.anyObject(), Mockito.anyObject());
143 modelReader.read("{Hello}");
144 }).hasMessage("Unable to unmarshal Apex concept ");
145 assertThatThrownBy(() -> {
146 Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(unmarshallerMock)
147 .setProperty(Mockito.anyString(), Mockito.anyString());
148 modelReader.read("{Hello}");
149 }).hasMessage("JAXB error setting unmarshaller for JSON input");
150 assertThatThrownBy(() -> {
151 Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(unmarshallerMock)
152 .setProperty(Mockito.anyString(), Mockito.anyString());
153 modelReader.read("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
154 }).hasMessage("JAXB error setting unmarshaller for XML input");