997664b13e12e2123182f1e84d28c65a6497b905
[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 ApexPolicyModelTest {
39     private Connection connection;
40     TestApexModel<AxPolicyModel> testApexModel;
41
42     /**
43      * Set up the policy model tests.
44      * 
45      * @throws Exception on setup errors
46      */
47     @Before
48     public void setup() throws Exception {
49         Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
50         connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true");
51
52         testApexModel = new TestApexModel<AxPolicyModel>(AxPolicyModel.class, new ApexPolicyModelCreatorTest());
53     }
54
55     @After
56     public void teardown() throws Exception {
57         connection.close();
58         new File("derby.log").delete();
59     }
60
61     @Test
62     public void testModelValid() throws Exception {
63         final AxValidationResult result = testApexModel.testApexModelValid();
64         assertTrue(result.toString().equals(VALID_MODEL_STRING));
65     }
66
67     @Test
68     public void testApexModelVaidateObservation() throws Exception {
69         final AxValidationResult result = testApexModel.testApexModelVaidateObservation();
70         assertTrue(result.toString().equals(OBSERVATION_MODEL_STRING));
71     }
72
73     @Test
74     public void testApexModelVaidateWarning() throws Exception {
75         final AxValidationResult result = testApexModel.testApexModelVaidateWarning();
76         assertTrue(result.toString().equals(WARNING_MODEL_STRING));
77     }
78
79     @Test
80     public void testModelVaidateInvalidModel() throws Exception {
81         final AxValidationResult result = testApexModel.testApexModelVaidateInvalidModel();
82         assertEquals(INVALID_MODEL_STRING, result.toString());
83     }
84
85     @Test
86     public void testModelVaidateMalstructured() throws Exception {
87         final AxValidationResult result = testApexModel.testApexModelVaidateMalstructured();
88         assertTrue(result.toString().equals(INVALID_MODEL_MALSTRUCTURED_STRING));
89     }
90
91     @Test
92     public void testModelWriteReadXml() throws Exception {
93         testApexModel.testApexModelWriteReadXml();
94     }
95
96     @Test
97     public void testModelWriteReadJson() throws Exception {
98         testApexModel.testApexModelWriteReadJson();
99     }
100
101     @Test
102     public void testModelWriteReadJpa() throws Exception {
103         final DaoParameters DaoParameters = new DaoParameters();
104         DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao");
105         DaoParameters.setPersistenceUnit("DAOTest");
106
107         testApexModel.testApexModelWriteReadJpa(DaoParameters);
108     }
109
110     private static final String VALID_MODEL_STRING = "***validation of model successful***";
111
112     private static final String OBSERVATION_MODEL_STRING = "\n"
113                     + "***observations noted during validation of model***\n"
114                     + "AxReferenceKey:(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,"
115                     + "localName=state):org.onap.policy.apex.model.policymodel.concepts.AxState:OBSERVATION:"
116                     + "state output stateOutput0 is not used directly by any task\n"
117                     + "********************************";
118
119     private static final String WARNING_MODEL_STRING = "\n" + "***warnings issued during validation of model***\n"
120                     + "AxArtifactKey:(name=policy,version=0.0.1)"
121                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicy:WARNING:state AxReferenceKey:"
122                     + "(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=anotherState) "
123                     + "is not referenced in the policy execution tree\n" + "********************************";
124
125     private static final String INVALID_MODEL_STRING = "\n" + "***validation of model failed***\n"
126                     + "AxArtifactKey:(name=contextAlbum0,version=0.0.1):"
127                     + "org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum:INVALID:scope is not defined\n"
128                     + "AxArtifactKey:(name=contextAlbum1,version=0.0.1):"
129                     + "org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum:INVALID:scope is not defined\n"
130                     + "AxReferenceKey:"
131                     + "(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=state):"
132                     + "org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
133                     + "task output field AxOutputField:(key=AxReferenceKey:"
134                     + "(parentKeyName=task,parentKeyVersion=0.0.1,parentLocalName=outputFields,localName=OE1PAR0),"
135                     + "fieldSchemaKey=AxArtifactKey:(name=eventContextItem0,version=0.0.1),optional=false) for task "
136                     + "task:0.0.1 not in output event outEvent0:0.0.1\n" + "AxReferenceKey:"
137                     + "(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=state):"
138                     + "org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
139                     + "task output field AxOutputField:(key=AxReferenceKey:"
140                     + "(parentKeyName=task,parentKeyVersion=0.0.1,parentLocalName=outputFields,localName=OE1PAR1),"
141                     + "fieldSchemaKey=AxArtifactKey:(name=eventContextItem1,version=0.0.1),optional=false) for task "
142                     + "task:0.0.1 not in output event outEvent0:0.0.1\n" + "********************************";
143
144     private static final String INVALID_MODEL_MALSTRUCTURED_STRING = "\n" + "***validation of model failed***\n"
145                     + "AxArtifactKey:(name=policyModel_KeyInfo,version=0.0.1):"
146                     + "org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation:INVALID:"
147                     + "keyInfoMap may not be empty\n" + "AxArtifactKey:(name=policyModel,version=0.0.1)"
148                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
149                     + "key information not found for key AxArtifactKey:(name=policyModel,version=0.0.1)\n"
150                     + "AxArtifactKey:(name=policyModel,version=0.0.1)"
151                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
152                     + "key information not found for key AxArtifactKey:(name=policyModel_KeyInfo,version=0.0.1)\n"
153                     + "AxArtifactKey:(name=policyModel,version=0.0.1)"
154                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
155                     + "key information not found for key AxArtifactKey:(name=policyModel_Schemas,version=0.0.1)\n"
156                     + "AxArtifactKey:(name=policyModel,version=0.0.1)"
157                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
158                     + "key information not found for key AxArtifactKey:(name=policyModel_Events,version=0.0.1)\n"
159                     + "AxArtifactKey:(name=policyModel,version=0.0.1)"
160                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
161                     + "key information not found for key AxArtifactKey:(name=policyModel_Albums,version=0.0.1)\n"
162                     + "AxArtifactKey:(name=policyModel,version=0.0.1)"
163                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
164                     + "key information not found for key AxArtifactKey:(name=policyModel_Tasks,version=0.0.1)\n"
165                     + "AxArtifactKey:(name=policyModel,version=0.0.1)"
166                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
167                     + "key information not found for key AxArtifactKey:(name=policyModel_Policies,version=0.0.1)\n"
168                     + "AxArtifactKey:(name=policyModel_Schemas,version=0.0.1):"
169                     + "org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas:INVALID:"
170                     + "contextSchemas may not be empty\n" + "AxArtifactKey:(name=policyModel_Events,version=0.0.1):"
171                     + "org.onap.policy.apex.model.eventmodel.concepts.AxEvents:INVALID:eventMap may not be empty\n"
172                     + "AxArtifactKey:(name=policyModel_Albums,version=0.0.1):"
173                     + "org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums:OBSERVATION:albums are empty\n"
174                     + "AxArtifactKey:(name=policyModel_Tasks,version=0.0.1)"
175                     + ":org.onap.policy.apex.model.policymodel.concepts.AxTasks:INVALID:taskMap may not be empty\n"
176                     + "AxArtifactKey:(name=policyModel_Policies,version=0.0.1)"
177                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicies:INVALID:policyMap may not be empty\n"
178                     + "********************************";
179 }