c2b09727329f4e4ed77f093c0b46a2ef0e3c1230
[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.apps.uservice.test.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", "src/test/resources/events/Events0104Out.json"};
43
44         final long[] expectedFileSizes = {25949, 23007};
45
46         testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
47     }
48
49     @Test
50     public void testJsonFilteredFileOutEvents() throws MessagingException, ApexException, IOException {
51         final String[] args = {"src/test/resources/prodcons/File2FileFilteredOutJsonEvent.json"};
52
53         final String[] outFilePaths =
54                 {"src/test/resources/events/Events0004Out.json", "src/test/resources/events/Events0104Out.json"};
55
56         final long[] expectedFileSizes = {25949, 23007};
57
58         testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
59     }
60
61     @Test
62     public void testJsonFilteredFileInEvents() throws MessagingException, ApexException, IOException {
63         final String[] args = {"src/test/resources/prodcons/File2FileFilteredInJsonEvent.json"};
64
65         final String[] outFilePaths = {"src/test/resources/events/Events0004Out.json"};
66
67         final long[] expectedFileSizes = {25949};
68
69         testFilteredFileEvents(args, outFilePaths, expectedFileSizes);
70     }
71
72     private void testFilteredFileEvents(final String[] args, final String[] outFilePaths,
73             final long[] expectedFileSizes) throws MessagingException, ApexException, IOException {
74         final ApexMain apexMain = new ApexMain(args);
75
76         final File outFile0 = new File(outFilePaths[0]);
77
78         while (!outFile0.exists()) {
79             ThreadUtilities.sleep(500);
80         }
81
82         // Wait for the file to be filled
83         long outFile0Size = 0;
84         for (int i = 0; i < 4; i++) {
85             final String fileString = TextFileUtils.getTextFileAsString(outFilePaths[0]).replaceAll("\\s+", "");
86             outFile0Size = fileString.length();
87             if (outFile0Size > 0 && outFile0Size >= expectedFileSizes[0]) {
88                 break;
89             }
90             ThreadUtilities.sleep(500);
91         }
92
93         ThreadUtilities.sleep(500);
94         apexMain.shutdown();
95
96         final long[] actualFileSizes = new long[expectedFileSizes.length];
97
98         for (int i = 0; i < outFilePaths.length; i++) {
99             final String fileString = TextFileUtils.getTextFileAsString(outFilePaths[i]).replaceAll("\\s+", "");
100             actualFileSizes[i] = fileString.length();
101             new File(outFilePaths[i]).delete();
102         }
103
104         for (int i = 0; i < actualFileSizes.length; i++) {
105             assertEquals(actualFileSizes[i], expectedFileSizes[i]);
106         }
107     }
108 }