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.models.acm.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.models.acm.concepts.AutomationCompositions;
30 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
31 import org.onap.policy.clamp.models.acm.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 AUTOMATION_COMPOSITION_JSON =
42 "src/test/resources/providers/TestAutomationCompositions.json";
43 private static final Coder CODER = new StandardCoder();
44 private static final AtomicInteger dbNameCounter = new AtomicInteger();
45 private static final String originalJson = ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION_JSON);
46 private static List<JpaAutomationComposition> jpaAutomationCompositions;
50 void beforeSetupDao() throws Exception {
51 var parameters = new PolicyModelsProviderParameters();
52 parameters.setDatabaseDriver("org.h2.Driver");
53 parameters.setName("PolicyProviderParameterGroup");
54 parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
55 parameters.setDatabaseUrl("jdbc:h2:mem:automationCompositionProviderTestDb" + dbNameCounter.getAndDecrement());
56 parameters.setDatabaseUser("policy");
57 parameters.setDatabasePassword("P01icY");
58 parameters.setPersistenceUnit("ToscaConceptTest");
60 pfDao = ModelsProvider.init(parameters);
61 var inputAutomationCompositions = CODER.decode(originalJson, AutomationCompositions.class);
62 jpaAutomationCompositions =
63 ProviderUtils.getJpaAndValidateList(inputAutomationCompositions.getAutomationCompositionList(),
64 JpaAutomationComposition::new, "AutomationCompositions");
66 pfDao.createCollection(jpaAutomationCompositions);
71 assertThat(new FilterRepositoryImpl().getPfDao()).isNotNull();
75 void testGetFilteredParams() {
76 var filterRepositoryImpl = new FilterRepositoryImpl() {
78 protected PfDao getPfDao() {
82 var result = filterRepositoryImpl.getFiltered(JpaAutomationComposition.class, null, null);
83 assertThat(result).hasSize(2);
85 result = filterRepositoryImpl.getFiltered(JpaAutomationComposition.class,
86 jpaAutomationCompositions.get(0).getName(), null);
87 assertThat(result).hasSize(1);
91 void testGetFiltered() {
92 var filterRepositoryImpl = new FilterRepositoryImpl() {
94 protected PfDao getPfDao() {
100 PfFilterParameters filterParams = PfFilterParameters
102 .name(jpaAutomationCompositions.get(0).getName())
106 var result = filterRepositoryImpl.getFiltered(JpaAutomationComposition.class, filterParams);
107 assertThat(result).hasSize(1);