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