2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.base;
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;
29 import org.junit.Test;
30 import org.onap.policy.models.base.testconcepts.DummyPfModel;
32 public class ModelServiceTest {
34 private static final String MODEL_KEY_IS_NULL = "modelKey is marked .*on.*ull but is null$";
35 private static final String MODEL_NAME = "ModelName";
38 public void testModelService() {
39 PfModelService.clear();
41 assertFalse(PfModelService.existsModel("NonExistantName"));
42 assertThatThrownBy(() -> PfModelService.getModel("NonExistantName"))
43 .hasMessage("Model for name NonExistantName not found in model service");
45 PfModelService.registerModel(MODEL_NAME, new DummyPfModel());
46 assertTrue(PfModelService.existsModel(MODEL_NAME));
47 assertNotNull(PfModelService.getModel(MODEL_NAME));
49 PfModelService.deregisterModel(MODEL_NAME);
51 assertFalse(PfModelService.existsModel(MODEL_NAME));
52 assertThatThrownBy(() -> PfModelService.getModel(MODEL_NAME))
53 .hasMessage("Model for name ModelName not found in model service");
55 PfModelService.registerModel(MODEL_NAME, new DummyPfModel());
56 assertTrue(PfModelService.existsModel(MODEL_NAME));
57 assertNotNull(PfModelService.getModel(MODEL_NAME));
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");
64 assertThatThrownBy(() -> PfModelService.registerModel(null, null)).hasMessageMatching(MODEL_KEY_IS_NULL);
66 assertThatThrownBy(() -> PfModelService.registerModel("nullModelName", null))
67 .hasMessageMatching("^model is marked .*on.*ull but is null$");
69 assertThatThrownBy(() -> PfModelService.registerModel(null, new DummyPfModel()))
70 .hasMessageMatching(MODEL_KEY_IS_NULL);
72 assertThatThrownBy(() -> PfModelService.deregisterModel(null)).hasMessageMatching(MODEL_KEY_IS_NULL);
74 assertThatThrownBy(() -> PfModelService.getModel(null)).hasMessageMatching(MODEL_KEY_IS_NULL);