2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.openstack.utils;
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;
41 @RunWith(MockitoJUnitRunner.class)
42 public class MsoTenantUtilsFactoryTest {
45 private CloudConfig cloudConfig;
47 private MsoKeystoneUtils msoKeystoneUtils;
49 private MsoKeystoneV3Utils msoKeystoneV3Utils;
51 private MsoTenantUtilsFactory msoTenantUtilsFactory;
54 public void getTenantUtils_shouldThrowException_whenNoCloudSiteFoundForGivenId() {
56 String cloudSiteId = "CloudSiteId";
57 given(cloudConfig.getCloudSite(cloudSiteId)).willReturn(Optional.empty());
60 MsoCloudSiteNotFound msoCloudSiteNotFound = catchThrowableOfType(
61 () -> msoTenantUtilsFactory.getTenantUtils(cloudSiteId), MsoCloudSiteNotFound.class);
64 assertThat(msoCloudSiteNotFound.getMessage()).contains(cloudSiteId);
68 public void getTenantUtils_shouldReturnNull_forInvalidServerType() throws MsoCloudSiteNotFound {
70 String cloudSiteId = "CloudSiteId";
71 CloudSite cloudSite = mock(CloudSite.class, RETURNS_DEEP_STUBS);
72 given(cloudConfig.getCloudSite(cloudSiteId)).willReturn(Optional.of(cloudSite));
75 MsoTenantUtils tenantUtils = msoTenantUtilsFactory.getTenantUtils(cloudSiteId);
78 assertThat(tenantUtils).isNull();
82 public void getTenantUtils_shouldReturnKeystoneUtils_forKeystoneServerType() throws MsoCloudSiteNotFound {
83 shouldReturnAppropriateUtilsInstanceForGivenServerType(ServerType.KEYSTONE, msoKeystoneUtils);
87 public void getTenantUtils_shouldReturnKeystoneV3Utils_forKeystoneV3ServerType() throws MsoCloudSiteNotFound {
88 shouldReturnAppropriateUtilsInstanceForGivenServerType(ServerType.KEYSTONE_V3, msoKeystoneV3Utils);
91 private <T extends MsoTenantUtils> void shouldReturnAppropriateUtilsInstanceForGivenServerType(
92 ServerType serverType, T expectedInstance) throws MsoCloudSiteNotFound {
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));
100 MsoTenantUtils tenantUtils = msoTenantUtilsFactory.getTenantUtils(cloudSiteId);
103 assertThat(tenantUtils).isEqualTo(expectedInstance);