Remove try/catch blocks with assertj - apex-pdp
[policy/apex-pdp.git] / model / basic-model / src / test / java / org / onap / policy / apex / model / basicmodel / handling / ApexModelFileWriterTest.java
1 /*
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
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.setValidateFlag(true);
40         assertTrue(modelFileWriter.isValidateFlag());
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         File xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ApexFileWriterTest.xml");
47
48         AxModel model = new DummyApexBasicModelCreator().getModel();
49
50         modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
51         modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, xmlTempFile.getAbsolutePath());
52
53         jsonTempFile.delete();
54         xmlTempFile.delete();
55         new File(tempDir.getAbsolutePath() + "/aaa").delete();
56         new File(tempDir.getAbsolutePath() + "/ccc").delete();
57
58         jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
59         xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ddd/ApexFileWriterTest.xml");
60
61         modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
62         modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, xmlTempFile.getAbsolutePath());
63
64         jsonTempFile.delete();
65         xmlTempFile.delete();
66
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();
71
72         File dirA = new File(tempDir.getAbsolutePath() + "/aaa");
73         //File dirB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
74         dirA.createNewFile();
75         //dirB.createNewFile();
76
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");
83
84         assertThatThrownBy(() -> modelFileWriter.apexModelWriteXmlFile(model, AxModel.class,
85                 jsonTempFile01.getAbsolutePath()))
86                 .hasMessageContaining("could not create directory");
87
88         dirA.delete();
89
90         dirA = new File(tempDir.getAbsolutePath() + "/aaa");
91         File fileB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
92         dirA.mkdir();
93         fileB.createNewFile();
94
95         jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
96         jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml");
97
98         File jsonTempFile02 = jsonTempFile;
99         assertThatThrownBy(() -> modelFileWriter.apexModelWriteJsonFile(model, AxModel.class,
100                 jsonTempFile02.getAbsolutePath()))
101                 .hasMessageContaining("error processing file");
102
103         assertThatThrownBy(() -> modelFileWriter.apexModelWriteXmlFile(model, AxModel.class,
104                 jsonTempFile02.getAbsolutePath()))
105                 .hasMessageContaining("error processing file");
106
107         fileB.delete();
108         dirA.delete();
109     }
110 }