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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.basicmodel.handling;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
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;
32 public class ApexModelFileWriterTest {
35 public void testModelFileWriter() throws IOException, ApexException {
36 ApexModelFileWriter<AxModel> modelFileWriter = new ApexModelFileWriter<>(true);
38 modelFileWriter.setValidateFlag(true);
39 assertTrue(modelFileWriter.isValidateFlag());
41 File tempFile = File.createTempFile("ApexFileWriterTest", "test");
42 File tempDir = tempFile.getParentFile();
45 File jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/ApexFileWriterTest.json");
46 File xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ApexFileWriterTest.xml");
48 AxModel model = new DummyApexBasicModelCreator().getModel();
50 modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
51 modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, xmlTempFile.getAbsolutePath());
53 jsonTempFile.delete();
55 new File(tempDir.getAbsolutePath() + "/aaa").delete();
56 new File(tempDir.getAbsolutePath() + "/ccc").delete();
58 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
59 xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ddd/ApexFileWriterTest.xml");
61 modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath());
62 modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, xmlTempFile.getAbsolutePath());
64 jsonTempFile.delete();
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();
72 File dirA = new File(tempDir.getAbsolutePath() + "/aaa");
73 //File dirB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
75 //dirB.createNewFile();
77 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
78 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml");
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 "));
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 "));
96 dirA = new File(tempDir.getAbsolutePath() + "/aaa");
97 File fileB = new File(tempDir.getAbsolutePath() + "/aaa/bbb");
99 fileB.createNewFile();
101 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json");
102 jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml");
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 "));
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 "));