57df96089204001b5126e693ad0edd5016538d0c
[policy/apex-pdp.git] / model / basic-model / src / test / java / org / onap / policy / apex / model / basicmodel / handling / SupportBasicModelTester.java
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 testModelsUnequal() throws ApexException {
56         final TestApexModel<AxModel> testApexModel0 = new TestApexModel<AxModel>(AxModel.class,
57             new DummyApexBasicModelCreator());
58         final TestApexModel<AxModel> testApexModel1 = new TestApexModel<AxModel>(AxModel.class,
59             new DummyApexBasicModelCreator());
60
61         testApexModel1.getModel().getKey().setVersion("0.0.2");
62
63         try {
64             testApexModel0.checkModelEquality(testApexModel0.getModel(), testApexModel1.getModel(),
65                 "Models are not equal");
66             fail("test should throw an exception here");
67         } catch (ApexException ae) {
68             assertEquals("Models are not equal", ae.getMessage());
69         }
70     }
71
72     @Test
73     public void testModelCreator0() throws ApexException {
74         final TestApexModel<AxModel> testApexModel = new TestApexModel<AxModel>(AxModel.class,
75             new SupportApexModelCreator0());
76
77         testApexModel.testApexModelValid();
78         try {
79             testApexModel.testApexModelVaidateObservation();
80             fail("Test should throw an exception");
81         } catch (final Exception e) {
82             assertEquals("model should have observations", e.getMessage());
83         }
84         try {
85             testApexModel.testApexModelVaidateWarning();
86             fail("Test should throw an exception");
87         } catch (final Exception e) {
88             assertEquals("model should have warnings", e.getMessage());
89         }
90         try {
91             testApexModel.testApexModelVaidateInvalidModel();
92             fail("Test should throw an exception");
93         } catch (final Exception e) {
94             assertEquals("model should not be valid ***validation of model successful***", e.getMessage());
95         }
96         try {
97             testApexModel.testApexModelVaidateMalstructured();
98             fail("Test should throw an exception");
99         } catch (final Exception e) {
100             assertEquals("model should not be valid ***validation of model successful***", e.getMessage());
101         }
102     }
103
104     @Test
105     public void testModelCreator1() throws ApexException {
106         final TestApexModel<AxModel> testApexModel = new TestApexModel<AxModel>(AxModel.class,
107             new SupportApexModelCreator1());
108
109         try {
110             testApexModel.testApexModelValid();
111             fail("Test should throw an exception");
112         } catch (final Exception e) {
113             assertTrue(e.getMessage().startsWith("model is invalid"));
114         }
115         try {
116             testApexModel.testApexModelVaidateObservation();
117             fail("Test should throw an exception");
118         } catch (final Exception e) {
119             assertTrue(e.getMessage().startsWith("model is invalid"));
120         }
121         try {
122             testApexModel.testApexModelVaidateWarning();
123             fail("Test should throw an exception");
124         } catch (final Exception e) {
125             assertTrue(e.getMessage().startsWith("model is invalid"));
126         }
127         testApexModel.testApexModelVaidateInvalidModel();
128         testApexModel.testApexModelVaidateMalstructured();
129     }
130
131     @Test
132     public void testModelCreator2() throws ApexException {
133         final TestApexModel<AxModel> testApexModel = new TestApexModel<AxModel>(AxModel.class,
134             new SupportApexModelCreator2());
135
136         testApexModel.testApexModelValid();
137         testApexModel.testApexModelVaidateObservation();
138         try {
139             testApexModel.testApexModelVaidateWarning();
140             fail("Test should throw an exception");
141         } catch (final Exception e) {
142             assertEquals("model should have warnings", e.getMessage());
143         }
144     }
145
146     @Test
147     public void testModelCreator1XmlJson() throws ApexException {
148         final TestApexModel<AxModel> testApexModel = new TestApexModel<AxModel>(AxModel.class,
149             new SupportApexModelCreator1());
150
151         try {
152             testApexModel.testApexModelWriteReadJson();
153             fail("Test should throw an exception");
154         } catch (final Exception e) {
155             assertTrue(e.getMessage().startsWith("error processing file"));
156         }
157
158         try {
159             testApexModel.testApexModelWriteReadXml();
160             fail("Test should throw an exception");
161         } catch (final Exception e) {
162             assertTrue(e.getMessage().startsWith("error processing file"));
163         }
164     }
165 }