7210a6afdc2d409ab7e57f14fb17201acce8255a
[policy/clamp.git] /
1 /*-
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
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.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;
39
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;
47     private PfDao pfDao;
48
49     @BeforeEach
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");
59
60         pfDao = ModelsProvider.init(parameters);
61         var inputAutomationCompositions = CODER.decode(originalJson, AutomationCompositions.class);
62         jpaAutomationCompositions =
63             ProviderUtils.getJpaAndValidateList(inputAutomationCompositions.getAutomationCompositionList(),
64                 JpaAutomationComposition::new, "AutomationCompositions");
65
66         pfDao.createCollection(jpaAutomationCompositions);
67     }
68
69     @Test
70     void testGetPfDao() {
71         assertThat(new FilterRepositoryImpl().getPfDao()).isNotNull();
72     }
73
74     @Test
75     void testGetFilteredParams() {
76         var filterRepositoryImpl = new FilterRepositoryImpl() {
77             @Override
78             protected PfDao getPfDao() {
79                 return pfDao;
80             }
81         };
82         var result = filterRepositoryImpl.getFiltered(JpaAutomationComposition.class, null, null);
83         assertThat(result).hasSize(2);
84
85         result = filterRepositoryImpl.getFiltered(JpaAutomationComposition.class,
86             jpaAutomationCompositions.get(0).getName(), null);
87         assertThat(result).hasSize(1);
88     }
89
90     @Test
91     void testGetFiltered() {
92         var filterRepositoryImpl = new FilterRepositoryImpl() {
93             @Override
94             protected PfDao getPfDao() {
95                 return pfDao;
96             }
97         };
98
99         // @formatter:off
100         PfFilterParameters filterParams = PfFilterParameters
101                 .builder()
102                 .name(jpaAutomationCompositions.get(0).getName())
103                 .build();
104         // @formatter:on
105
106         var result = filterRepositoryImpl.getFiltered(JpaAutomationComposition.class, filterParams);
107         assertThat(result).hasSize(1);
108     }
109 }