347cce80ee072e98b20226dbaf5b31bbcfa8b55f
[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 TestFile2File {
36
37     @Test
38     public void testJsonFileEvents() throws MessagingException, ApexException, IOException {
39         final String[] args = {"src/test/resources/prodcons/File2FileJsonEvent.json"};
40
41         testFileEvents(args, "src/test/resources/events/EventsOut.json", 48956);
42     }
43
44     @Test
45     public void testXMLFileEvents() throws MessagingException, ApexException, IOException {
46         final String[] args = {"src/test/resources/prodcons/File2FileXmlEvent.json"};
47
48         testFileEvents(args, "src/test/resources/events/EventsOut.xmlfile", 106739);
49     }
50
51     private void testFileEvents(final String[] args, final String outFilePath, final long expectedFileSize)
52             throws MessagingException, ApexException, IOException {
53         final ApexMain apexMain = new ApexMain(args);
54
55         final File outFile = new File(outFilePath);
56
57         while (!outFile.exists()) {
58             ThreadUtilities.sleep(500);
59         }
60
61         // Wait for the file to be filled
62         long outFileSize = 0;
63         while (true) {
64             final String fileString = TextFileUtils.getTextFileAsString(outFilePath).replaceAll("\\s+", "");
65             outFileSize = fileString.length();
66             if (outFileSize > 0 && outFileSize >= expectedFileSize) {
67                 break;
68             }
69             ThreadUtilities.sleep(500);
70         }
71
72         apexMain.shutdown();
73         outFile.delete();
74         assertEquals(outFileSize, expectedFileSize);
75     }
76 }
77
78