36c64e517556443f574446448b704b269aa0b35f
[policy/apex-pdp.git] / tools / tools-common / src / test / java / org / onap / policy / apex / tools / common / OutputFileTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (c) 2020 Nordix Foundation.
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 package org.onap.policy.apex.tools.common;
21
22 import static org.junit.Assert.*;
23
24 import java.io.File;
25 import java.nio.file.FileSystems;
26 import java.nio.file.Path;
27
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31
32 public class OutputFileTest {
33
34     final String testFileName = "testing.txt";
35     final Path fp = FileSystems.getDefault().getPath(testFileName);
36     File file = fp.toFile();
37
38     @Before
39     public void setUp() {
40         if (file.exists()){
41             file.delete();
42         }
43     }
44
45     @Test
46     public void testToWriter() {
47         OutputFile testFile = new OutputFile(testFileName,false);
48         testFile.validate();
49         file.setReadable(false);
50         file.setWritable(false);
51         assertNull(testFile.toWriter());
52         file.setWritable(true);
53         assertNotNull(testFile.toWriter());
54     }
55
56     @Test
57     public void testValidate() {
58         OutputFile testFile = new OutputFile(testFileName,true);
59         assertNull(testFile.validate());
60         file.setReadable(false);
61         file.setWritable(false);
62         assertNotNull(testFile.validate());
63         OutputFile testFile2 = new OutputFile(testFileName);
64         assertNotNull(testFile2.validate());
65         assertEquals("file already exists",testFile2.validate());
66     }
67
68     @Test
69     public void testToOutputStream() {
70         OutputFile testFile = new OutputFile(testFileName,true);
71         assertNotNull(testFile.toOutputStream());
72         file.setReadable(false);
73         file.setWritable(false);
74         assertNull(testFile.toOutputStream());
75     }
76
77     @After
78     public void testDown() {
79         if (file.exists()){
80             file.delete();
81         }
82     }
83 }