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=========================================================
21 package org.onap.policy.apex.testsuites.performance.benchmark.engine.main;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.util.concurrent.TimeUnit;
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;
35 public class BaseTest {
36 protected static final long TIME_OUT_IN_MS = TimeUnit.SECONDS.toMillis(10);
38 protected long getFileLength(final String file, final long expectedFileSize) throws IOException {
39 return getFileLength(10, file, expectedFileSize);
42 protected long getFileLength(final int loopTime, final String file, final long expectedFileSize)
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) {
51 ThreadUtilities.sleep(500);
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) {
62 ThreadUtilities.sleep(500);
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());
73 private void deleteFiles(final File... files) throws IOException {
74 for (final File file : files) {
75 Files.deleteIfExists(file.toPath());
79 private boolean isFilesExist(final File[] files) {
80 for (final File file : files) {
88 protected File[] toFile(final String[] filesPath) {
89 if (filesPath == null || filesPath.length == 0) {
92 final File[] files = new File[filesPath.length];
94 for (int index = 0; index < filesPath.length; index++) {
95 files[index] = new File(filesPath[index]);
101 protected void testFileEvents(final String[] args, final String[] expectedFilesPath, final long expectedFileSize)
103 final ApexMain apexMain = new ApexMain(args);
105 final File[] expectedFiles = toFile(expectedFilesPath);
108 waitForOutFiles(expectedFiles);
109 assertFilesExists(expectedFiles);
111 for (final String filePath : expectedFilesPath) {
112 assertEquals(expectedFileSize, getFileLength(filePath, expectedFileSize));
118 deleteFiles(expectedFiles);