Fix more sonar issues in models: yaml to dao
[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  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
29 import org.junit.Test;
30 import org.onap.policy.models.base.testconcepts.DummyPfModel;
31
32 public class ModelServiceTest {
33
34     private static final String MODEL_KEY_IS_NULL = "modelKey is marked @NonNull but is null";
35     private static final String MODEL_NAME = "ModelName";
36
37     @Test
38     public void testModelService() {
39         PfModelService.clear();
40
41         assertFalse(PfModelService.existsModel("NonExistantName"));
42         assertThatThrownBy(() -> PfModelService.getModel("NonExistantName"))
43                         .hasMessage("Model for name NonExistantName not found in model service");
44
45         PfModelService.registerModel(MODEL_NAME, new DummyPfModel());
46         assertTrue(PfModelService.existsModel(MODEL_NAME));
47         assertNotNull(PfModelService.getModel(MODEL_NAME));
48
49         PfModelService.deregisterModel(MODEL_NAME);
50
51         assertFalse(PfModelService.existsModel(MODEL_NAME));
52         assertThatThrownBy(() -> PfModelService.getModel(MODEL_NAME))
53                         .hasMessage("Model for name ModelName not found in model service");
54
55         PfModelService.registerModel(MODEL_NAME, new DummyPfModel());
56         assertTrue(PfModelService.existsModel(MODEL_NAME));
57         assertNotNull(PfModelService.getModel(MODEL_NAME));
58
59         PfModelService.clear();
60         assertFalse(PfModelService.existsModel(MODEL_NAME));
61         assertThatThrownBy(() -> PfModelService.getModel(MODEL_NAME))
62                         .hasMessage("Model for name ModelName not found in model service");
63
64         assertThatThrownBy(() -> PfModelService.registerModel(null, null))
65                         .hasMessage(MODEL_KEY_IS_NULL);
66
67         assertThatThrownBy(() -> PfModelService.registerModel("nullModelName", null))
68                         .hasMessage("model is marked @NonNull but is null");
69
70         assertThatThrownBy(() -> PfModelService.registerModel(null, new DummyPfModel()))
71                         .hasMessage(MODEL_KEY_IS_NULL);
72
73         assertThatThrownBy(() -> PfModelService.deregisterModel(null))
74                         .hasMessage(MODEL_KEY_IS_NULL);
75
76         assertThatThrownBy(() -> PfModelService.getModel(null)).hasMessage(MODEL_KEY_IS_NULL);
77     }
78 }