bcdcd571ffa28397733bf2e9fffa3765034a6472
[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.basicmodel.service.ParameterService;
37 import org.onap.policy.apex.model.utilities.TextFileUtils;
38 import org.onap.policy.apex.service.engine.main.ApexMain;
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         ModelService.clear();
90
91         new ApexCLIEditorMain(cliArgs);
92
93         ModelService.clear();
94
95         final String[] args = new String[] { "-m", tempModelFile.getAbsolutePath(), "-c", configFile };
96         final ApexMain apexMain = new ApexMain(args);
97
98         // The output event will be in this file
99         final File outputEventFile = new File(outputFile);
100         String receivedApexOutputString = "";
101         for (int tenthsOfSecondsToWait = 100; tenthsOfSecondsToWait > 0; tenthsOfSecondsToWait--) {
102             if (outputEventFile.exists() && outputEventFile.length() > 0) {
103                 // The output event is in this file
104                 receivedApexOutputString = TextFileUtils.getTextFileAsString(outputEventFile.getCanonicalPath())
105                                 .replaceAll("\\s+", "");
106                 break;
107             }
108
109             ThreadUtilities.sleep(100);
110         }
111
112         // Shut down Apex
113         apexMain.shutdown();
114         ParameterService.clear();
115
116         assertTrue("Test failed, the output event file was not created", outputEventFile.exists());
117         outputEventFile.delete();
118
119         assertTrue("Test failed, the output event file was empty", receivedApexOutputString.length() > 0);
120
121         // We compare the output to what we expect to get
122         final String expectedFileContent = TextFileUtils
123                         .getTextFileAsString(compareFile);
124         final String outputEventCompareString = expectedFileContent.replaceAll("\\s+", "");
125
126         assertEquals(outputEventCompareString, receivedApexOutputString);
127     }
128
129 }