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
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=========================================================
20 package org.onap.policy.apex.service.engine.main;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.util.concurrent.TimeUnit;
30 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
31 import org.onap.policy.apex.model.utilities.TextFileUtils;
33 public class BaseTest {
34 protected final static long TIME_OUT_IN_MS = TimeUnit.SECONDS.toMillis(10);
36 protected long getFileLength(final String file, final long expectedFileSize) throws IOException {
37 return getFileLength(10, file, expectedFileSize);
40 protected long getFileLength(final int loopTime, final String file, final long expectedFileSize)
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) {
49 ThreadUtilities.sleep(500);
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) {
60 ThreadUtilities.sleep(500);
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());
71 private void deleteFiles(final File... files) throws IOException {
72 for (final File file : files) {
73 Files.deleteIfExists(file.toPath());
77 private boolean isFilesExist(final File[] files) {
78 for (final File file : files) {
86 protected File[] toFile(final String[] filesPath) {
87 if (filesPath == null || filesPath.length == 0) {
90 final File[] files = new File[filesPath.length];
92 for (int index = 0; index < filesPath.length; index++) {
93 files[index] = new File(filesPath[index]);
99 protected void testFileEvents(final String[] args, final String[] expectedFilesPath, final long expectedFileSize)
101 final ApexMain apexMain = new ApexMain(args);
103 final File[] expectedFiles = toFile(expectedFilesPath);
106 waitForOutFiles(expectedFiles);
107 assertFilesExists(expectedFiles);
109 for (final String filePath : expectedFilesPath) {
110 assertEquals(expectedFileSize, getFileLength(filePath, expectedFileSize));
116 deleteFiles(expectedFiles);