Configure "portal app_password" in DB by environment variable
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / WebConfigTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2020 AT&T Intellectual Property. 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.onap.vid.controller;
22
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.equalTo;
25 import static org.mockito.Mockito.when;
26 import static org.onap.vid.testUtils.TestUtils.setStringsInStringFields;
27
28 import javax.inject.Inject;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.onap.portalsdk.core.domain.App;
33 import org.onap.portalsdk.core.service.DataAccessService;
34 import org.onap.portalsdk.core.util.SystemProperties;
35 import org.onap.vid.config.DataSourceConfig;
36 import org.onap.vid.properties.Features;
37 import org.springframework.test.context.ContextConfiguration;
38 import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
41 import org.togglz.core.manager.FeatureManager;
42
43 @ContextConfiguration(classes = {DataSourceConfig.class, SystemProperties.class})
44 public class WebConfigTest extends AbstractTestNGSpringContextTests {
45
46     @Inject private DataAccessService dataAccessService;
47     @Mock FeatureManager featureManager;
48     @InjectMocks WebConfig webConfig;
49
50     @BeforeMethod
51     public void initMocks() {
52         MockitoAnnotations.initMocks(this);
53         webConfig.dataAccessService = dataAccessService;
54
55         // set default app values
56         App defaultApp = setStringsInStringFields(new App());
57         defaultApp.setId(1L);
58         dataAccessService.saveDomainObject(defaultApp, null);
59
60         // enable feature
61         when(featureManager.isActive(Features.FLAG_GUILIN_CONFIG_PORTAL_APP_PASSWORD)).thenReturn(true);
62     }
63
64     @Test
65     public void persistPortalAppPassword_givenFlagAndValue_thenValueIsPersisted() {
66         assertThat(setAndGetPortalAppPassword("fresh password"),
67             equalTo("fresh password"));
68     }
69
70     @Test
71     public void persistPortalAppPassword_givenEmptyValue_thenDbValueUnchanged() {
72         assertThat(setAndGetPortalAppPassword(""),
73             equalTo("appPassword"));
74     }
75
76     @Test
77     public void persistPortalAppPassword_givenNullValue_thenDbValueUnchanged() {
78         assertThat(setAndGetPortalAppPassword(null),
79             equalTo("appPassword"));
80     }
81
82     public String setAndGetPortalAppPassword(String password) {
83         webConfig.portalAppPassword = password;
84
85         webConfig.persistPortalAppPassword();
86         return appDomainPasswordFromDB();
87     }
88
89     private String appDomainPasswordFromDB() {
90         return ((App) dataAccessService.getDomainObject(App.class, 1L, null))
91             .getAppPassword();
92     }
93 }