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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.testsuites.integration.uservice.adapt.file;
23 import static org.junit.Assert.assertEquals;
26 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.model.utilities.TextFileUtils;
34 import org.onap.policy.apex.service.engine.main.ApexMain;
36 public class TestFile2FileFiltered {
38 * Clear relative file root environment variable.
41 public void clearRelativeFileRoot() {
42 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
46 public void testJsonFilteredFileInOutEvents() throws MessagingException, ApexException, IOException {
48 { "-rfr", "target", "-c", "target/examples/config/SampleDomain/File2FileFilteredInOutJsonEvent.json" };
50 final String[] outFilePaths =
51 { "target/examples/events/SampleDomain/Events0004Out.json",
52 "target/examples/events/SampleDomain/Events0104Out.json" };
54 final long[] expectedFileSizes =
57 testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
61 public void testJsonFilteredFileOutEvents() throws MessagingException, ApexException, IOException {
63 { "-rfr", "target", "-c", "target/examples/config/SampleDomain/File2FileFilteredOutJsonEvent.json" };
65 final String[] outFilePaths =
66 { "target/examples/events/SampleDomain/Events0004Out.json",
67 "target/examples/events/SampleDomain/Events0104Out.json" };
69 final long[] expectedFileSizes =
72 testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
76 public void testJsonFilteredFileInEvents() throws MessagingException, ApexException, IOException {
78 { "-rfr", "target", "-c", "target/examples/config/SampleDomain/File2FileFilteredInJsonEvent.json" };
80 final String[] outFilePaths =
81 { "target/examples/events/SampleDomain/Events0004Out.json" };
83 final long[] expectedFileSizes =
86 testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
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);
93 final File outFile0 = new File(outFilePaths[0]);
95 while (!outFile0.exists()) {
96 ThreadUtilities.sleep(500);
99 // Wait for the file to be filled
100 long outFile0Size = 0;
101 for (int i = 0; i < 4; i++) {
102 final String fileString = stripVariableLengthText(outFilePaths[0]);
103 outFile0Size = fileString.length();
104 if (outFile0Size > 0 && outFile0Size >= expectedFileSizes[0]) {
107 ThreadUtilities.sleep(500);
110 ThreadUtilities.sleep(500);
113 final long[] actualFileSizes = new long[expectedFileSizes.length];
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();
121 for (int i = 0; i < actualFileSizes.length; i++) {
122 assertEquals(expectedFileSizes[i], actualFileSizes[i]);
127 * Strip variable length text from file string.
129 * @param textFileAsString the file to read and strip
130 * @return the stripped string
131 * @throws IOException on out file read exceptions
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>");