2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.uservice.adapt.events.syncasync;
24 import static org.junit.Assert.assertEquals;
27 import java.io.IOException;
29 import org.apache.commons.lang3.StringUtils;
30 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
31 import org.onap.policy.apex.service.engine.main.ApexMain;
32 import org.onap.policy.common.utils.resources.TextFileUtils;
34 public class BaseEventTest {
35 private static final long TIME_OUT_IN_MS = 10000;
37 private void waitForOutFiles(final String[] expectedFileNames, final long expectedFileSize) throws IOException {
38 final long totalExpectedSize = expectedFileSize * expectedFileNames.length;
40 long startWaitTime = System.currentTimeMillis();
41 long lastTotalSize = 0;
46 for (String expectedFileName : expectedFileNames) {
47 totalSize += getEventCount(expectedFileName);
50 if (totalSize >= totalExpectedSize) {
54 // We're making progress, extend the timeout
55 if (totalSize > lastTotalSize) {
56 lastTotalSize = totalSize;
57 startWaitTime = System.currentTimeMillis();
60 ThreadUtilities.sleep(100);
62 while (TIME_OUT_IN_MS >= System.currentTimeMillis() - startWaitTime);
65 private int getEventCount(final String expectedFileName) throws IOException {
66 File expectedFile = new File(expectedFileName);
68 if (!expectedFile.exists()) {
72 String expectedFileContents = TextFileUtils.getTextFileAsString(expectedFileName);
74 return StringUtils.countMatches(expectedFileContents, "{");
77 protected void testFileEvents(final String[] args, final String[] expectedFileNames, final long expectedFileSize)
79 final ApexMain apexMain = new ApexMain(args);
81 waitForOutFiles(expectedFileNames, expectedFileSize);
85 for (final String expectedFileName : expectedFileNames) {
86 assertEquals(expectedFileSize, getEventCount(expectedFileName));