709c67c875e44531bea465e9577c1bec2beb2d42
[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.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
31 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
33 import org.onap.policy.apex.model.utilities.TextFileUtils;
34 import org.onap.policy.apex.service.engine.main.ApexMain;
35
36 public class TestFile2File {
37     /**
38      * Clear relative file root environment variable.
39      */
40     @Before
41     public void clearRelativeFileRoot() {
42         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
43     }
44
45     @Test
46     public void testJsonFileEvents() throws MessagingException, ApexException, IOException {
47         final String[] args = {"-rfr", "target", "-c", "target/examples/config/SampleDomain/File2FileJsonEvent.json"};
48
49         testFileEvents(args, "target/examples/events/SampleDomain/EventsOut.json", 42200);
50     }
51
52     @Test
53     public void testXmlFileEvents() throws MessagingException, ApexException, IOException {
54         final String[] args = {"-rfr", "target", "-c", "target/examples/config/SampleDomain/File2FileXmlEvent.json"};
55
56         testFileEvents(args, "target/examples/events/SampleDomain/EventsOut.xmlfile", 100000);
57     }
58
59     private void testFileEvents(final String[] args, final String outFilePath, final long expectedFileSize)
60             throws MessagingException, ApexException, IOException {
61         final ApexMain apexMain = new ApexMain(args);
62
63         final File outFile = new File(outFilePath);
64
65         while (!outFile.exists()) {
66             ThreadUtilities.sleep(500);
67         }
68
69         // Wait for the file to be filled
70         long outFileSize = 0;
71         while (true) {
72             final String fileString = stripVariableLengthText(outFilePath);
73             outFileSize = fileString.length();
74             if (outFileSize > 0 && outFileSize >= expectedFileSize) {
75                 break;
76             }
77             ThreadUtilities.sleep(500);
78         }
79
80         apexMain.shutdown();
81         assertEquals(expectedFileSize, outFileSize);
82     }
83
84     /**
85      * Strip variable length text from file string.
86      * 
87      * @param textFileAsString the file to read and strip
88      * @return the stripped string
89      * @throws IOException on out file read exceptions
90      */
91     private String stripVariableLengthText(final String outFile) throws IOException {
92         return TextFileUtils.getTextFileAsString(outFile)
93                         .replaceAll("\\s+", "")
94                         .replaceAll(":\\d*\\.?\\d*,", ":0,")
95                         .replaceAll(":\\d*}", ":0}")
96                         .replaceAll("<value>\\d*\\.?\\d*</value>", "<value>0</value>");
97     }
98 }
99
100