2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.basicmodel.handling;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertTrue;
28 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;
33 public class ApexModelFileWriterTest {
36 public void testModelFileWriter() throws IOException, ApexException {
37 ApexModelFileWriter<AxModel> modelFileWriter = new ApexModelFileWriter<>(true);
39 modelFileWriter.setValidateFlag(true);
40 assertTrue(modelFileWriter.isValidateFlag());
42 File tempFile = File.createTempFile("ApexFileWriterTest", "test");
43 File tempDir = tempFile.getParentFile();
45 File jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/ApexFileWriterTest.json");
46 File xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ApexFileWriterTest.xml");
48 AxModel model = new DummyApexBasicModelCreator().getModel();
50 modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
51 modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, xmlTempFile.getAbsolutePath());
53 jsonTempFile.delete();
55 new File(tempDir.getAbsolutePath() + "/aaa").delete();
56 new File(tempDir.getAbsolutePath() + "/ccc").delete();
58 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
59 xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ddd/ApexFileWriterTest.xml");
61 modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
62 modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, xmlTempFile.getAbsolutePath());
64 jsonTempFile.delete();
67 new File(tempDir.getAbsolutePath() + "/aaa/bbb").delete();
68 new File(tempDir.getAbsolutePath() + "/aaa").delete();
69 new File(tempDir.getAbsolutePath() + "/ccc/ddd").delete();
70 new File(tempDir.getAbsolutePath() + "/ccc").delete();
72 File dirA = new File(tempDir.getAbsolutePath() + "/aaa");
73 //File dirB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
75 //dirB.createNewFile();
77 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
78 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml");
79 final File jsonTempFile01 = jsonTempFile;
80 assertThatThrownBy(() -> modelFileWriter.apexModelWriteJsonFile(model, AxModel.class,
81 jsonTempFile01.getAbsolutePath()))
82 .hasMessageContaining("could not create directory");
84 assertThatThrownBy(() -> modelFileWriter.apexModelWriteXmlFile(model, AxModel.class,
85 jsonTempFile01.getAbsolutePath()))
86 .hasMessageContaining("could not create directory");
90 dirA = new File(tempDir.getAbsolutePath() + "/aaa");
91 File fileB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
93 fileB.createNewFile();
95 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
96 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml");
98 File jsonTempFile02 = jsonTempFile;
99 assertThatThrownBy(() -> modelFileWriter.apexModelWriteJsonFile(model, AxModel.class,
100 jsonTempFile02.getAbsolutePath()))
101 .hasMessageContaining("error processing file");
103 assertThatThrownBy(() -> modelFileWriter.apexModelWriteXmlFile(model, AxModel.class,
104 jsonTempFile02.getAbsolutePath()))
105 .hasMessageContaining("error processing file");