d7ff4c08bf1875625361a7a74b0bc660078d0aed
[policy/apex-pdp.git] /
1 /*
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.basicmodel.handling;
22
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;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30
31 import org.junit.Test;
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
34 import org.onap.policy.apex.model.basicmodel.handling.ApexModelWriter;
35
36 public class ApexModelWriterTest {
37
38     @Test
39     public void testModelWriter() throws IOException, ApexException {
40         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
41
42         modelWriter.setValidateFlag(true);
43         assertTrue(modelWriter.getValidateFlag());
44         assertEquals(0, modelWriter.getCDataFieldSet().size());
45
46         assertFalse(modelWriter.isJsonOutput());
47         modelWriter.setJsonOutput(true);
48         assertTrue(modelWriter.isJsonOutput());
49         modelWriter.setJsonOutput(false);
50         assertFalse(modelWriter.isJsonOutput());
51
52         ByteArrayOutputStream baos = new ByteArrayOutputStream();
53
54         AxModel model = new DummyApexBasicModelCreator().getModel();
55
56         modelWriter.write(model, baos);
57         modelWriter.setJsonOutput(true);
58         modelWriter.write(model, baos);
59         modelWriter.setJsonOutput(false);
60
61         modelWriter.setValidateFlag(false);
62         modelWriter.write(model, baos);
63         modelWriter.setJsonOutput(true);
64         modelWriter.write(model, baos);
65         modelWriter.setJsonOutput(false);
66
67         modelWriter.setValidateFlag(true);
68         model.getKeyInformation().getKeyInfoMap().clear();
69         try {
70             modelWriter.write(model, baos);
71             fail("Test should throw an exception here");
72         } catch (Exception e) {
73             assertEquals("Apex concept xml (BasicModel:0.0.1) validation failed", e.getMessage().substring(0, 53));
74         }
75         model.getKeyInformation().generateKeyInfo(model);
76
77         try {
78             modelWriter.write(null, baos);
79             fail("Test should throw an exception here");
80         } catch (Exception e) {
81             assertEquals("concept may not be null", e.getMessage());
82         }
83
84         try {
85             ByteArrayOutputStream nullBaos = null;
86             modelWriter.write(model, nullBaos);
87             fail("Test should throw an exception here");
88         } catch (Exception e) {
89             assertEquals("concept stream may not be null", e.getMessage());
90         }
91     }
92 }