Changes for checkstyle 8.32
[policy/apex-pdp.git] / testsuites / integration / integration-uservice-test / src / test / java / org / onap / policy / apex / testsuites / integration / uservice / adapt / file / TestFile2FileFiltered.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 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.service.engine.main.ApexMain;
34 import org.onap.policy.common.utils.resources.TextFileUtils;
35
36 public class TestFile2FileFiltered {
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 testJsonFilteredFileInOutEvents() throws MessagingException, ApexException, IOException {
47         // @formatter:off
48         final String[] args =
49             { "-rfr", "target", "-c", "target/examples/config/SampleDomain/File2FileFilteredInOutJsonEvent.json" };
50
51         final String[] outFilePaths =
52             { "target/examples/events/SampleDomain/Events0004Out.json",
53                 "target/examples/events/SampleDomain/Events0104Out.json" };
54
55         final long[] expectedFileSizes =
56             { 22366, 19834 };
57
58         testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
59         // @formatter:on
60     }
61
62     @Test
63     public void testJsonFilteredFileOutEvents() throws MessagingException, ApexException, IOException {
64         // @formatter:off
65         final String[] args =
66             { "-rfr", "target", "-c", "target/examples/config/SampleDomain/File2FileFilteredOutJsonEvent.json" };
67
68         final String[] outFilePaths =
69             { "target/examples/events/SampleDomain/Events0004Out.json",
70                 "target/examples/events/SampleDomain/Events0104Out.json" };
71
72         final long[] expectedFileSizes =
73             { 22366, 19834 };
74
75         testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
76         // @formatter:on
77     }
78
79     @Test
80     public void testJsonFilteredFileInEvents() throws MessagingException, ApexException, IOException {
81         // @formatter:off
82         final String[] args =
83             { "-rfr", "target", "-c", "target/examples/config/SampleDomain/File2FileFilteredInJsonEvent.json" };
84
85         final String[] outFilePaths =
86             { "target/examples/events/SampleDomain/Events0004Out.json" };
87
88         final long[] expectedFileSizes =
89             { 22366 };
90
91         testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
92         // @formatter:on
93     }
94
95     private void testFilteredFileEvents(final String[] args, final String[] outFilePaths,
96             final long[] expectedFileSizes) throws MessagingException, ApexException, IOException {
97         final ApexMain apexMain = new ApexMain(args);
98
99         final File outFile0 = new File(outFilePaths[0]);
100
101         while (!outFile0.exists()) {
102             ThreadUtilities.sleep(500);
103         }
104
105         // Wait for the file to be filled
106         long outFile0Size = 0;
107         for (int i = 0; i < 20; i++) {
108             final String fileString = stripVariableLengthText(outFilePaths[0]);
109             outFile0Size = fileString.length();
110             if (outFile0Size > 0 && outFile0Size >= expectedFileSizes[0]) {
111                 break;
112             }
113             ThreadUtilities.sleep(500);
114         }
115
116         ThreadUtilities.sleep(500);
117         apexMain.shutdown();
118
119         final long[] actualFileSizes = new long[expectedFileSizes.length];
120
121         for (int i = 0; i < outFilePaths.length; i++) {
122             final String fileString = stripVariableLengthText(outFilePaths[i]);
123             actualFileSizes[i] = fileString.length();
124             new File(outFilePaths[i]).delete();
125         }
126
127         for (int i = 0; i < actualFileSizes.length; i++) {
128             assertEquals(expectedFileSizes[i], actualFileSizes[i]);
129         }
130     }
131
132     /**
133      * Strip variable length text from file string.
134      *
135      * @param textFileAsString the file to read and strip
136      * @return the stripped string
137      * @throws IOException on out file read exceptions
138      */
139     private String stripVariableLengthText(final String outFile) throws IOException {
140         return TextFileUtils.getTextFileAsString(outFile).replaceAll("\\s+", "").replaceAll(":\\d*\\.?\\d*,", ":0,")
141                 .replaceAll(":\\d*}", ":0}").replaceAll("<value>\\d*\\.?\\d*</value>", "<value>0</value>");
142     }
143 }