2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020,2022 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.setValidate(true);
40 assertTrue(modelFileWriter.isValidate());
42 File tempFile = File.createTempFile("ApexFileWriterTest", "test");
43 File tempDir = tempFile.getParentFile();
45 File jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/ApexFileWriterTest.json");
47 AxModel model = new DummyApexBasicModelCreator().getModel();
49 modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
51 jsonTempFile.delete();
52 new File(tempDir.getAbsolutePath() + "/aaa").delete();
53 new File(tempDir.getAbsolutePath() + "/ccc").delete();
55 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
57 modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
59 jsonTempFile.delete();
61 new File(tempDir.getAbsolutePath() + "/aaa/bbb").delete();
62 new File(tempDir.getAbsolutePath() + "/aaa").delete();
63 new File(tempDir.getAbsolutePath() + "/ccc/ddd").delete();
64 new File(tempDir.getAbsolutePath() + "/ccc").delete();
66 File dirA = new File(tempDir.getAbsolutePath() + "/aaa");
67 // File dirB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
69 // dirB.createNewFile();
71 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
72 final File jsonTempFile01 = jsonTempFile;
74 () -> modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile01.getAbsolutePath()))
75 .hasMessageContaining("could not create directory");
79 dirA = new File(tempDir.getAbsolutePath() + "/aaa");
80 File fileB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
82 fileB.createNewFile();
84 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
86 File jsonTempFile02 = jsonTempFile;
88 () -> modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile02.getAbsolutePath()))
89 .hasMessageContaining("error processing file");