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.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;
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;
51 public TemporaryFolder tempTestDir = new TemporaryFolder();
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");
59 eventContextString = ResourceUtils.getResourceAsString("examples/scripts/TestPolicyJavaEventContext.apex");
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";
65 testEventAlbumContextTest();
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");
74 eventContextString = ResourceUtils.getResourceAsString("examples/scripts/TestPolicyAvroEventContext.apex");
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";
80 testEventAlbumContextTest();
83 private void testEventAlbumContextTest() throws IOException, ApexException {
84 TextFileUtils.putStringAsFile(eventContextString, tempCommandFile);
86 final String[] cliArgs = new String[] { "-c", tempCommandFile.getCanonicalPath(), "-l",
87 tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath() };
89 new ApexCLIEditorMain(cliArgs);
91 final String[] args = new String[] { "-m", tempModelFile.getAbsolutePath(), "-c", configFile };
92 final ApexMain apexMain = new ApexMain(args);
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+", "");
105 ThreadUtilities.sleep(100);
111 assertTrue("Test failed, the output event file was not created", outputEventFile.exists());
112 outputEventFile.delete();
114 assertTrue("Test failed, the output event file was empty", receivedApexOutputString.length() > 0);
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+", "");
121 assertEquals(outputEventCompareString, receivedApexOutputString);