2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.testsuites.integration.uservice.adapt.file;
25 import static org.junit.Assert.assertEquals;
28 import java.io.IOException;
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;
37 public class TestFile2FileFiltered {
39 * Clear relative file root environment variable.
42 public void clearRelativeFileRoot() {
43 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
47 public void testJsonFilteredFileInOutEvents() throws MessagingException, ApexException, IOException {
50 { "-rfr", "target", "-p", "target/examples/config/SampleDomain/File2FileFilteredInOutJsonEvent.json" };
52 final String[] outFilePaths =
53 { "target/examples/events/SampleDomain/Events0004Out.json",
54 "target/examples/events/SampleDomain/Events0104Out.json" };
56 final long[] expectedFileSizes =
59 testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
64 public void testJsonFilteredFileOutEvents() throws MessagingException, ApexException, IOException {
67 { "-rfr", "target", "-p", "target/examples/config/SampleDomain/File2FileFilteredOutJsonEvent.json" };
69 final String[] outFilePaths =
70 { "target/examples/events/SampleDomain/Events0004Out.json",
71 "target/examples/events/SampleDomain/Events0104Out.json" };
73 final long[] expectedFileSizes =
76 testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
81 public void testJsonFilteredFileInEvents() throws MessagingException, ApexException, IOException {
84 { "-rfr", "target", "-p", "target/examples/config/SampleDomain/File2FileFilteredInJsonEvent.json" };
86 final String[] outFilePaths =
87 { "target/examples/events/SampleDomain/Events0004Out.json" };
89 final long[] expectedFileSizes =
92 testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
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);
100 final File outFile0 = new File(outFilePaths[0]);
102 while (!outFile0.exists()) {
103 ThreadUtilities.sleep(500);
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]) {
114 ThreadUtilities.sleep(500);
117 ThreadUtilities.sleep(500);
120 final long[] actualFileSizes = new long[expectedFileSizes.length];
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();
128 for (int i = 0; i < actualFileSizes.length; i++) {
129 assertEquals(expectedFileSizes[i], actualFileSizes[i]);
134 * Strip variable length text from file string.
136 * @param textFileAsString the file to read and strip
137 * @return the stripped string
138 * @throws IOException on out file read exceptions
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>");