242a9415c39791cd86e15c095bbcc565d0fd2e48
[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
25 import java.io.File;
26 import java.io.IOException;
27
28 import org.junit.Test;
29 import org.onap.policy.apex.auth.clieditor.ApexCLIEditorMain;
30 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
31 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
32 import org.onap.policy.apex.model.basicmodel.service.ModelService;
33 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
34 import org.onap.policy.apex.model.utilities.ResourceUtils;
35 import org.onap.policy.apex.model.utilities.TextFileUtils;
36 import org.onap.policy.apex.service.engine.main.ApexMain;
37
38 public class JavaEventAlbumContextTest {
39     @Test
40     public void testJavaEventAlbumContextTest() throws IOException, ApexException {
41         final File tempCommandFile = File.createTempFile("TestPolicyJavaEventContext", ".apex");
42         final File tempLogFile = File.createTempFile("TestPolicyJavaEventContext", ".log");
43         final File tempModelFile = File.createTempFile("TestPolicyJavaEventContext", ".json");
44
45         final String javaEventContextString =
46                 ResourceUtils.getResourceAsString("examples/scripts/TestPolicyJavaEventContext.apex");
47         TextFileUtils.putStringAsFile(javaEventContextString, tempCommandFile);
48
49         final String[] cliArgs = new String[] {"-c", tempCommandFile.getCanonicalPath(), "-l",
50                 tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()};
51
52         ModelService.clear();
53
54         new ApexCLIEditorMain(cliArgs);
55
56         tempCommandFile.delete();
57         tempLogFile.delete();
58
59         ModelService.clear();
60
61         final String[] args = new String[] {"-m", tempModelFile.getAbsolutePath(), "-c",
62                 "src/test/resources/prodcons/Context_JavaEventAlbum_file2file.json"};
63         final ApexMain apexMain = new ApexMain(args);
64         ThreadUtilities.sleep(1000);
65         apexMain.shutdown();
66         ParameterService.clear();
67
68         // The output event is in this file
69         final File outputEventFile = new File("src/test/resources/events/Context_JavaEventAlbum_EventOut.json");
70         final String actualFileContent = TextFileUtils.getTextFileAsString(outputEventFile.getCanonicalPath());
71         final String outputEventString = actualFileContent.replaceAll("\\s+", "");
72
73         // We compare the output to what we expect to get
74         final String expectedFileContent = TextFileUtils
75                 .getTextFileAsString("src/test/resources/events/Context_JavaEventAlbum_EventOutCompare.json");
76         final String outputEventCompareString = expectedFileContent.replaceAll("\\s+", "");
77
78         assertEquals(outputEventCompareString, outputEventString);
79
80         tempModelFile.delete();
81         outputEventFile.delete();
82     }
83 }