2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020,2022 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 org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
43 @RunWith(MockitoJUnitRunner.class)
44 public class ApexModelReaderTest {
46 public void testModelReader() throws IOException, ApexException {
47 AxModel model = new DummyApexBasicModelCreator().getModel();
48 AxModel invalidModel = new DummyApexBasicModelCreator().getInvalidModel();
50 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
51 modelWriter.setValidate(true);
53 ByteArrayOutputStream baos = new ByteArrayOutputStream();
54 modelWriter.write(model, baos);
56 ByteArrayOutputStream baosInvalid = new ByteArrayOutputStream();
57 modelWriter.setValidate(false);
58 modelWriter.write(invalidModel, baosInvalid);
60 ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
62 modelReader.setValidate(true);
63 assertTrue(modelReader.isValidate());
65 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
66 AxModel readModel = modelReader.read(bais);
67 assertEquals(model, readModel);
69 ByteArrayInputStream baisInvalid = new ByteArrayInputStream(baosInvalid.toByteArray());
70 assertThatThrownBy(() -> modelReader.read(baisInvalid))
71 .hasMessageStartingWith("Apex concept validation failed");
72 modelReader.setValidate(false);
73 assertFalse(modelReader.isValidate());
75 ByteArrayInputStream bais2 = new ByteArrayInputStream(baos.toByteArray());
76 AxModel readModel2 = modelReader.read(bais2);
77 assertEquals(model, readModel2);
79 ByteArrayOutputStream baosJson = new ByteArrayOutputStream();
80 modelWriter.write(model, baosJson);
82 ByteArrayInputStream baisJson = new ByteArrayInputStream(baosJson.toByteArray());
83 AxModel readModelJson = modelReader.read(baisJson);
84 assertEquals(model, readModelJson);
86 String dummyString = "SomeDummyText";
87 ByteArrayInputStream baisDummy = new ByteArrayInputStream(dummyString.getBytes());
88 assertThatThrownBy(() -> modelReader.read(baisDummy))
89 .hasMessageContaining("Unable to unmarshal Apex concept");
90 ByteArrayInputStream nullBais = null;
91 assertThatThrownBy(() -> modelReader.read(nullBais))
92 .hasMessage("concept stream may not be null");
94 assertThatThrownBy(() -> {
95 FileInputStream fis = new FileInputStream(new File("somewhere/over/the/rainbow"));
96 modelReader.read(fis);
97 }).hasMessageContaining("rainbow");
98 final File tempFile = File.createTempFile("Apex", "Dummy");
99 BufferedReader br = new BufferedReader(new FileReader(tempFile));
101 assertThatThrownBy(() -> modelReader.read(br))
102 .hasMessage("Unable to read Apex concept ");