32c4ff38b26969e6b0c8f4317d7dfea114945305
[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.enginemodel.handling;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.File;
26 import java.sql.Connection;
27 import java.sql.DriverManager;
28
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
33 import org.onap.policy.apex.model.basicmodel.dao.DaoParameters;
34 import org.onap.policy.apex.model.basicmodel.test.TestApexModel;
35 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel;
36
37 public class ApexEngineModelTest {
38     private Connection connection;
39     TestApexModel<AxEngineModel> testApexModel;
40
41     /**
42      * Set up the test.
43      * 
44      * @throws Exception errors from test setup
45      */
46     @Before
47     public void setup() throws Exception {
48         Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
49         connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true");
50
51         testApexModel = new TestApexModel<AxEngineModel>(AxEngineModel.class, new TestApexEngineModelCreator());
52     }
53
54     @After
55     public void teardown() throws Exception {
56         connection.close();
57         new File("derby.log").delete();
58     }
59
60     @Test
61     public void testModelValid() throws Exception {
62         final AxValidationResult result = testApexModel.testApexModelValid();
63         assertTrue(result.toString().equals(VALID_MODEL_STRING));
64     }
65
66     @Test
67     public void testModelVaidateInvalidModel() throws Exception {
68         final AxValidationResult result = testApexModel.testApexModelVaidateInvalidModel();
69         assertTrue(result.toString().equals(INVALID_MODEL_STRING));
70     }
71
72     @Test
73     public void testModelVaidateMalstructured() throws Exception {
74         final AxValidationResult result = testApexModel.testApexModelVaidateMalstructured();
75         assertTrue(result.toString().equals(INVALID_MODEL_MALSTRUCTURED_STRING));
76     }
77
78     @Test
79     public void testModelWriteReadXml() throws Exception {
80         testApexModel.testApexModelWriteReadXml();
81     }
82
83     @Test
84     public void testModelWriteReadJson() throws Exception {
85         testApexModel.testApexModelWriteReadJson();
86     }
87
88     @Test
89     public void testModelWriteReadJpa() throws Exception {
90         final DaoParameters DaoParameters = new DaoParameters();
91         DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao");
92         DaoParameters.setPersistenceUnit("DAOTest");
93
94         testApexModel.testApexModelWriteReadJpa(DaoParameters);
95     }
96
97     private static final String VALID_MODEL_STRING = "***validation of model successful***";
98
99     private static final String INVALID_MODEL_STRING = "\n" + "***validation of model failed***\n"
100                     + "AxArtifactKey:(name=AnEngine,version=0.0.1):"
101                     + "org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel:INVALID:"
102                     + "AxEngineModel - state is UNDEFINED\n" + "********************************";
103
104     private static final String INVALID_MODEL_MALSTRUCTURED_STRING = "\n" + "***validation of model failed***\n"
105                     + "AxArtifactKey:(name=AnEngine,version=0.0.1):"
106                     + "org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel:INVALID:"
107                     + "AxEngineModel - timestamp is not set\n" + "AxArtifactKey:(name=AnEngine,version=0.0.1):"
108                     + "org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel:INVALID:"
109                     + "AxEngineModel - state is UNDEFINED\n" + "********************************";
110 }