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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.apps.uservice.test.context;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
27 import java.io.IOException;
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;
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;
49 public TemporaryFolder tempTestDir = new TemporaryFolder();
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");
57 eventContextString = ResourceUtils.getResourceAsString("examples/scripts/TestPolicyJavaEventContext.apex");
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";
63 testEventAlbumContextTest();
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");
72 eventContextString = ResourceUtils.getResourceAsString("examples/scripts/TestPolicyAvroEventContext.apex");
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";
78 testEventAlbumContextTest();
81 private void testEventAlbumContextTest() throws IOException, ApexException {
82 TextFileUtils.putStringAsFile(eventContextString, tempCommandFile);
84 final String[] cliArgs = new String[] { "-c", tempCommandFile.getCanonicalPath(), "-l",
85 tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath() };
87 new ApexCommandLineEditorMain(cliArgs);
89 final String[] args = new String[] { "-m", tempModelFile.getAbsolutePath(), "-c", configFile };
90 final ApexMain apexMain = new ApexMain(args);
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+", "");
103 ThreadUtilities.sleep(100);
109 assertTrue("Test failed, the output event file was not created", outputEventFile.exists());
110 outputEventFile.delete();
112 assertTrue("Test failed, the output event file was empty", receivedApexOutputString.length() > 0);
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+", "");
119 assertEquals(outputEventCompareString, receivedApexOutputString);