3192079d7cfb8de2186b960bb2589dd39ea3cb3d
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.policymodel.handling;
23
24 import static org.junit.Assert.assertEquals;
25
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
29 import org.onap.policy.apex.model.basicmodel.dao.DaoParameters;
30 import org.onap.policy.apex.model.basicmodel.test.TestApexModel;
31 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
32
33 public class ApexPolicyModelTest {
34     TestApexModel<AxPolicyModel> testApexModel;
35
36     /**
37      * Set up the policy model tests.
38      *
39      * @throws Exception on setup errors
40      */
41     @Before
42     public void setup() throws Exception {
43         testApexModel = new TestApexModel<AxPolicyModel>(AxPolicyModel.class, new SupportApexPolicyModelCreator());
44     }
45
46     @Test
47     public void testModelValid() throws Exception {
48         final AxValidationResult result = testApexModel.testApexModelValid();
49         assertEquals(VALID_MODEL_STRING, result.toString());
50     }
51
52     @Test
53     public void testApexModelVaidateObservation() throws Exception {
54         final AxValidationResult result = testApexModel.testApexModelVaidateObservation();
55         assertEquals(OBSERVATION_MODEL_STRING, result.toString());
56     }
57
58     @Test
59     public void testApexModelVaidateWarning() throws Exception {
60         final AxValidationResult result = testApexModel.testApexModelVaidateWarning();
61         assertEquals(WARNING_MODEL_STRING, result.toString());
62     }
63
64     @Test
65     public void testModelVaidateInvalidModel() throws Exception {
66         final AxValidationResult result = testApexModel.testApexModelVaidateInvalidModel();
67         assertEquals(INVALID_MODEL_STRING, result.toString());
68     }
69
70     @Test
71     public void testModelVaidateMalstructured() throws Exception {
72         final AxValidationResult result = testApexModel.testApexModelVaidateMalstructured();
73         assertEquals(INVALID_MODEL_MALSTRUCTURED_STRING, result.toString());
74     }
75
76     @Test
77     public void testModelWriteReadXml() throws Exception {
78         testApexModel.testApexModelWriteReadXml();
79     }
80
81     @Test
82     public void testModelWriteReadJson() throws Exception {
83         testApexModel.testApexModelWriteReadJson();
84     }
85
86     @Test
87     public void testModelWriteReadJpa() throws Exception {
88         final DaoParameters DaoParameters = new DaoParameters();
89         DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao");
90         DaoParameters.setPersistenceUnit("DAOTest");
91
92         testApexModel.testApexModelWriteReadJpa(DaoParameters);
93     }
94
95     private static final String VALID_MODEL_STRING = "***validation of model successful***";
96
97     private static final String OBSERVATION_MODEL_STRING = "\n"
98                     + "***observations noted during validation of model***\n"
99                     + "AxReferenceKey:(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,"
100                     + "localName=state):org.onap.policy.apex.model.policymodel.concepts.AxState:OBSERVATION:"
101                     + "state output stateOutput0 is not used directly by any task\n"
102                     + "********************************";
103
104     private static final String WARNING_MODEL_STRING = "\n" + "***warnings issued during validation of model***\n"
105                     + "AxArtifactKey:(name=policy,version=0.0.1)"
106                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicy:WARNING:state AxReferenceKey:"
107                     + "(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=anotherState) "
108                     + "is not referenced in the policy execution tree\n" + "********************************";
109
110     private static final String INVALID_MODEL_STRING = "\n" + "***validation of model failed***\n"
111                     + "AxArtifactKey:(name=contextAlbum0,version=0.0.1):"
112                     + "org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum:INVALID:scope is not defined\n"
113                     + "AxArtifactKey:(name=contextAlbum1,version=0.0.1):"
114                     + "org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum:INVALID:scope is not defined\n"
115                     + "AxReferenceKey:"
116                     + "(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=state):"
117                     + "org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
118                     + "task output field AxOutputField:(key=AxReferenceKey:"
119                     + "(parentKeyName=task,parentKeyVersion=0.0.1,parentLocalName=outputFields,localName=OE1PAR0),"
120                     + "fieldSchemaKey=AxArtifactKey:(name=eventContextItem0,version=0.0.1),optional=false) for task "
121                     + "task:0.0.1 not in output event outEvent0:0.0.1\n" + "AxReferenceKey:"
122                     + "(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=state):"
123                     + "org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
124                     + "task output field AxOutputField:(key=AxReferenceKey:"
125                     + "(parentKeyName=task,parentKeyVersion=0.0.1,parentLocalName=outputFields,localName=OE1PAR1),"
126                     + "fieldSchemaKey=AxArtifactKey:(name=eventContextItem1,version=0.0.1),optional=false) for task "
127                     + "task:0.0.1 not in output event outEvent0:0.0.1\n" + "********************************";
128
129     private static final String INVALID_MODEL_MALSTRUCTURED_STRING = "\n" + "***validation of model failed***\n"
130                     + "AxArtifactKey:(name=policyModel_KeyInfo,version=0.0.1):"
131                     + "org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation:INVALID:"
132                     + "keyInfoMap may not be empty\n" + "AxArtifactKey:(name=policyModel,version=0.0.1)"
133                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
134                     + "key information not found for key AxArtifactKey:(name=policyModel,version=0.0.1)\n"
135                     + "AxArtifactKey:(name=policyModel,version=0.0.1)"
136                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
137                     + "key information not found for key AxArtifactKey:(name=policyModel_KeyInfo,version=0.0.1)\n"
138                     + "AxArtifactKey:(name=policyModel,version=0.0.1)"
139                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
140                     + "key information not found for key AxArtifactKey:(name=policyModel_Schemas,version=0.0.1)\n"
141                     + "AxArtifactKey:(name=policyModel,version=0.0.1)"
142                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
143                     + "key information not found for key AxArtifactKey:(name=policyModel_Events,version=0.0.1)\n"
144                     + "AxArtifactKey:(name=policyModel,version=0.0.1)"
145                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:"
146                     + "key information not found for key AxArtifactKey:(name=policyModel_Albums,version=0.0.1)\n"
147                     + "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_Tasks,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_Policies,version=0.0.1)\n"
153                     + "AxArtifactKey:(name=policyModel_Schemas,version=0.0.1):"
154                     + "org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas:INVALID:"
155                     + "contextSchemas may not be empty\n" + "AxArtifactKey:(name=policyModel_Events,version=0.0.1):"
156                     + "org.onap.policy.apex.model.eventmodel.concepts.AxEvents:INVALID:eventMap may not be empty\n"
157                     + "AxArtifactKey:(name=policyModel_Albums,version=0.0.1):"
158                     + "org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums:OBSERVATION:albums are empty\n"
159                     + "AxArtifactKey:(name=policyModel_Tasks,version=0.0.1)"
160                     + ":org.onap.policy.apex.model.policymodel.concepts.AxTasks:INVALID:taskMap may not be empty\n"
161                     + "AxArtifactKey:(name=policyModel_Policies,version=0.0.1)"
162                     + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicies:INVALID:policyMap may not be empty\n"
163                     + "********************************";
164 }