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