7c0ccc7d13db33e34a407664d6721d891983e686
[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
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 import org.onap.policy.apex.model.basicmodel.handling.ApexModelFileWriter;
33
34 public class ApexModelFileWriterTest {
35
36     @Test
37     public void testModelFileWriter() throws IOException, ApexException {
38         ApexModelFileWriter<AxModel> modelFileWriter = new ApexModelFileWriter<>(true);
39
40         modelFileWriter.setValidateFlag(true);
41         assertTrue(modelFileWriter.isValidateFlag());
42
43         File tempFile = File.createTempFile("ApexFileWriterTest", "test");
44         File tempDir = tempFile.getParentFile();
45         tempFile.delete();
46         
47         File jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/ApexFileWriterTest.json");
48         File xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ApexFileWriterTest.xml");
49         
50         AxModel model = new DummyApexBasicModelCreator().getModel();
51
52         modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
53         modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, xmlTempFile.getAbsolutePath());
54         
55         jsonTempFile.delete();
56         xmlTempFile.delete();
57         new File(tempDir.getAbsolutePath() + "/aaa").delete();
58         new File(tempDir.getAbsolutePath() + "/ccc").delete();
59        
60         jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
61         xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ddd/ApexFileWriterTest.xml");
62         
63         modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
64         modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, xmlTempFile.getAbsolutePath());
65                 
66         jsonTempFile.delete();
67         xmlTempFile.delete();
68
69         new File(tempDir.getAbsolutePath() + "/aaa/bbb").delete();
70         new File(tempDir.getAbsolutePath() + "/aaa").delete();
71         new File(tempDir.getAbsolutePath() + "/ccc/ddd").delete();
72         new File(tempDir.getAbsolutePath() + "/ccc").delete();
73         
74         File dirA = new File(tempDir.getAbsolutePath() + "/aaa");
75         //File dirB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
76         dirA.createNewFile();
77         //dirB.createNewFile();
78         
79         jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
80         jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml");
81
82         try {
83             modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
84             fail("this test should throw an exception here");
85         }
86         catch (Exception e) {
87             assertTrue(e.getMessage().contains("could not create directory "));
88         }
89
90         try {
91             modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
92             fail("this test should throw an exception here");
93         }
94         catch (Exception e) {
95             assertTrue(e.getMessage().contains("could not create directory "));
96         }
97
98         dirA.delete();
99
100         dirA = new File(tempDir.getAbsolutePath() + "/aaa");
101         File fileB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
102         dirA.mkdir();
103         fileB.createNewFile();
104         
105         jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
106         jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml");
107
108         try {
109             modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
110             fail("this test should throw an exception here");
111         }
112         catch (Exception e) {
113             assertTrue(e.getMessage().contains("error processing file "));
114         }
115
116         try {
117             modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
118             fail("this test should throw an exception here");
119         }
120         catch (Exception e) {
121             assertTrue(e.getMessage().contains("error processing file "));
122         }
123
124         fileB.delete();
125         dirA.delete();
126     }
127 }