2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository;
23 import static org.assertj.core.api.Assertions.assertThat;
25 import java.util.List;
26 import java.util.concurrent.atomic.AtomicInteger;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
30 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop;
31 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ProviderUtils;
32 import org.onap.policy.common.utils.coder.Coder;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.models.dao.PfDao;
36 import org.onap.policy.models.dao.PfFilterParameters;
37 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
38 import org.onap.policy.models.provider.impl.ModelsProvider;
40 class FilterRepositoryImplTest {
41 private static final String CONTROL_LOOP_JSON = "src/test/resources/providers/TestControlLoops.json";
42 private static final Coder CODER = new StandardCoder();
43 private static final AtomicInteger dbNameCounter = new AtomicInteger();
44 private static final String originalJson = ResourceUtils.getResourceAsString(CONTROL_LOOP_JSON);
45 private static List<JpaControlLoop> jpaControlLoops;
49 void beforeSetupDao() throws Exception {
50 var parameters = new PolicyModelsProviderParameters();
51 parameters.setDatabaseDriver("org.h2.Driver");
52 parameters.setName("PolicyProviderParameterGroup");
53 parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
54 parameters.setDatabaseUrl("jdbc:h2:mem:controlLoopProviderTestDb" + dbNameCounter.getAndDecrement());
55 parameters.setDatabaseUser("policy");
56 parameters.setDatabasePassword("P01icY");
57 parameters.setPersistenceUnit("ToscaConceptTest");
59 pfDao = ModelsProvider.init(parameters);
60 var inputControlLoops = CODER.decode(originalJson, ControlLoops.class);
61 jpaControlLoops = ProviderUtils.getJpaAndValidateList(inputControlLoops.getControlLoopList(),
62 JpaControlLoop::new, "ControlLoops");
64 pfDao.createCollection(jpaControlLoops);
69 assertThat(new FilterRepositoryImpl().getPfDao()).isNotNull();
73 void testGetFilteredParams() {
74 var filterRepositoryImpl = new FilterRepositoryImpl() {
76 protected PfDao getPfDao() {
80 var result = filterRepositoryImpl.getFiltered(JpaControlLoop.class, null, null);
81 assertThat(result).hasSize(2);
83 result = filterRepositoryImpl.getFiltered(JpaControlLoop.class, jpaControlLoops.get(0).getName(), null);
84 assertThat(result).hasSize(1);
88 void testGetFiltered() {
89 var filterRepositoryImpl = new FilterRepositoryImpl() {
91 protected PfDao getPfDao() {
97 PfFilterParameters filterParams = PfFilterParameters
99 .name(jpaControlLoops.get(0).getName())
103 var result = filterRepositoryImpl.getFiltered(JpaControlLoop.class, filterParams);
104 assertThat(result).hasSize(1);