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