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