Replaced all tabs with spaces in java and pom.xml
[so.git] / adapters / mso-adapter-utils / src / test / java / org / onap / so / openstack / utils / MsoTenantUtilsFactoryTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright 2019 Nokia
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.openstack.utils;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.catchThrowableOfType;
27 import static org.mockito.BDDMockito.given;
28 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
29 import static org.mockito.Mockito.mock;
30 import java.util.Optional;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.onap.so.cloud.CloudConfig;
37 import org.onap.so.db.catalog.beans.CloudSite;
38 import org.onap.so.db.catalog.beans.ServerType;
39 import org.onap.so.openstack.exceptions.MsoCloudSiteNotFound;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class MsoTenantUtilsFactoryTest {
43
44     @Mock
45     private CloudConfig cloudConfig;
46     @Mock
47     private MsoKeystoneUtils msoKeystoneUtils;
48     @Mock
49     private MsoKeystoneV3Utils msoKeystoneV3Utils;
50     @InjectMocks
51     private MsoTenantUtilsFactory msoTenantUtilsFactory;
52
53     @Test
54     public void getTenantUtils_shouldThrowException_whenNoCloudSiteFoundForGivenId() {
55         // GIVEN
56         String cloudSiteId = "CloudSiteId";
57         given(cloudConfig.getCloudSite(cloudSiteId)).willReturn(Optional.empty());
58
59         // WHEN
60         MsoCloudSiteNotFound msoCloudSiteNotFound = catchThrowableOfType(
61                 () -> msoTenantUtilsFactory.getTenantUtils(cloudSiteId), MsoCloudSiteNotFound.class);
62
63         // THEN
64         assertThat(msoCloudSiteNotFound.getMessage()).contains(cloudSiteId);
65     }
66
67     @Test
68     public void getTenantUtils_shouldReturnNull_forInvalidServerType() throws MsoCloudSiteNotFound {
69         // GIVEN
70         String cloudSiteId = "CloudSiteId";
71         CloudSite cloudSite = mock(CloudSite.class, RETURNS_DEEP_STUBS);
72         given(cloudConfig.getCloudSite(cloudSiteId)).willReturn(Optional.of(cloudSite));
73
74         // WHEN
75         MsoTenantUtils tenantUtils = msoTenantUtilsFactory.getTenantUtils(cloudSiteId);
76
77         // THEN
78         assertThat(tenantUtils).isNull();
79     }
80
81     @Test
82     public void getTenantUtils_shouldReturnKeystoneUtils_forKeystoneServerType() throws MsoCloudSiteNotFound {
83         shouldReturnAppropriateUtilsInstanceForGivenServerType(ServerType.KEYSTONE, msoKeystoneUtils);
84     }
85
86     @Test
87     public void getTenantUtils_shouldReturnKeystoneV3Utils_forKeystoneV3ServerType() throws MsoCloudSiteNotFound {
88         shouldReturnAppropriateUtilsInstanceForGivenServerType(ServerType.KEYSTONE_V3, msoKeystoneV3Utils);
89     }
90
91     private <T extends MsoTenantUtils> void shouldReturnAppropriateUtilsInstanceForGivenServerType(
92             ServerType serverType, T expectedInstance) throws MsoCloudSiteNotFound {
93         // GIVEN
94         String cloudSiteId = "CloudSiteId";
95         CloudSite cloudSite = mock(CloudSite.class, RETURNS_DEEP_STUBS);
96         given(cloudSite.getIdentityService().getIdentityServerType()).willReturn(serverType);
97         given(cloudConfig.getCloudSite(cloudSiteId)).willReturn(Optional.of(cloudSite));
98
99         // WHEN
100         MsoTenantUtils tenantUtils = msoTenantUtilsFactory.getTenantUtils(cloudSiteId);
101
102         // THEN
103         assertThat(tenantUtils).isEqualTo(expectedInstance);
104     }
105 }