5360a214801a59c6afcd89d4f440673739cb0d86
[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 /**
39  * The Class AvroEventAlbumContextTest.
40  */
41 public class AvroEventAlbumContextTest {
42
43     /**
44      * Test avro event fields, by starting an engine, send event in, test event out.
45      *
46      * @throws IOException Signals that an I/O exception has occurred.
47      * @throws ApexException the apex exception
48      */
49     @Test
50     public void testAvroEventAlbumContextTest() throws IOException, ApexException {
51         final File tempCommandFile = File.createTempFile("TestPolicyAvroEventContext", ".apex");
52         final File tempLogFile = File.createTempFile("TestPolicyAvroEventContext", ".log");
53         final File tempModelFile = File.createTempFile("TestPolicyAvroEventContext", ".json");
54
55         final String javaEventContextString =
56                 ResourceUtils.getResourceAsString("examples/scripts/TestPolicyAvroEventContext.apex");
57         TextFileUtils.putStringAsFile(javaEventContextString, tempCommandFile);
58
59         final String[] cliArgs = new String[] {"-c", tempCommandFile.getCanonicalPath(), "-l",
60                 tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()};
61
62         ModelService.clear();
63
64         new ApexCLIEditorMain(cliArgs);
65
66         tempCommandFile.delete();
67         tempLogFile.delete();
68
69         ModelService.clear();
70
71         final String[] args = new String[] {"-m", tempModelFile.getAbsolutePath(), "-c",
72                 "src/test/resources/prodcons/Context_AvroEventAlbum_file2file.json"};
73         final ApexMain apexMain = new ApexMain(args);
74         ThreadUtilities.sleep(1000);
75         apexMain.shutdown();
76         
77         ParameterService.clear();
78         // The output event is in this file
79         final File outputEventFile = new File("src/test/resources/events/Context_AvroEventAlbum_EventOut.json");
80         final String outputEventString =
81                 TextFileUtils.getTextFileAsString(outputEventFile.getCanonicalPath()).replaceAll("\\s+", "");
82
83         // We compare the output to what we expect to get
84         final String outputEventCompareString = TextFileUtils
85                 .getTextFileAsString("src/test/resources/events/Context_AvroEventAlbum_EventOutCompare.json")
86                 .replaceAll("\\s+", "");
87
88         // Check what we got is what we expected to get
89         assertEquals(outputEventCompareString, outputEventString);
90
91         
92         tempModelFile.delete();
93         outputEventFile.delete();
94     }
95 }