35b725f2af88641b2cc8d374bd12ff4f1ae6acf2
[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.auth.clieditor;
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 import java.net.URL;
29
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
34 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
35 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
36 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38
39 /**
40  * The Class TestCLIEditorScripting.
41  *
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  */
44 public class CommandLineEditorScriptingTest {
45
46     private File tempModelFile;
47     private File tempLogFile;
48
49     private String[] samplePolicyArgs;
50
51     private String[] samplePolicyMapArgs;
52
53     /**
54      * Initialise args.
55      *
56      * @throws IOException Signals that an I/O exception has occurred.
57      */
58     @Before
59     public void initialiseArgs() throws IOException {
60         tempModelFile = File.createTempFile("SampleLBPolicyMap", ".json");
61         tempLogFile = File.createTempFile("SampleLBPolicyMap", ".log");
62
63         samplePolicyArgs = new String[] {"-c", "src/test/resources/scripts/SampleLBPolicy.apex", "-o",
64                 tempModelFile.getAbsolutePath(), "-l", tempLogFile.getAbsolutePath()};
65
66         samplePolicyMapArgs = new String[] {"-c", "src/test/resources/scripts/SampleLBPolicy_WithMap.apex", "-o",
67                 tempModelFile.getAbsolutePath(), "-l", tempLogFile.getAbsolutePath()};
68     }
69
70     /**
71      * Removes the generated files.
72      */
73     @After
74     public void removeGeneratedFiles() {
75         tempModelFile.delete();
76         tempLogFile.delete();
77     }
78
79     /**
80      * Test sample Fuzzy LB policy script.
81      *
82      * @throws IOException Signals that an I/O exception has occurred.
83      * @throws ApexModelException if there is an Apex error
84      */
85     @Test
86     public void testSamplePolicyScript() throws IOException, ApexModelException {
87         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(samplePolicyArgs);
88         assertEquals(0, cliEditor.getErrorCount());
89
90         // Read the file from disk
91         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
92
93         final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath());
94         final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
95
96         final URL compareModelUrl =
97                 ResourceUtils.getLocalFile("src/test/resources/compare/FuzzyPolicyModel_Compare.json");
98         final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream());
99
100         // Ignore key info UUIDs
101         writtenModel.getKeyInformation().getKeyInfoMap().clear();
102         compareModel.getKeyInformation().getKeyInfoMap().clear();
103
104         assertTrue(writtenModel.equals(compareModel));
105     }
106
107     /**
108      * Test sample Fuzzy LB map policy script.
109      *
110      * @throws IOException Signals that an I/O exception has occurred.
111      * @throws ApexModelException if there is an Apex error
112      */
113     @Test
114     public void testSampleMapPolicyScript() throws IOException, ApexModelException {
115         tempModelFile.delete();
116
117         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(samplePolicyMapArgs);
118         assertEquals(0, cliEditor.getErrorCount());
119
120         assertTrue(tempModelFile.isFile());
121
122         // Read the file from disk
123         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
124
125         final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath());
126         final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
127
128         final AxValidationResult validationResult = new AxValidationResult();
129         writtenModel.validate(validationResult);
130         assertEquals(AxValidationResult.ValidationResult.OBSERVATION, validationResult.getValidationResult());
131     }
132 }