Adding apex auth modules
[policy/apex-pdp.git] / auth / cli-editor / src / test / java / org / onap / policy / apex / auth / clieditor / TestContextAlbums.java
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.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.net.URL;
30
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
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.apex.model.utilities.ResourceUtils;
38
39 public class TestContextAlbums {
40     private String[] logicBlockArgs;
41
42     private File tempModelFile;
43
44     @Before
45     public void createTempFiles() throws IOException {
46         tempModelFile = File.createTempFile("TestPolicyModel", ".json");
47
48         logicBlockArgs = new String[] {
49                 "-c",
50                 "src/test/resources/scripts/ContextAlbums.apex",
51                 "-o",
52                 tempModelFile.getAbsolutePath(),
53                 "-nl"
54         };
55     }
56
57     /**
58      * Removes the generated models.
59      */
60     @After
61     public void removeGeneratedModels() {
62         tempModelFile.delete();
63     }
64
65     /**
66      * Test logic block.
67      *
68      * @throws IOException Signals that an I/O exception has occurred.
69      * @throws ApexModelException if there is an Apex error
70      */
71     @Test
72     public void testLogicBlock() throws IOException, ApexModelException {
73         final ApexCLIEditorMain cliEditor = new ApexCLIEditorMain(logicBlockArgs);
74         assertEquals(1, cliEditor.getErrorCount());
75
76         // Read the file from disk
77         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
78         modelReader.setValidateFlag(false);
79
80         final URL writtenModelURL = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath());
81         final AxPolicyModel writtenModel = modelReader.read(writtenModelURL.openStream());
82         assertNotNull(writtenModel);
83
84         final URL compareModelURL = ResourceUtils.getLocalFile("src/test/resources/compare/ContextAlbumsModel_Compare.json");
85         final AxPolicyModel compareModel = modelReader.read(compareModelURL.openStream());
86
87         // Ignore key info UUIDs
88         writtenModel.getKeyInformation().getKeyInfoMap().clear();
89         compareModel.getKeyInformation().getKeyInfoMap().clear();
90
91         assertTrue(writtenModel.equals(compareModel));
92     }
93 }