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