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;
34 public class ApexModelFileWriterTest {
37 public void testModelFileWriter() throws IOException, ApexException {
38 ApexModelFileWriter<AxModel> modelFileWriter = new ApexModelFileWriter<>(true);
40 modelFileWriter.setValidateFlag(true);
41 assertTrue(modelFileWriter.isValidateFlag());
43 File tempFile = File.createTempFile("ApexFileWriterTest", "test");
44 File tempDir = tempFile.getParentFile();
47 File jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/ApexFileWriterTest.json");
48 File xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ApexFileWriterTest.xml");
50 AxModel model = new DummyApexBasicModelCreator().getModel();
52 modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
53 modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, xmlTempFile.getAbsolutePath());
55 jsonTempFile.delete();
57 new File(tempDir.getAbsolutePath() + "/aaa").delete();
58 new File(tempDir.getAbsolutePath() + "/ccc").delete();
60 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
61 xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ddd/ApexFileWriterTest.xml");
63 modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
64 modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, xmlTempFile.getAbsolutePath());
66 jsonTempFile.delete();
69 new File(tempDir.getAbsolutePath() + "/aaa/bbb").delete();
70 new File(tempDir.getAbsolutePath() + "/aaa").delete();
71 new File(tempDir.getAbsolutePath() + "/ccc/ddd").delete();
72 new File(tempDir.getAbsolutePath() + "/ccc").delete();
74 File dirA = new File(tempDir.getAbsolutePath() + "/aaa");
75 //File dirB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
77 //dirB.createNewFile();
79 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
80 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml");
83 modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
84 fail("this test should throw an exception here");
87 assertTrue(e.getMessage().contains("could not create directory "));
91 modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
92 fail("this test should throw an exception here");
95 assertTrue(e.getMessage().contains("could not create directory "));
100 dirA = new File(tempDir.getAbsolutePath() + "/aaa");
101 File fileB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
103 fileB.createNewFile();
105 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
106 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml");
109 modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
110 fail("this test should throw an exception here");
112 catch (Exception e) {
113 assertTrue(e.getMessage().contains("error processing file "));
117 modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
118 fail("this test should throw an exception here");
120 catch (Exception e) {
121 assertTrue(e.getMessage().contains("error processing file "));