b3fa512d5d6ebf7ccdf6f34bfb3a40831b67a688
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-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.file;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.File;
26 import java.io.IOException;
27
28 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
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.model.utilities.TextFileUtils;
32 import org.onap.policy.apex.service.engine.main.ApexMain;
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 MessagingException the messaging exception
47      * @throws ApexException the apex exception
48      * @throws IOException Signals that an I/O exception has occurred.
49      */
50     public static void main(final String[] args) throws MessagingException, ApexException, IOException {
51         final String[] apexArgs = {"-rfr", "target", "-c", "examples/config/SampleDomain/File2FileJsonEvent.json"};
52
53         testFileEvents(apexArgs, "src/test/resources/events/EventsOut.json", 48656);
54     }
55
56     /**
57      * Test file events.
58      *
59      * @param args the args
60      * @param outFilePath the out file path
61      * @param expectedFileSize the expected file size
62      * @throws MessagingException the messaging exception
63      * @throws ApexException the apex exception
64      * @throws IOException Signals that an I/O exception has occurred.
65      */
66     private static void testFileEvents(final String[] args, final String outFilePath, final long expectedFileSize)
67             throws MessagingException, ApexException, IOException {
68         final ApexMain apexMain = new ApexMain(args);
69
70         final File outFile = new File(outFilePath);
71
72         while (!outFile.exists()) {
73             ThreadUtilities.sleep(500);
74         }
75
76         // Wait for the file to be filled
77         long outFileSize = 0;
78         while (true) {
79             final String fileString = stripVariableLengthText(outFilePath);
80             outFileSize = fileString.length();
81             if (outFileSize > 0 && outFileSize >= expectedFileSize) {
82                 break;
83             }
84             ThreadUtilities.sleep(500);
85         }
86
87         // Here's the long time I was talking about above!
88         ThreadUtilities.sleep(100000000);
89
90         apexMain.shutdown();
91         outFile.delete();
92         assertEquals(outFileSize, expectedFileSize);
93     }
94     
95
96     /**
97      * Strip variable length text from file string.
98      * 
99      * @param textFileAsString the file to read and strip
100      * @return the stripped string
101      * @throws IOException on out file read exceptions
102      */
103     private static String stripVariableLengthText(final String outFile) throws IOException {
104         return TextFileUtils.getTextFileAsString(outFile)
105                         .replaceAll("\\s+", "")
106                         .replaceAll(":\\d*\\.?\\d*,", ":0,")
107                         .replaceAll(":\\d*}", ":0}")
108                         .replaceAll("<value>\\d*\\.?\\d*</value>", "<value>0</value>");
109     }
110 }
111
112