d38107daf72061409bd3b795223966fa4a51be8f
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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 package org.onap.policy.apex.service.engine.main;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.util.concurrent.TimeUnit;
29
30 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
31 import org.onap.policy.apex.model.utilities.TextFileUtils;
32
33 public class BaseTest {
34     protected final static long TIME_OUT_IN_MS = TimeUnit.SECONDS.toMillis(10);
35
36     protected long getFileLength(final String file, final long expectedFileSize) throws IOException {
37         return getFileLength(10, file, expectedFileSize);
38     }
39
40     protected long getFileLength(final int loopTime, final String file, final long expectedFileSize)
41             throws IOException {
42         long length = 0;
43         for (int i = 0; i < loopTime; i++) {
44             final String fileString = TextFileUtils.getTextFileAsString(file).replaceAll("\\s+", "");
45             length = fileString.length();
46             if (length > 0 && length >= expectedFileSize) {
47                 break;
48             }
49             ThreadUtilities.sleep(500);
50         }
51         return length;
52     }
53
54     protected void waitForOutFiles(final File... files) {
55         final long timeInMillis = System.currentTimeMillis();
56         while (!isFilesExist(files)) {
57             if (System.currentTimeMillis() - timeInMillis > TIME_OUT_IN_MS) {
58                 break;
59             }
60             ThreadUtilities.sleep(500);
61         }
62
63     }
64
65     private void assertFilesExists(final File... files) {
66         for (final File file : files) {
67             assertTrue("Test failed, the output " + file + "event file was not created", file.exists());
68         }
69     }
70
71     private void deleteFiles(final File... files) throws IOException {
72         for (final File file : files) {
73             Files.deleteIfExists(file.toPath());
74         }
75     }
76
77     private boolean isFilesExist(final File[] files) {
78         for (final File file : files) {
79             if (!file.exists()) {
80                 return false;
81             }
82         }
83         return true;
84     }
85
86     protected File[] toFile(final String[] filesPath) {
87         if (filesPath == null || filesPath.length == 0) {
88             return new File[] {};
89         }
90         final File[] files = new File[filesPath.length];
91
92         for (int index = 0; index < filesPath.length; index++) {
93             files[index] = new File(filesPath[index]);
94         }
95
96         return files;
97     }
98
99     protected void testFileEvents(final String[] args, final String[] expectedFilesPath, final long expectedFileSize)
100             throws Exception {
101         final ApexMain apexMain = new ApexMain(args);
102
103         final File[] expectedFiles = toFile(expectedFilesPath);
104         try {
105
106             waitForOutFiles(expectedFiles);
107             assertFilesExists(expectedFiles);
108
109             for (final String filePath : expectedFilesPath) {
110                 assertEquals(expectedFileSize, getFileLength(filePath, expectedFileSize));
111             }
112
113             apexMain.shutdown();
114
115         } finally {
116             deleteFiles(expectedFiles);
117         }
118     }
119
120 }