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 / ApexModelWriterTest.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.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.Writer;
32 import java.lang.reflect.Field;
33 import javax.xml.bind.JAXBException;
34 import javax.xml.bind.Marshaller;
35 import javax.xml.bind.PropertyException;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.MockitoAnnotations;
40 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
42 import org.w3c.dom.Document;
43
44 public class ApexModelWriterTest {
45     @Mock
46     private Marshaller marshallerMock;
47
48     @Test
49     public void testModelWriter() throws IOException, ApexException {
50         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
51
52         modelWriter.setValidateFlag(true);
53         assertTrue(modelWriter.getValidateFlag());
54         assertEquals(0, modelWriter.getCDataFieldSet().size());
55
56         assertFalse(modelWriter.isJsonOutput());
57         modelWriter.setJsonOutput(true);
58         assertTrue(modelWriter.isJsonOutput());
59         modelWriter.setJsonOutput(false);
60         assertFalse(modelWriter.isJsonOutput());
61
62         ByteArrayOutputStream baos = new ByteArrayOutputStream();
63
64         AxModel model = new DummyApexBasicModelCreator().getModel();
65
66         modelWriter.write(model, baos);
67         modelWriter.setJsonOutput(true);
68         modelWriter.write(model, baos);
69         modelWriter.setJsonOutput(false);
70
71         modelWriter.setValidateFlag(false);
72         modelWriter.write(model, baos);
73         modelWriter.setJsonOutput(true);
74         modelWriter.write(model, baos);
75         modelWriter.setJsonOutput(false);
76
77         modelWriter.setValidateFlag(true);
78         model.getKeyInformation().getKeyInfoMap().clear();
79         assertThatThrownBy(() -> modelWriter.write(model, baos))
80             .hasMessageContaining("Apex concept xml (BasicModel:0.0.1) validation failed");
81         model.getKeyInformation().generateKeyInfo(model);
82
83         assertThatThrownBy(() -> modelWriter.write(null, baos))
84             .hasMessage("concept may not be null");
85
86         ByteArrayOutputStream nullBaos = null;
87         assertThatThrownBy(() -> modelWriter.write(model, nullBaos))
88             .hasMessage("concept stream may not be null");
89     }
90
91     @Test
92     public void testSetOutputTypeError() throws ApexModelException, NoSuchFieldException, SecurityException,
93             IllegalArgumentException, IllegalAccessException, PropertyException {
94         MockitoAnnotations.initMocks(this);
95
96         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
97
98         Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller");
99         marshallerField.setAccessible(true);
100         marshallerField.set(modelWriter, marshallerMock);
101         marshallerField.setAccessible(false);
102         Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock)
103             .setProperty(Mockito.anyString(), Mockito.anyString());
104         assertThatThrownBy(() -> modelWriter.setJsonOutput(true))
105             .hasMessage("JAXB error setting marshaller for JSON output");
106         Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock)
107             .setProperty(Mockito.anyString(), Mockito.anyString());
108         assertThatThrownBy(() -> modelWriter.setJsonOutput(false))
109             .hasMessage("JAXB error setting marshaller for XML output");
110     }
111
112     @Test
113     public void testOutputJsonError() throws ApexModelException, NoSuchFieldException, SecurityException,
114             IllegalArgumentException, IllegalAccessException, JAXBException {
115         MockitoAnnotations.initMocks(this);
116
117         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
118
119         Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller");
120         marshallerField.setAccessible(true);
121         marshallerField.set(modelWriter, marshallerMock);
122         marshallerField.setAccessible(false);
123
124         modelWriter.setValidateFlag(false);
125         modelWriter.setJsonOutput(true);
126
127         ByteArrayOutputStream baos = new ByteArrayOutputStream();
128         AxModel model = new DummyApexBasicModelCreator().getModel();
129         Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock)
130             .marshal((AxModel) Mockito.anyObject(), (Writer) Mockito.anyObject());
131         assertThatThrownBy(() -> modelWriter.write(model, baos)).hasMessage("Unable to marshal Apex concept to JSON");
132     }
133
134     @Test
135     public void testOutputXmlError() throws ApexModelException, NoSuchFieldException, SecurityException,
136             IllegalArgumentException, IllegalAccessException, JAXBException {
137         MockitoAnnotations.initMocks(this);
138
139         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
140         modelWriter.setJsonOutput(false);
141
142         Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller");
143         marshallerField.setAccessible(true);
144         marshallerField.set(modelWriter, marshallerMock);
145         marshallerField.setAccessible(false);
146
147         modelWriter.setValidateFlag(false);
148         modelWriter.setJsonOutput(false);
149
150         ByteArrayOutputStream baos = new ByteArrayOutputStream();
151         AxModel model = new DummyApexBasicModelCreator().getModel();
152
153         Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock)
154             .marshal((AxModel) Mockito.anyObject(), (Document) Mockito.anyObject());
155
156         assertThatThrownBy(() -> modelWriter.write(model, baos))
157             .hasMessage("Unable to marshal Apex concept to XML");
158     }
159 }