Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / basic-model / src / test / java / org / onap / policy / apex / model / basicmodel / handling / ApexModelReaderTest.java
1 /*
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.basicmodel.handling;
22
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;
27
28 import java.io.BufferedReader;
29 import java.io.ByteArrayInputStream;
30 import java.io.ByteArrayOutputStream;
31 import java.io.File;
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;
46
47 public class ApexModelReaderTest {
48     @Mock
49     private Unmarshaller unmarshallerMock;
50
51     @Test
52     public void testModelReader() throws IOException, ApexException {
53         AxModel model = new DummyApexBasicModelCreator().getModel();
54         AxModel invalidModel = new DummyApexBasicModelCreator().getInvalidModel();
55         
56         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
57         modelWriter.setValidateFlag(true);
58         modelWriter.setJsonOutput(true);
59         
60         ByteArrayOutputStream baos = new ByteArrayOutputStream();
61         modelWriter.write(model, baos);
62         
63         ByteArrayOutputStream baosInvalid = new ByteArrayOutputStream();
64         modelWriter.setValidateFlag(false);
65         modelWriter.write(invalidModel, baosInvalid);
66         
67         ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
68         
69         modelReader.setValidateFlag(true);
70         assertTrue(modelReader.getValidateFlag());
71         
72         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
73         AxModel readModel = modelReader.read(bais);
74         assertEquals(model, readModel);
75         
76         ByteArrayInputStream baisInvalid = new ByteArrayInputStream(baosInvalid.toByteArray());
77         try {
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"));
82         }
83         
84         modelReader.setValidateFlag(false);
85         assertFalse(modelReader.getValidateFlag());
86         
87         ByteArrayInputStream bais2 = new ByteArrayInputStream(baos.toByteArray());
88         AxModel readModel2 = modelReader.read(bais2);
89         assertEquals(model, readModel2);
90         
91         modelWriter.setJsonOutput(false);
92         
93         ByteArrayOutputStream baosXml = new ByteArrayOutputStream();
94         modelWriter.write(model, baosXml);
95         
96         ByteArrayInputStream baisXml = new ByteArrayInputStream(baosXml.toByteArray());
97         AxModel readModelXml = modelReader.read(baisXml);
98         assertEquals(model, readModelXml);
99         
100         String dummyString = "SomeDummyText";
101         ByteArrayInputStream baisDummy = new ByteArrayInputStream(dummyString.getBytes());
102         try {
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());
107         }
108         
109         try {
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());
115         }
116         
117         try {
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"));
123         }
124         
125         File tempFile = File.createTempFile("Apex", "Dummy");
126         try {
127             BufferedReader br = new BufferedReader(new FileReader(tempFile));
128             br.close();
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());
133         } finally {
134             tempFile.delete();
135         }
136         
137         modelReader.setSchema(null);
138         
139         tempFile = File.createTempFile("Apex", "Dummy");
140         try {
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());
145         } finally {
146             tempFile.delete();
147         }
148         
149         modelReader.setSchema("xml/example.xsd");
150     }
151
152     @Test
153     public void testSetInputTypeError() throws ApexModelException {
154         MockitoAnnotations.initMocks(this);
155
156         ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
157
158         try {
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");
165         }
166
167         try {
168             Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(unmarshallerMock)
169                 .unmarshal((StreamSource) Mockito.anyObject(), Mockito.anyObject());
170
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());
175         }
176
177         try {
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());
184         }
185
186         try {
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());
193         }
194     }
195 }