Improve testing stability
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / application-config-rest / application-config-rest-services / src / test / java / org / openecomp / sdcrests / applicationconfig / rest / services / ApplicationConfigurationImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdcrests.applicationconfig.rest.services;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.openecomp.core.utilities.applicationconfig.dao.type.ApplicationConfigEntity;
27 import org.openecomp.core.utilities.applicationconfig.type.ConfigurationData;
28 import org.openecomp.sdc.applicationconfig.ApplicationConfigManager;
29 import org.openecomp.sdcrests.applicationconfiguration.types.ApplicationConfigDto;
30 import org.openecomp.sdcrests.applicationconfiguration.types.ConfigurationDataDto;
31 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
32 import org.springframework.http.HttpStatus;
33
34 import javax.ws.rs.core.Response;
35 import java.io.ByteArrayInputStream;
36 import java.io.InputStream;
37 import java.util.ArrayList;
38
39 import static org.junit.Assert.assertEquals;
40 import static org.mockito.ArgumentMatchers.eq;
41 import static org.mockito.Mockito.verify;
42 import static org.mockito.Mockito.when;
43 import static org.mockito.MockitoAnnotations.openMocks;
44
45 public class ApplicationConfigurationImplTest {
46
47
48     private ApplicationConfigurationImpl applicationConfiguration;
49
50     @Mock
51     ApplicationConfigManager applicationConfigManager;
52
53     @Before
54     public void setUp() {
55         openMocks(this);
56         applicationConfiguration = new ApplicationConfigurationImpl(applicationConfigManager);
57     }
58
59     @Test
60     public void validateInsertInToTableCallsManagerFunctionWithValidParameters() {
61
62         final String testNamespace = "namespace";
63         final String testKey = "key";
64         final String testValue = "testingValue";
65         final InputStream testInput = new ByteArrayInputStream(testValue.getBytes());
66
67         Response response = applicationConfiguration.insertToTable(testNamespace, testKey, testInput);
68
69         assertEquals(response.getStatus(), HttpStatus.OK.value());
70         verify(applicationConfigManager).insertIntoTable(eq(testNamespace),eq(testKey),eq(testValue));
71     }
72
73     @Test
74     public void validateGetFromTableReturnsValidObject() {
75
76         final String testNamespace = "namespace";
77         final String testKey = "key";
78         final ConfigurationData testValue = new ConfigurationData("testValue", 111);
79
80         when(applicationConfigManager.getFromTable(eq(testNamespace),eq(testKey))).thenReturn(testValue);
81
82         Response response = applicationConfiguration.getFromTable(testNamespace, testKey);
83
84         assertEquals(response.getEntity().getClass(), ConfigurationDataDto.class);
85         assertEquals(((ConfigurationDataDto)response.getEntity()).getValue(),testValue.getValue());
86     }
87
88
89     @Test
90     public void validateGetListOfConfigurationByNamespaceFromTableReturnsValidList() {
91
92         final String testNamespace = "namespace";
93         final ArrayList<ApplicationConfigEntity> testApplicationConfigEntities = new ArrayList<>();
94         final ApplicationConfigEntity testConfigEntity01 = new ApplicationConfigEntity();
95         final String testValue01 = "testValue01";
96         final ApplicationConfigEntity testConfigEntity02 = new ApplicationConfigEntity();
97         final String testValue02 = "testValue02";
98         testConfigEntity01.setValue(testValue01);
99         testConfigEntity02.setValue(testValue02);
100         testApplicationConfigEntities.add(testConfigEntity01);
101         testApplicationConfigEntities.add(testConfigEntity02);
102
103         when(applicationConfigManager.getListOfConfigurationByNamespace(eq(testNamespace)))
104                 .thenReturn(testApplicationConfigEntities);
105
106         Response response = applicationConfiguration.getListOfConfigurationByNamespaceFromTable(testNamespace);
107
108         assertEquals(response.getEntity().getClass(), GenericCollectionWrapper.class);
109         assertEquals(
110                 ((GenericCollectionWrapper)response.getEntity()).getResults().size(),
111                 testApplicationConfigEntities.size()
112         );
113         assertEquals(
114                 ((ApplicationConfigDto)response.readEntity(GenericCollectionWrapper.class).getResults().get(0)).getValue(),
115                 testApplicationConfigEntities.get(0).getValue()
116         );
117         assertEquals(
118                 ((ApplicationConfigDto)response.readEntity(GenericCollectionWrapper.class).getResults().get(1)).getValue(),
119                 testApplicationConfigEntities.get(1).getValue()
120         );
121     }
122
123 }