Remove try/catch blocks with assertj - apex-pdp
[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  *  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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.basicmodel.handling;
23
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;
28
29 import java.io.BufferedReader;
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.File;
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;
47
48 public class ApexModelReaderTest {
49     @Mock
50     private Unmarshaller unmarshallerMock;
51
52     @Test
53     public void testModelReader() throws IOException, ApexException {
54         AxModel model = new DummyApexBasicModelCreator().getModel();
55         AxModel invalidModel = new DummyApexBasicModelCreator().getInvalidModel();
56
57         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
58         modelWriter.setValidateFlag(true);
59         modelWriter.setJsonOutput(true);
60
61         ByteArrayOutputStream baos = new ByteArrayOutputStream();
62         modelWriter.write(model, baos);
63
64         ByteArrayOutputStream baosInvalid = new ByteArrayOutputStream();
65         modelWriter.setValidateFlag(false);
66         modelWriter.write(invalidModel, baosInvalid);
67
68         ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
69
70         modelReader.setValidateFlag(true);
71         assertTrue(modelReader.getValidateFlag());
72
73         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
74         AxModel readModel = modelReader.read(bais);
75         assertEquals(model, readModel);
76
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());
82
83         ByteArrayInputStream bais2 = new ByteArrayInputStream(baos.toByteArray());
84         AxModel readModel2 = modelReader.read(bais2);
85         assertEquals(model, readModel2);
86
87         modelWriter.setJsonOutput(false);
88
89         ByteArrayOutputStream baosXml = new ByteArrayOutputStream();
90         modelWriter.write(model, baosXml);
91
92         ByteArrayInputStream baisXml = new ByteArrayInputStream(baosXml.toByteArray());
93         AxModel readModelXml = modelReader.read(baisXml);
94         assertEquals(model, readModelXml);
95
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");
103
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));
110         br.close();
111         assertThatThrownBy(() -> modelReader.read(br))
112              .hasMessage("Unable to read Apex concept ");
113         tempFile.delete();
114         modelReader.setSchema(null);
115
116         final File tempFileA = File.createTempFile("Apex", "Dummy");
117         assertThatThrownBy(() -> modelReader.setSchema(tempFileA.getCanonicalPath()))
118             .hasMessage("Unable to load schema");
119         tempFile.delete();
120         modelReader.setSchema("xml/example.xsd");
121     }
122
123     @Test
124     public void testSetInputTypeError() throws ApexModelException,
125         NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
126         MockitoAnnotations.initMocks(this);
127
128         ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
129
130         Field marshallerField = modelReader.getClass().getDeclaredField("unmarshaller");
131         marshallerField.setAccessible(true);
132         marshallerField.set(modelReader, unmarshallerMock);
133         marshallerField.setAccessible(false);
134
135         assertThatThrownBy(() -> {
136             Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(unmarshallerMock)
137                 .unmarshal((StreamSource) Mockito.anyObject(), Mockito.anyObject());
138
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");
151     }
152 }