5dfeede0a5f77d6576f02994bb1725d4df61f72b
[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.model.policymodel.handling;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.File;
27 import java.sql.Connection;
28 import java.sql.DriverManager;
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.dao.DaoParameters;
35 import org.onap.policy.apex.model.basicmodel.test.TestApexModel;
36 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
37
38 public class TestApexPolicyModel {
39     private Connection connection;
40     TestApexModel<AxPolicyModel> testApexModel;
41
42     @Before
43     public void setup() throws Exception {
44         Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
45         connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true");
46
47         testApexModel = new TestApexModel<AxPolicyModel>(AxPolicyModel.class, new TestApexPolicyModelCreator());
48     }
49
50     @After
51     public void teardown() throws Exception {
52         connection.close();
53         new File("derby.log").delete();
54     }
55
56     @Test
57     public void testModelValid() throws Exception {
58         final AxValidationResult result = testApexModel.testApexModelValid();
59         assertTrue(result.toString().equals(VALID_MODEL_STRING));
60     }
61
62     @Test
63     public void testApexModelVaidateObservation() throws Exception {
64         final AxValidationResult result = testApexModel.testApexModelVaidateObservation();
65         assertTrue(result.toString().equals(OBSERVATION_MODEL_STRING));
66     }
67
68
69     @Test
70     public void testApexModelVaidateWarning() throws Exception {
71         final AxValidationResult result = testApexModel.testApexModelVaidateWarning();
72         assertTrue(result.toString().equals(WARNING_MODEL_STRING));
73     }
74
75     @Test
76     public void testModelVaidateInvalidModel() throws Exception {
77         final AxValidationResult result = testApexModel.testApexModelVaidateInvalidModel();
78         assertEquals(INVALID_MODEL_STRING, result.toString());
79     }
80
81     @Test
82     public void testModelVaidateMalstructured() throws Exception {
83         final AxValidationResult result = testApexModel.testApexModelVaidateMalstructured();
84         assertTrue(result.toString().equals(INVALID_MODEL_MALSTRUCTURED_STRING));
85     }
86
87     @Test
88     public void testModelWriteReadXML() throws Exception {
89         testApexModel.testApexModelWriteReadXml();
90     }
91
92     @Test
93     public void testModelWriteReadJSON() throws Exception {
94         testApexModel.testApexModelWriteReadJson();
95     }
96
97     @Test
98     public void testModelWriteReadJPA() throws Exception {
99         final DaoParameters DaoParameters = new DaoParameters();
100         DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao");
101         DaoParameters.setPersistenceUnit("DAOTest");
102
103         testApexModel.testApexModelWriteReadJpa(DaoParameters);
104     }
105
106     private static final String VALID_MODEL_STRING = "***validation of model successful***";
107
108     private static final String OBSERVATION_MODEL_STRING =
109             "\n" + "***observations noted during validation of model***\n"
110                     + "AxReferenceKey:(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=state):org.onap.policy.apex.model.policymodel.concepts.AxState:OBSERVATION:state output stateOutput0 is not used directly by any task\n"
111                     + "********************************";
112
113     private static final String WARNING_MODEL_STRING = "\n" + "***warnings issued during validation of model***\n"
114             + "AxArtifactKey:(name=policy,version=0.0.1):org.onap.policy.apex.model.policymodel.concepts.AxPolicy:WARNING:state AxReferenceKey:(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=anotherState) is not referenced in the policy execution tree\n"
115             + "********************************";
116
117     private static final String INVALID_MODEL_STRING = "\n" + "***validation of model failed***\n"
118             + "AxArtifactKey:(name=contextAlbum0,version=0.0.1):org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum:INVALID:scope is not defined\n"
119             + "AxArtifactKey:(name=contextAlbum1,version=0.0.1):org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum:INVALID:scope is not defined\n"
120             + "AxReferenceKey:(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=state):org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:task output field AxOutputField:(key=AxReferenceKey:(parentKeyName=task,parentKeyVersion=0.0.1,parentLocalName=outputFields,localName=OE1PAR0),fieldSchemaKey=AxArtifactKey:(name=eventContextItem0,version=0.0.1),optional=false) for task task:0.0.1 not in output event outEvent0:0.0.1\n"
121             + "AxReferenceKey:(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=state):org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:task output field AxOutputField:(key=AxReferenceKey:(parentKeyName=task,parentKeyVersion=0.0.1,parentLocalName=outputFields,localName=OE1PAR1),fieldSchemaKey=AxArtifactKey:(name=eventContextItem1,version=0.0.1),optional=false) for task task:0.0.1 not in output event outEvent0:0.0.1\n"
122             + "********************************";
123
124     private static final String INVALID_MODEL_MALSTRUCTURED_STRING = "\n" + "***validation of model failed***\n"
125             + "AxArtifactKey:(name=policyModel_KeyInfo,version=0.0.1):org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation:INVALID:keyInfoMap may not be empty\n"
126             + "AxArtifactKey:(name=policyModel,version=0.0.1):org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:key information not found for key AxArtifactKey:(name=policyModel,version=0.0.1)\n"
127             + "AxArtifactKey:(name=policyModel,version=0.0.1):org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:key information not found for key AxArtifactKey:(name=policyModel_KeyInfo,version=0.0.1)\n"
128             + "AxArtifactKey:(name=policyModel,version=0.0.1):org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:key information not found for key AxArtifactKey:(name=policyModel_Schemas,version=0.0.1)\n"
129             + "AxArtifactKey:(name=policyModel,version=0.0.1):org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:key information not found for key AxArtifactKey:(name=policyModel_Events,version=0.0.1)\n"
130             + "AxArtifactKey:(name=policyModel,version=0.0.1):org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:key information not found for key AxArtifactKey:(name=policyModel_Albums,version=0.0.1)\n"
131             + "AxArtifactKey:(name=policyModel,version=0.0.1):org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:key information not found for key AxArtifactKey:(name=policyModel_Tasks,version=0.0.1)\n"
132             + "AxArtifactKey:(name=policyModel,version=0.0.1):org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:key information not found for key AxArtifactKey:(name=policyModel_Policies,version=0.0.1)\n"
133             + "AxArtifactKey:(name=policyModel_Schemas,version=0.0.1):org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas:INVALID:contextSchemas may not be empty\n"
134             + "AxArtifactKey:(name=policyModel_Events,version=0.0.1):org.onap.policy.apex.model.eventmodel.concepts.AxEvents:INVALID:eventMap may not be empty\n"
135             + "AxArtifactKey:(name=policyModel_Albums,version=0.0.1):org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums:OBSERVATION:albums are empty\n"
136             + "AxArtifactKey:(name=policyModel_Tasks,version=0.0.1):org.onap.policy.apex.model.policymodel.concepts.AxTasks:INVALID:taskMap may not be empty\n"
137             + "AxArtifactKey:(name=policyModel_Policies,version=0.0.1):org.onap.policy.apex.model.policymodel.concepts.AxPolicies:INVALID:policyMap may not be empty\n"
138             + "********************************";
139 }