Changes for checkstyle 8.32
[policy/apex-pdp.git] / testsuites / integration / integration-uservice-test / src / test / java / org / onap / policy / apex / testsuites / integration / uservice / context / EventAlbumContextTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.uservice.context;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.File;
28 import java.io.IOException;
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.TemporaryFolder;
33 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
34 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
35 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
36 import org.onap.policy.apex.service.engine.main.ApexMain;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.common.utils.resources.TextFileUtils;
39
40 public class EventAlbumContextTest {
41     private File tempCommandFile;
42     private File tempLogFile;
43     private File tempModelFile;
44     private String eventContextString;
45     private String configFile;
46     private String outputFile;
47     private String compareFile;
48
49     @Rule
50     public TemporaryFolder tempTestDir = new TemporaryFolder();
51
52     /**
53      * Clear relative file root environment variable.
54      */
55     @Before
56     public void clearRelativeFileRoot() {
57         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
58     }
59
60     @Test
61     public void testJavaEventAlbumContextTest() throws IOException, ApexException {
62         tempCommandFile = tempTestDir.newFile("TestPolicyJavaEventContext.apex");
63         tempLogFile = tempTestDir.newFile("TestPolicyJavaEventContext.log");
64         tempModelFile = tempTestDir.newFile("TestPolicyJavaEventContext.json");
65
66         eventContextString = ResourceUtils.getResourceAsString("examples/scripts/TestPolicyJavaEventContext.apex");
67
68         configFile = "src/test/resources/prodcons/Context_JavaEventAlbum_file2file.json";
69         outputFile = "target/Context_JavaEventAlbum_EventOut.json";
70         compareFile = "src/test/resources/events/Context_JavaEventAlbum_EventOutCompare.json";
71
72         testEventAlbumContextTest();
73     }
74
75     @Test
76     public void testAvroEventAlbumContextTest() throws IOException, ApexException {
77         tempCommandFile = tempTestDir.newFile("TestPolicyAvroEventContext.apex");
78         tempLogFile = tempTestDir.newFile("TestPolicyAvroEventContext.log");
79         tempModelFile = tempTestDir.newFile("TestPolicyAvroEventContext.json");
80
81         eventContextString = ResourceUtils.getResourceAsString("examples/scripts/TestPolicyAvroEventContext.apex");
82
83         configFile = "src/test/resources/prodcons/Context_AvroEventAlbum_file2file.json";
84         outputFile = "target/Context_AvroEventAlbum_EventOut.json";
85         compareFile = "src/test/resources/events/Context_AvroEventAlbum_EventOutCompare.json";
86
87         testEventAlbumContextTest();
88     }
89
90     private void testEventAlbumContextTest() throws IOException, ApexException {
91         TextFileUtils.putStringAsFile(eventContextString, tempCommandFile);
92
93         final String[] cliArgs = new String[] {"-c", tempCommandFile.getCanonicalPath(), "-l",
94             tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()};
95
96         new ApexCommandLineEditorMain(cliArgs);
97
98         final String[] args = new String[] {"-m", tempModelFile.getAbsolutePath(), "-c", configFile};
99         final ApexMain apexMain = new ApexMain(args);
100
101         // The output event will be in this file
102         final File outputEventFile = new File(outputFile);
103         String receivedApexOutputString = "";
104         for (int tenthsOfSecondsToWait = 100; tenthsOfSecondsToWait > 0; tenthsOfSecondsToWait--) {
105             if (outputEventFile.exists() && outputEventFile.length() > 0) {
106                 // The output event is in this file
107                 receivedApexOutputString =
108                         TextFileUtils.getTextFileAsString(outputEventFile.getCanonicalPath()).replaceAll("\\s+", "");
109                 break;
110             }
111
112             ThreadUtilities.sleep(100);
113         }
114
115         // Shut down Apex
116         apexMain.shutdown();
117
118         assertTrue("Test failed, the output event file was not created", outputEventFile.exists());
119         outputEventFile.delete();
120
121         assertTrue("Test failed, the output event file was empty", receivedApexOutputString.length() > 0);
122
123         // We compare the output to what we expect to get
124         final String expectedFileContent = TextFileUtils.getTextFileAsString(compareFile);
125         final String outputEventCompareString = expectedFileContent.replaceAll("\\s+", "");
126
127         assertEquals(outputEventCompareString, receivedApexOutputString);
128     }
129
130 }