0f8f956abe897a0e42ac06386eb1717ab7172ac7
[policy/apex-pdp.git] /
1 /*
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.basicmodel.handling;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.File;
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;
32
33 public class ApexModelFileWriterTest {
34
35     @Test
36     public void testModelFileWriter() throws IOException, ApexException {
37         ApexModelFileWriter<AxModel> modelFileWriter = new ApexModelFileWriter<>(true);
38
39         modelFileWriter.setValidate(true);
40         assertTrue(modelFileWriter.isValidate());
41
42         File tempFile = File.createTempFile("ApexFileWriterTest", "test");
43         File tempDir = tempFile.getParentFile();
44
45         File jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/ApexFileWriterTest.json");
46
47         AxModel model = new DummyApexBasicModelCreator().getModel();
48
49         modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
50
51         jsonTempFile.delete();
52         new File(tempDir.getAbsolutePath() + "/aaa").delete();
53         new File(tempDir.getAbsolutePath() + "/ccc").delete();
54
55         jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
56
57         modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
58
59         jsonTempFile.delete();
60
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();
65
66         File dirA = new File(tempDir.getAbsolutePath() + "/aaa");
67         // File dirB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
68         dirA.createNewFile();
69         // dirB.createNewFile();
70
71         jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
72         final File jsonTempFile01 = jsonTempFile;
73         assertThatThrownBy(
74             () -> modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile01.getAbsolutePath()))
75                 .hasMessageContaining("could not create directory");
76
77         dirA.delete();
78
79         dirA = new File(tempDir.getAbsolutePath() + "/aaa");
80         File fileB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
81         dirA.mkdir();
82         fileB.createNewFile();
83
84         jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
85
86         File jsonTempFile02 = jsonTempFile;
87         assertThatThrownBy(
88             () -> modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile02.getAbsolutePath()))
89                 .hasMessageContaining("error processing file");
90
91         fileB.delete();
92         dirA.delete();
93     }
94 }