0e790d3dc525fdeb4c71fb458c0c963e2d7746ab
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / ModelServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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.models.base;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import org.junit.Test;
30 import org.onap.policy.models.base.testconcepts.DummyPfModel;
31
32 public class ModelServiceTest {
33
34     @Test
35     public void testModelService() {
36         PfModelService.clear();
37
38         assertFalse(PfModelService.existsModel("NonExistantName"));
39         try {
40             PfModelService.getModel("NonExistantName");
41         } catch (final Exception e) {
42             assertEquals("Model for name NonExistantName not found in model service", e.getMessage());
43         }
44
45         PfModelService.registerModel("ModelName", new DummyPfModel());
46         assertTrue(PfModelService.existsModel("ModelName"));
47         assertNotNull(PfModelService.getModel("ModelName"));
48
49         PfModelService.deregisterModel("ModelName");
50
51         assertFalse(PfModelService.existsModel("ModelName"));
52         try {
53             PfModelService.getModel("ModelName");
54         } catch (final Exception e) {
55             assertEquals("Model for name ModelName not found in model service", e.getMessage());
56         }
57
58         PfModelService.registerModel("ModelName", new DummyPfModel());
59         assertTrue(PfModelService.existsModel("ModelName"));
60         assertNotNull(PfModelService.getModel("ModelName"));
61
62         PfModelService.clear();
63         assertFalse(PfModelService.existsModel("ModelName"));
64         try {
65             PfModelService.getModel("ModelName");
66         } catch (final Exception e) {
67             assertEquals("Model for name ModelName not found in model service", e.getMessage());
68         }
69
70         try {
71             PfModelService.registerModel(null, null);
72             fail("test should throw an exception");
73         } catch (Exception exc) {
74             assertEquals("modelKey is marked @NonNull but is null", exc.getMessage());
75         }
76
77         try {
78             PfModelService.registerModel("nullModelName", null);
79             fail("test should throw an exception");
80         } catch (Exception exc) {
81             assertEquals("model is marked @NonNull but is null", exc.getMessage());
82         }
83
84         try {
85             PfModelService.registerModel(null, new DummyPfModel());
86             fail("test should throw an exception");
87         } catch (Exception exc) {
88             assertEquals("modelKey is marked @NonNull but is null", exc.getMessage());
89         }
90
91         try {
92             PfModelService.deregisterModel(null);
93             fail("test should throw an exception");
94         } catch (Exception exc) {
95             assertEquals("modelKey is marked @NonNull but is null", exc.getMessage());
96         }
97
98         try {
99             PfModelService.getModel(null);
100             fail("test should throw an exception");
101         } catch (Exception exc) {
102             assertEquals("modelKey is marked @NonNull but is null", exc.getMessage());
103         }
104     }
105 }