1152771eaa5eb401fd8431d4cd038c6a30d3ef41
[policy/apex-pdp.git] /
1 /*
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
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.BufferedReader;
31 import java.io.ByteArrayInputStream;
32 import java.io.ByteArrayOutputStream;
33 import java.io.File;
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;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class ApexModelReaderTest {
45     @Test
46     public void testModelReader() throws IOException, ApexException {
47         AxModel model = new DummyApexBasicModelCreator().getModel();
48         AxModel invalidModel = new DummyApexBasicModelCreator().getInvalidModel();
49
50         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
51         modelWriter.setValidate(true);
52
53         ByteArrayOutputStream baos = new ByteArrayOutputStream();
54         modelWriter.write(model, baos);
55
56         ByteArrayOutputStream baosInvalid = new ByteArrayOutputStream();
57         modelWriter.setValidate(false);
58         modelWriter.write(invalidModel, baosInvalid);
59
60         ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true);
61
62         modelReader.setValidate(true);
63         assertTrue(modelReader.isValidate());
64
65         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
66         AxModel readModel = modelReader.read(bais);
67         assertEquals(model, readModel);
68
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());
74
75         ByteArrayInputStream bais2 = new ByteArrayInputStream(baos.toByteArray());
76         AxModel readModel2 = modelReader.read(bais2);
77         assertEquals(model, readModel2);
78
79         ByteArrayOutputStream baosJson = new ByteArrayOutputStream();
80         modelWriter.write(model, baosJson);
81
82         ByteArrayInputStream baisJson = new ByteArrayInputStream(baosJson.toByteArray());
83         AxModel readModelJson = modelReader.read(baisJson);
84         assertEquals(model, readModelJson);
85
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");
93
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));
100         br.close();
101         assertThatThrownBy(() -> modelReader.read(br))
102              .hasMessage("Unable to read Apex concept ");
103         tempFile.delete();
104     }
105 }