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.ByteArrayOutputStream;
29 import java.io.IOException;
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;
36 public class ApexModelWriterTest {
39 public void testModelWriter() throws IOException, ApexException {
40 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
42 modelWriter.setValidateFlag(true);
43 assertTrue(modelWriter.getValidateFlag());
44 assertEquals(0, modelWriter.getCDataFieldSet().size());
46 assertFalse(modelWriter.isJsonOutput());
47 modelWriter.setJsonOutput(true);
48 assertTrue(modelWriter.isJsonOutput());
49 modelWriter.setJsonOutput(false);
50 assertFalse(modelWriter.isJsonOutput());
52 ByteArrayOutputStream baos = new ByteArrayOutputStream();
54 AxModel model = new DummyApexBasicModelCreator().getModel();
56 modelWriter.write(model, baos);
57 modelWriter.setJsonOutput(true);
58 modelWriter.write(model, baos);
59 modelWriter.setJsonOutput(false);
61 modelWriter.setValidateFlag(false);
62 modelWriter.write(model, baos);
63 modelWriter.setJsonOutput(true);
64 modelWriter.write(model, baos);
65 modelWriter.setJsonOutput(false);
67 modelWriter.setValidateFlag(true);
68 model.getKeyInformation().getKeyInfoMap().clear();
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));
75 model.getKeyInformation().generateKeyInfo(model);
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());
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());