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.assertTrue;
24 import static org.junit.Assert.fail;
27 import java.io.IOException;
29 import org.junit.Test;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
32 import org.onap.policy.apex.model.basicmodel.handling.ApexModelFileWriter;
35 * @author Liam Fallon (liam.fallon@ericsson.com)
37 public class TestModelFileWriter {
40 public void testModelFileWriter() throws IOException, ApexException {
41 AxModel model = new TestApexBasicModelCreator().getModel();
43 ApexModelFileWriter<AxModel> modelFileWriter = new ApexModelFileWriter<>(true);
45 modelFileWriter.setValidateFlag(true);
46 assertTrue(modelFileWriter.isValidateFlag());
48 File tempFile = File.createTempFile("ApexFileWriterTest", "test");
49 File tempDir = tempFile.getParentFile();
52 File jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/ApexFileWriterTest.json");
53 File xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ApexFileWriterTest.xml");
55 modelFileWriter.apexModelWriteJSONFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
57 modelFileWriter.apexModelWriteXMLFile(model, AxModel.class, xmlTempFile.getAbsolutePath());
59 jsonTempFile.delete();
61 new File(tempDir.getAbsolutePath() + "/aaa").delete();
62 new File(tempDir.getAbsolutePath() + "/ccc").delete();
64 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
65 xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ddd/ApexFileWriterTest.xml");
67 modelFileWriter.apexModelWriteJSONFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
68 modelFileWriter.apexModelWriteXMLFile(model, AxModel.class, xmlTempFile.getAbsolutePath());
70 jsonTempFile.delete();
73 new File(tempDir.getAbsolutePath() + "/aaa/bbb").delete();
74 new File(tempDir.getAbsolutePath() + "/aaa").delete();
75 new File(tempDir.getAbsolutePath() + "/ccc/ddd").delete();
76 new File(tempDir.getAbsolutePath() + "/ccc").delete();
78 File dirA = new File(tempDir.getAbsolutePath() + "/aaa");
79 //File dirB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
81 //dirB.createNewFile();
83 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
84 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml");
87 modelFileWriter.apexModelWriteJSONFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
88 fail("this test should throw an exception here");
91 assertTrue(e.getMessage().contains("could not create directory "));
95 modelFileWriter.apexModelWriteXMLFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
96 fail("this test should throw an exception here");
99 assertTrue(e.getMessage().contains("could not create directory "));
104 dirA = new File(tempDir.getAbsolutePath() + "/aaa");
105 File fileB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
107 fileB.createNewFile();
109 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
110 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml");
113 modelFileWriter.apexModelWriteJSONFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
114 fail("this test should throw an exception here");
116 catch (Exception e) {
117 assertTrue(e.getMessage().contains("error processing file "));
121 modelFileWriter.apexModelWriteXMLFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
122 fail("this test should throw an exception here");
124 catch (Exception e) {
125 assertTrue(e.getMessage().contains("error processing file "));