8f9aa6cee6f502601c13070ba96e7533dfe2f6a3
[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.context;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.File;
27 import java.io.IOException;
28
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.TemporaryFolder;
32 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
33 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
34 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
35 import org.onap.policy.apex.model.utilities.TextFileUtils;
36 import org.onap.policy.apex.service.engine.main.ApexMain;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38
39 public class EventAlbumContextTest {
40     private File tempCommandFile;
41     private File tempLogFile;
42     private File tempModelFile;
43     private String eventContextString;
44     private String configFile;
45     private String outputFile;
46     private String compareFile;
47
48     @Rule
49     public TemporaryFolder tempTestDir = new TemporaryFolder();
50
51     @Test
52     public void testJavaEventAlbumContextTest() throws IOException, ApexException {
53         tempCommandFile = tempTestDir.newFile("TestPolicyJavaEventContext.apex");
54         tempLogFile = tempTestDir.newFile("TestPolicyJavaEventContext.log");
55         tempModelFile = tempTestDir.newFile("TestPolicyJavaEventContext.json");
56
57         eventContextString = ResourceUtils.getResourceAsString("examples/scripts/TestPolicyJavaEventContext.apex");
58
59         configFile = "src/test/resources/prodcons/Context_JavaEventAlbum_file2file.json";
60         outputFile = "src/test/resources/events/Context_JavaEventAlbum_EventOut.json";
61         compareFile = "src/test/resources/events/Context_JavaEventAlbum_EventOutCompare.json";
62         
63         testEventAlbumContextTest();
64     }
65
66     @Test
67     public void testAvroEventAlbumContextTest() throws IOException, ApexException {
68         tempCommandFile = tempTestDir.newFile("TestPolicyAvroEventContext.apex");
69         tempLogFile = tempTestDir.newFile("TestPolicyAvroEventContext.log");
70         tempModelFile = tempTestDir.newFile("TestPolicyAvroEventContext.json");
71
72         eventContextString = ResourceUtils.getResourceAsString("examples/scripts/TestPolicyAvroEventContext.apex");
73
74         configFile = "src/test/resources/prodcons/Context_AvroEventAlbum_file2file.json";
75         outputFile = "src/test/resources/events/Context_AvroEventAlbum_EventOut.json";
76         compareFile = "src/test/resources/events/Context_AvroEventAlbum_EventOutCompare.json";
77         
78         testEventAlbumContextTest();
79     }
80
81     private void testEventAlbumContextTest() throws IOException, ApexException {
82         TextFileUtils.putStringAsFile(eventContextString, tempCommandFile);
83
84         final String[] cliArgs = new String[] { "-c", tempCommandFile.getCanonicalPath(), "-l",
85                         tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath() };
86
87         new ApexCommandLineEditorMain(cliArgs);
88
89         final String[] args = new String[] { "-m", tempModelFile.getAbsolutePath(), "-c", configFile };
90         final ApexMain apexMain = new ApexMain(args);
91
92         // The output event will be in this file
93         final File outputEventFile = new File(outputFile);
94         String receivedApexOutputString = "";
95         for (int tenthsOfSecondsToWait = 100; tenthsOfSecondsToWait > 0; tenthsOfSecondsToWait--) {
96             if (outputEventFile.exists() && outputEventFile.length() > 0) {
97                 // The output event is in this file
98                 receivedApexOutputString = TextFileUtils.getTextFileAsString(outputEventFile.getCanonicalPath())
99                                 .replaceAll("\\s+", "");
100                 break;
101             }
102
103             ThreadUtilities.sleep(100);
104         }
105
106         // Shut down Apex
107         apexMain.shutdown();
108
109         assertTrue("Test failed, the output event file was not created", outputEventFile.exists());
110         outputEventFile.delete();
111
112         assertTrue("Test failed, the output event file was empty", receivedApexOutputString.length() > 0);
113
114         // We compare the output to what we expect to get
115         final String expectedFileContent = TextFileUtils
116                         .getTextFileAsString(compareFile);
117         final String outputEventCompareString = expectedFileContent.replaceAll("\\s+", "");
118
119         assertEquals(outputEventCompareString, receivedApexOutputString);
120     }
121
122 }