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