c441c2fe8a225a9df5be19e3b619955c5c168d97
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 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.clamp.models.acm.persistence.repository;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.concurrent.atomic.AtomicInteger;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.onap.policy.clamp.models.acm.concepts.Participant;
31 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaParticipant;
32 import org.onap.policy.clamp.models.acm.persistence.provider.ProviderUtils;
33 import org.onap.policy.common.utils.coder.Coder;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36 import org.onap.policy.models.dao.PfDao;
37 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
38 import org.onap.policy.models.provider.impl.ModelsProvider;
39
40 class FilterRepositoryImplTest {
41     private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json";
42     private final List<Participant> inputParticipants = new ArrayList<>();
43     private List<JpaParticipant> jpaParticipantList;
44     private final String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_JSON);
45     private static final Coder CODER = new StandardCoder();
46
47     private static final AtomicInteger dbNameCounter = new AtomicInteger();
48     private PfDao pfDao;
49
50     @BeforeEach
51     void beforeSetupDao() throws Exception {
52         var parameters = new PolicyModelsProviderParameters();
53         parameters.setDatabaseDriver("org.h2.Driver");
54         parameters.setName("PolicyProviderParameterGroup");
55         parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
56         parameters.setDatabaseUrl("jdbc:h2:mem:automationCompositionProviderTestDb" + dbNameCounter.getAndDecrement());
57         parameters.setDatabaseUser("policy");
58         parameters.setDatabasePassword("P01icY");
59         parameters.setPersistenceUnit("ToscaConceptTest");
60
61         pfDao = ModelsProvider.init(parameters);
62         inputParticipants.add(CODER.decode(originalJson, Participant.class));
63         jpaParticipantList = ProviderUtils.getJpaAndValidateList(inputParticipants, JpaParticipant::new, "participant");
64         pfDao.createCollection(jpaParticipantList);
65     }
66
67     @Test
68     void testGetPfDao() {
69         assertThat(new FilterRepositoryImpl().getPfDao()).isNotNull();
70     }
71
72     @Test
73     void testGetFilteredParams() {
74         var filterRepositoryImpl = new FilterRepositoryImpl() {
75             @Override
76             protected PfDao getPfDao() {
77                 return pfDao;
78             }
79         };
80         var result = filterRepositoryImpl.getFiltered(JpaParticipant.class, null, null);
81         assertThat(result).hasSize(1);
82
83         result = filterRepositoryImpl.getFiltered(JpaParticipant.class, jpaParticipantList.get(0).getName(), null);
84         assertThat(result).hasSize(1);
85     }
86 }