Changes for checkstyle 8.32
[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  * ================================================================================
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.basicmodel.handling;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.File;
27 import java.io.IOException;
28 import org.junit.Test;
29 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
31
32 public class ApexModelFileWriterTest {
33
34     @Test
35     public void testModelFileWriter() throws IOException, ApexException {
36         ApexModelFileWriter<AxModel> modelFileWriter = new ApexModelFileWriter<>(true);
37
38         modelFileWriter.setValidateFlag(true);
39         assertTrue(modelFileWriter.isValidateFlag());
40
41         File tempFile = File.createTempFile("ApexFileWriterTest", "test");
42         File tempDir = tempFile.getParentFile();
43         tempFile.delete();
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
80         try {
81             modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
82             fail("this test should throw an exception here");
83         } catch (Exception e) {
84             assertTrue(e.getMessage().contains("could not create directory "));
85         }
86
87         try {
88             modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
89             fail("this test should throw an exception here");
90         } catch (Exception e) {
91             assertTrue(e.getMessage().contains("could not create directory "));
92         }
93
94         dirA.delete();
95
96         dirA = new File(tempDir.getAbsolutePath() + "/aaa");
97         File fileB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
98         dirA.mkdir();
99         fileB.createNewFile();
100         
101         jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
102         jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml");
103
104         try {
105             modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
106             fail("this test should throw an exception here");
107         } catch (Exception e) {
108             assertTrue(e.getMessage().contains("error processing file "));
109         }
110
111         try {
112             modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
113             fail("this test should throw an exception here");
114         } catch (Exception e) {
115             assertTrue(e.getMessage().contains("error processing file "));
116         }
117
118         fileB.delete();
119         dirA.delete();
120     }
121 }