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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.basicmodel.handling;
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;
28 import java.io.BufferedReader;
29 import java.io.ByteArrayInputStream;
30 import java.io.ByteArrayOutputStream;
32 import java.io.FileInputStream;
33 import java.io.FileReader;
34 import java.io.IOException;
36 import org.junit.Test;
37 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
39 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
40 import org.onap.policy.apex.model.basicmodel.handling.ApexModelWriter;
42 public class ApexModelReaderTest {
45 public void testModelReader() throws IOException, ApexException {
46 AxModel model = new DummyApexBasicModelCreator().getModel();
47 AxModel invalidModel = new DummyApexBasicModelCreator().getInvalidModel();
49 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
50 modelWriter.setValidateFlag(true);
51 modelWriter.setJsonOutput(true);
53 ByteArrayOutputStream baos = new ByteArrayOutputStream();
54 modelWriter.write(model, baos);
56 ByteArrayOutputStream baosInvalid = new ByteArrayOutputStream();
57 modelWriter.setValidateFlag(false);
58 modelWriter.write(invalidModel, baosInvalid);
60 ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
62 modelReader.setValidateFlag(true);
63 assertTrue(modelReader.getValidateFlag());
65 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
66 AxModel readModel = modelReader.read(bais);
67 assertEquals(model, readModel);
69 ByteArrayInputStream baisInvalid = new ByteArrayInputStream(baosInvalid.toByteArray());
71 modelReader.read(baisInvalid);
72 fail("test should throw an exceptino here");
75 assertTrue(e.getMessage().startsWith("Apex concept validation failed"));
78 modelReader.setValidateFlag(false);
79 assertFalse(modelReader.getValidateFlag());
81 ByteArrayInputStream bais2 = new ByteArrayInputStream(baos.toByteArray());
82 AxModel readModel2 = modelReader.read(bais2);
83 assertEquals(model, readModel2);
85 modelWriter.setJsonOutput(false);
87 ByteArrayOutputStream baosXml = new ByteArrayOutputStream();
88 modelWriter.write(model, baosXml);
90 ByteArrayInputStream baisXml = new ByteArrayInputStream(baosXml.toByteArray());
91 AxModel readModelXml = modelReader.read(baisXml);
92 assertEquals(model, readModelXml);
94 String dummyString = "SomeDummyText";
95 ByteArrayInputStream baisDummy = new ByteArrayInputStream(dummyString.getBytes());
97 modelReader.read(baisDummy);
98 fail("test should throw an exception here");
100 catch (Exception e) {
101 assertEquals("format of input for Apex concept is neither JSON nor XML", e.getMessage());
105 ByteArrayInputStream nullBais = null;
106 modelReader.read(nullBais);
107 fail("test should throw an exception here");
109 catch (Exception e) {
110 assertEquals("concept stream may not be null", e.getMessage());
114 FileInputStream fis = new FileInputStream(new File("somewhere/over/the/rainbow"));
115 modelReader.read(fis);
116 fail("test should throw an exception here");
118 catch (Exception e) {
119 assertTrue(e.getMessage().contains("rainbow"));
122 File tempFile = File.createTempFile("Apex", "Dummy");
124 BufferedReader br = new BufferedReader(new FileReader(tempFile));
126 modelReader.read(br);
127 fail("test should throw an exception here");
129 catch (Exception e) {
130 assertEquals("Unable to read Apex concept ", e.getMessage());
136 modelReader.setSchema(null);
138 tempFile = File.createTempFile("Apex", "Dummy");
140 modelReader.setSchema(tempFile.getCanonicalPath());
141 fail("test should throw an exception here");
143 catch (Exception e) {
144 assertEquals("Unable to load schema", e.getMessage());
150 modelReader.setSchema("xml/example.xsd");