57e3053a839f57ef155ca8a32b941c612ca2690c
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020-2022 Nordix Foundation.
5  *  Modifications Copyright (C) 2020-2022 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.testsuites.integration.uservice.adapt.file;
24
25 import static org.junit.Assert.assertEquals;
26
27 import java.io.File;
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;
36
37 public class TestFile2File {
38     /**
39      * Clear relative file root environment variable.
40      */
41     @Before
42     public void clearRelativeFileRoot() {
43         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
44     }
45
46     @Test
47     public void testJsonFileEvents() throws MessagingException, ApexException, IOException {
48         final String[] args = {"-rfr", "target", "-p", "target/examples/config/SampleDomain/File2FileJsonEvent.json"};
49
50         testFileEvents(args, "target/examples/events/SampleDomain/EventsOut.json", 44400);
51     }
52
53     private void testFileEvents(final String[] args, final String outFilePath, final long expectedFileSize)
54             throws MessagingException, ApexException, IOException {
55         final ApexMain apexMain = new ApexMain(args);
56
57         final File outFile = new File(outFilePath);
58
59         while (!outFile.exists()) {
60             ThreadUtilities.sleep(500);
61         }
62
63         // Wait for the file to be filled
64         long outFileSize = 0;
65         while (true) {
66             final String fileString = stripVariableLengthText(outFilePath);
67             outFileSize = fileString.length();
68             if (outFileSize > 0 && outFileSize >= expectedFileSize) {
69                 break;
70             }
71             ThreadUtilities.sleep(500);
72         }
73
74         apexMain.shutdown();
75         assertEquals(expectedFileSize, outFileSize);
76     }
77
78     /**
79      * Strip variable length text from file string.
80      *
81      * @param outFile the file to read and strip
82      * @return the stripped string
83      * @throws IOException on out file read exceptions
84      */
85     private String stripVariableLengthText(final String outFile) throws IOException {
86         return TextFileUtils.getTextFileAsString(outFile).replaceAll("\\s+", "").replaceAll(":\\d*\\.?\\d*,", ":0,")
87                 .replaceAll(":\\d*}", ":0}").replaceAll("<value>\\d*\\.?\\d*</value>", "<value>0</value>");
88     }
89 }