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