e632a521ed9af7f32c2db754e69a07070e196107
[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 public class SupportBasicModelTester {
33
34     @Test
35     public void testNormalModelCreator() throws ApexException {
36         final TestApexModel<AxModel> testApexModel = new TestApexModel<AxModel>(AxModel.class,
37                         new DummyApexBasicModelCreator());
38
39         testApexModel.testApexModelValid();
40         try {
41             testApexModel.testApexModelVaidateObservation();
42             fail("Test should throw an exception");
43         } catch (final Exception e) {
44             assertEquals("model should have observations", e.getMessage());
45         }
46         testApexModel.testApexModelVaidateWarning();
47         testApexModel.testApexModelVaidateInvalidModel();
48         testApexModel.testApexModelVaidateMalstructured();
49
50         testApexModel.testApexModelWriteReadJson();
51         testApexModel.testApexModelWriteReadXml();
52     }
53
54     @Test
55     public void testModelCreator0() throws ApexException {
56         final TestApexModel<AxModel> testApexModel = new TestApexModel<AxModel>(AxModel.class,
57                         new TestApexModelCreator0Test());
58
59         testApexModel.testApexModelValid();
60         try {
61             testApexModel.testApexModelVaidateObservation();
62             fail("Test should throw an exception");
63         } catch (final Exception e) {
64             assertEquals("model should have observations", e.getMessage());
65         }
66         try {
67             testApexModel.testApexModelVaidateWarning();
68             fail("Test should throw an exception");
69         } catch (final Exception e) {
70             assertEquals("model should have warnings", e.getMessage());
71         }
72         try {
73             testApexModel.testApexModelVaidateInvalidModel();
74             fail("Test should throw an exception");
75         } catch (final Exception e) {
76             assertEquals("model should not be valid ***validation of model successful***", e.getMessage());
77         }
78         try {
79             testApexModel.testApexModelVaidateMalstructured();
80             fail("Test should throw an exception");
81         } catch (final Exception e) {
82             assertEquals("model should not be valid ***validation of model successful***", e.getMessage());
83         }
84     }
85
86     @Test
87     public void testModelCreator1() throws ApexException {
88         final TestApexModel<AxModel> testApexModel = new TestApexModel<AxModel>(AxModel.class,
89                         new TestApexModelCreator1Test());
90
91         try {
92             testApexModel.testApexModelValid();
93             fail("Test should throw an exception");
94         } catch (final Exception e) {
95             assertTrue(e.getMessage().startsWith("model is invalid"));
96         }
97         try {
98             testApexModel.testApexModelVaidateObservation();
99             fail("Test should throw an exception");
100         } catch (final Exception e) {
101             assertTrue(e.getMessage().startsWith("model is invalid"));
102         }
103         try {
104             testApexModel.testApexModelVaidateWarning();
105             fail("Test should throw an exception");
106         } catch (final Exception e) {
107             assertTrue(e.getMessage().startsWith("model is invalid"));
108         }
109         testApexModel.testApexModelVaidateInvalidModel();
110         testApexModel.testApexModelVaidateMalstructured();
111     }
112
113     @Test
114     public void testModelCreator2() throws ApexException {
115         final TestApexModel<AxModel> testApexModel = new TestApexModel<AxModel>(AxModel.class,
116                         new TestApexModelCreator2Test());
117
118         testApexModel.testApexModelValid();
119         testApexModel.testApexModelVaidateObservation();
120         try {
121             testApexModel.testApexModelVaidateWarning();
122             fail("Test should throw an exception");
123         } catch (final Exception e) {
124             assertEquals("model should have warnings", e.getMessage());
125         }
126     }
127
128     @Test
129     public void testModelCreator1XmlJson() throws ApexException {
130         final TestApexModel<AxModel> testApexModel = new TestApexModel<AxModel>(AxModel.class,
131                         new TestApexModelCreator1Test());
132
133         try {
134             testApexModel.testApexModelWriteReadJson();
135             fail("Test should throw an exception");
136         } catch (final Exception e) {
137             assertTrue(e.getMessage().startsWith("error processing file"));
138         }
139
140         try {
141             testApexModel.testApexModelWriteReadXml();
142             fail("Test should throw an exception");
143         } catch (final Exception e) {
144             assertTrue(e.getMessage().startsWith("error processing file"));
145         }
146     }
147 }