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