de14b7f97b1147f649a70698bc498c328cef0003
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020-2021, 2024 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.uservice.adapt.file;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.File;
28 import java.io.IOException;
29 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
31 import org.onap.policy.apex.service.engine.main.ApexMain;
32 import org.onap.policy.common.utils.resources.TextFileUtils;
33
34 /**
35  * The Class TestFile2FileIgnore.
36  */
37 public class TestFile2FileIgnore {
38     // This test is used just to bring up an instance of Apex for manual testing and demonstrations
39     // It should always be ignored in automated testing because it holds Apex up for a very long
40     // time
41
42     /**
43      * The main method.
44      *
45      * @param args the arguments
46      * @throws ApexException the apex exception
47      * @throws IOException Signals that an I/O exception has occurred.
48      */
49     public static void main(final String[] args) throws ApexException, IOException {
50         final String[] apexArgs = {"-rfr", "target", "-c", "examples/config/SampleDomain/File2FileJsonEvent.json"};
51
52         testFileEvents(apexArgs);
53     }
54
55     /**
56      * Test file events.
57      *
58      * @param args the args
59      * @throws ApexException the apex exception
60      * @throws IOException   Signals that an I/O exception has occurred.
61      */
62     private static void testFileEvents(final String[] args)
63         throws ApexException, IOException {
64         final ApexMain apexMain = new ApexMain(args);
65
66         final File outFile = new File("target/EventsOut.json");
67
68         while (!outFile.exists()) {
69             ThreadUtilities.sleep(500);
70         }
71
72         // Wait for the file to be filled
73         long outFileSize;
74         while (true) {
75             final String fileString = stripVariableLengthText();
76             outFileSize = fileString.length();
77             if (outFileSize > 0 && outFileSize >= (long) 48656) {
78                 break;
79             }
80             ThreadUtilities.sleep(500);
81         }
82
83         // Here's the long time I was talking about above!
84         ThreadUtilities.sleep(100000000);
85
86         apexMain.shutdown();
87         assertTrue(outFile.delete());
88         assertEquals(48656, outFileSize);
89     }
90
91     /**
92      * Strip variable length text from file string.
93      *
94      * @return the stripped string
95      * @throws IOException on out file read exceptions
96      */
97     private static String stripVariableLengthText() throws IOException {
98         return TextFileUtils.getTextFileAsString("target/EventsOut.json")
99             .replaceAll("\\s+", "").replaceAll(":\\d*\\.?\\d*,", ":0,")
100             .replaceAll(":\\d*}", ":0}").replaceAll("<value>\\d*\\.?\\d*</value>", "<value>0</value>");
101     }
102 }