9199d2a98f79fe240c75f821068bb89ad0d91cbd
[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.integration.uservice.adapt.events.syncasync;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.File;
26 import java.io.IOException;
27
28 import org.apache.commons.lang3.StringUtils;
29 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
30 import org.onap.policy.apex.model.utilities.TextFileUtils;
31 import org.onap.policy.apex.service.engine.main.ApexMain;
32
33 public class BaseEventTest {
34     private static final long TIME_OUT_IN_MS = 10000;
35
36     private void waitForOutFiles(final String[] expectedFileNames, final long expectedFileSize) throws IOException {
37         final long totalExpectedSize = expectedFileSize * expectedFileNames.length;
38
39         long startWaitTime = System.currentTimeMillis();
40         long lastTotalSize = 0;
41         
42         do {
43             long totalSize = 0;
44
45             for (String expectedFileName : expectedFileNames) {
46                 totalSize += getEventCount(expectedFileName);
47             }
48
49             if (totalSize >= totalExpectedSize) {
50                 return;
51             }
52         
53             // We're making progress, extend the timeout
54             if (totalSize > lastTotalSize) {
55                 lastTotalSize = totalSize;
56                 startWaitTime = System.currentTimeMillis();
57             }
58             
59             ThreadUtilities.sleep(100);
60         }
61         while (TIME_OUT_IN_MS >= System.currentTimeMillis() - startWaitTime);
62     }
63
64     private int getEventCount(final String expectedFileName) throws IOException {
65         File expectedFile = new File(expectedFileName);
66         
67         if (!expectedFile.exists()) {
68             return 0;
69         }
70
71         String expectedFileContents = TextFileUtils.getTextFileAsString(expectedFileName);
72         
73         return StringUtils.countMatches(expectedFileContents, "{");
74     }
75
76     protected void testFileEvents(final String[] args, final String[] expectedFileNames, final long expectedFileSize)
77                     throws Exception {
78         final ApexMain apexMain = new ApexMain(args);
79
80         waitForOutFiles(expectedFileNames, expectedFileSize);
81
82         apexMain.shutdown();
83
84         for (final String expectedFileName : expectedFileNames) {
85             assertEquals(expectedFileSize, getEventCount(expectedFileName));
86         }
87     }
88 }