9ddd3cc7ecc36b74dafb30de3370349d24ac5b37
[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.basicmodel.handling;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import org.junit.Test;
28 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
30 import org.onap.policy.apex.model.basicmodel.test.TestApexModel;
31
32 /**
33  * @author Liam Fallon (liam.fallon@ericsson.com)
34  */
35 public class TestBasicModelTest {
36
37     @Test
38     public void testNormalModelCreator() throws ApexException {
39         final TestApexModel<AxModel> testApexModel =
40                 new TestApexModel<AxModel>(AxModel.class, new TestApexBasicModelCreator());
41
42         testApexModel.testApexModelValid();
43         try {
44             testApexModel.testApexModelVaidateObservation();
45             fail("Test should throw an exception");
46         } catch (final Exception e) {
47             assertEquals("model should have observations", e.getMessage());
48         }
49         testApexModel.testApexModelVaidateWarning();
50         testApexModel.testApexModelVaidateInvalidModel();
51         testApexModel.testApexModelVaidateMalstructured();
52
53         testApexModel.testApexModelWriteReadJSON();
54         testApexModel.testApexModelWriteReadXML();
55     }
56
57     @Test
58     public void testModelCreator0() throws ApexException {
59         final TestApexModel<AxModel> testApexModel =
60                 new TestApexModel<AxModel>(AxModel.class, new TestApexTestModelCreator0());
61
62         testApexModel.testApexModelValid();
63         try {
64             testApexModel.testApexModelVaidateObservation();
65             fail("Test should throw an exception");
66         } catch (final Exception e) {
67             assertEquals("model should have observations", e.getMessage());
68         }
69         try {
70             testApexModel.testApexModelVaidateWarning();
71             fail("Test should throw an exception");
72         } catch (final Exception e) {
73             assertEquals("model should have warnings", e.getMessage());
74         }
75         try {
76             testApexModel.testApexModelVaidateInvalidModel();
77             fail("Test should throw an exception");
78         } catch (final Exception e) {
79             assertEquals("should not be valid ***validation of model successful***", e.getMessage());
80         }
81         try {
82             testApexModel.testApexModelVaidateMalstructured();
83             fail("Test should throw an exception");
84         } catch (final Exception e) {
85             assertEquals("should not be valid ***validation of model successful***", e.getMessage());
86         }
87     }
88
89     @Test
90     public void testModelCreator1() throws ApexException {
91         final TestApexModel<AxModel> testApexModel =
92                 new TestApexModel<AxModel>(AxModel.class, new TestApexTestModelCreator1());
93
94         try {
95             testApexModel.testApexModelValid();
96             fail("Test should throw an exception");
97         } catch (final Exception e) {
98             assertTrue(e.getMessage().startsWith("model is invalid"));
99         }
100         try {
101             testApexModel.testApexModelVaidateObservation();
102             fail("Test should throw an exception");
103         } catch (final Exception e) {
104             assertTrue(e.getMessage().startsWith("model is invalid"));
105         }
106         try {
107             testApexModel.testApexModelVaidateWarning();
108             fail("Test should throw an exception");
109         } catch (final Exception e) {
110             assertTrue(e.getMessage().startsWith("model is invalid"));
111         }
112         testApexModel.testApexModelVaidateInvalidModel();
113         testApexModel.testApexModelVaidateMalstructured();
114     }
115
116     @Test
117     public void testModelCreator2() throws ApexException {
118         final TestApexModel<AxModel> testApexModel =
119                 new TestApexModel<AxModel>(AxModel.class, new TestApexTestModelCreator2());
120
121         testApexModel.testApexModelValid();
122         testApexModel.testApexModelVaidateObservation();
123         try {
124             testApexModel.testApexModelVaidateWarning();
125             fail("Test should throw an exception");
126         } catch (final Exception e) {
127             assertEquals("model should have warnings", e.getMessage());
128         }
129     }
130
131     @Test
132     public void testModelCreator1XMLJSON() throws ApexException {
133         final TestApexModel<AxModel> testApexModel =
134                 new TestApexModel<AxModel>(AxModel.class, new TestApexTestModelCreator1());
135
136         try {
137             testApexModel.testApexModelWriteReadJSON();
138             fail("Test should throw an exception");
139         } catch (final Exception e) {
140             assertTrue(e.getMessage().startsWith("error processing file"));
141         }
142
143         try {
144             testApexModel.testApexModelWriteReadXML();
145             fail("Test should throw an exception");
146         } catch (final Exception e) {
147             assertTrue(e.getMessage().startsWith("error processing file"));
148         }
149     }
150 }