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