48cd6c0136d459e1809e5af1ed963e910fc6cd1f
[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.Test;
29 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
30 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
31 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
32 import org.onap.policy.apex.model.utilities.TextFileUtils;
33 import org.onap.policy.apex.service.engine.main.ApexMain;
34
35 public class TestFile2FileFiltered {
36
37     @Test
38     public void testJsonFilteredFileInOutEvents() throws MessagingException, ApexException, IOException {
39         final String[] args = {"src/test/resources/prodcons/File2FileFilteredInOutJsonEvent.json"};
40
41         final String[] outFilePaths = {
42             "src/test/resources/events/Events0004Out.json",
43             "src/test/resources/events/Events0104Out.json"
44         };
45
46         final long[] expectedFileSizes = {25949, 23007};
47
48         testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
49     }
50
51     @Test
52     public void testJsonFilteredFileOutEvents() throws MessagingException, ApexException, IOException {
53         final String[] args = {"src/test/resources/prodcons/File2FileFilteredOutJsonEvent.json"};
54
55         final String[] outFilePaths = {
56             "src/test/resources/events/Events0004Out.json",
57             "src/test/resources/events/Events0104Out.json"
58         };
59
60         final long[] expectedFileSizes = {25949, 23007};
61
62         testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
63     }
64
65     @Test
66     public void testJsonFilteredFileInEvents() throws MessagingException, ApexException, IOException {
67         final String[] args = {"src/test/resources/prodcons/File2FileFilteredInJsonEvent.json"};
68
69         final String[] outFilePaths = {"src/test/resources/events/Events0004Out.json"};
70
71         final long[] expectedFileSizes = {25949};
72
73         testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
74     }
75
76     private void testFilteredFileEvents(final String[] args, final String[] outFilePaths,
77             final long[] expectedFileSizes) throws MessagingException, ApexException, IOException {
78         final ApexMain apexMain = new ApexMain(args);
79
80         final File outFile0 = new File(outFilePaths[0]);
81
82         while (!outFile0.exists()) {
83             ThreadUtilities.sleep(500);
84         }
85
86         // Wait for the file to be filled
87         long outFile0Size = 0;
88         for (int i = 0; i < 4; i++) {
89             final String fileString = TextFileUtils.getTextFileAsString(outFilePaths[0]).replaceAll("\\s+", "");
90             outFile0Size = fileString.length();
91             if (outFile0Size > 0 && outFile0Size >= expectedFileSizes[0]) {
92                 break;
93             }
94             ThreadUtilities.sleep(500);
95         }
96
97         ThreadUtilities.sleep(500);
98         apexMain.shutdown();
99
100         final long[] actualFileSizes = new long[expectedFileSizes.length];
101
102         for (int i = 0; i < outFilePaths.length; i++) {
103             final String fileString = TextFileUtils.getTextFileAsString(outFilePaths[i]).replaceAll("\\s+", "");
104             actualFileSizes[i] = fileString.length();
105             new File(outFilePaths[i]).delete();
106         }
107
108         for (int i = 0; i < actualFileSizes.length; i++) {
109             assertEquals(actualFileSizes[i], expectedFileSizes[i]);
110         }
111     }
112 }