From ef8c8012c037b0cefbdbf930819524e2050d7c87 Mon Sep 17 00:00:00 2001 From: Lukasz Muszkieta Date: Thu, 8 Feb 2018 13:00:12 +0100 Subject: add junit for AuthenticationMethodFactory Change-Id: I115792b73c62c50362a39fe45936fe294fe575dc Issue-ID: SO-414 Signed-off-by: Lukasz Muszkieta --- .../org/openecomp/mso/cloud/CloudIdentity.java | 27 ++---- .../AuthenticationMethodFactory.java | 9 +- .../AuthenticationMethodFactoryTest.java | 103 +++++++++++++++++++++ .../authentication/AuthenticationMethodTest.java | 61 ++---------- 4 files changed, 121 insertions(+), 79 deletions(-) create mode 100644 adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactoryTest.java (limited to 'adapters/mso-adapter-utils/src') diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudIdentity.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudIdentity.java index d0ba7e09fc..ba9a7d5007 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudIdentity.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudIdentity.java @@ -23,28 +23,23 @@ package org.openecomp.mso.cloud; -import java.io.IOException; -import java.net.URISyntaxException; +import com.woorea.openstack.keystone.model.Authentication; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; import java.security.GeneralSecurityException; - import org.codehaus.jackson.annotate.JsonProperty; import org.codehaus.jackson.map.annotate.JsonDeserialize; import org.codehaus.jackson.map.annotate.JsonSerialize; -import org.openecomp.mso.openstack.exceptions.MsoAdapterException; -import org.openecomp.mso.openstack.exceptions.MsoException; -import org.openecomp.mso.openstack.utils.MsoKeystoneUtils; -import org.openecomp.mso.openstack.utils.MsoTenantUtils; -import org.openecomp.mso.openstack.utils.MsoTenantUtilsFactory; import org.openecomp.mso.cloud.authentication.AuthenticationMethodFactory; import org.openecomp.mso.cloud.authentication.AuthenticationWrapper; import org.openecomp.mso.cloud.authentication.wrappers.RackspaceAPIKeyWrapper; import org.openecomp.mso.cloud.authentication.wrappers.UsernamePasswordWrapper; import org.openecomp.mso.logger.MessageEnum; import org.openecomp.mso.logger.MsoLogger; - -import com.woorea.openstack.keystone.model.authentication.UsernamePassword; +import org.openecomp.mso.openstack.exceptions.MsoException; +import org.openecomp.mso.openstack.utils.MsoKeystoneUtils; +import org.openecomp.mso.openstack.utils.MsoTenantUtils; +import org.openecomp.mso.openstack.utils.MsoTenantUtilsFactory; import org.openecomp.mso.utils.CryptoUtils; -import com.woorea.openstack.keystone.model.Authentication; /** * JavaBean JSON class for a CloudIdentity. This bean represents a cloud identity @@ -143,15 +138,11 @@ public class CloudIdentity { } } } - - public Authentication getAuthentication () throws MsoException { + + public Authentication getAuthentication() { if (this.getIdentityAuthenticationType() != null) { - try { return AuthenticationMethodFactory.getAuthenticationFor(this); - } catch (IllegalAccessException | InstantiationException | ClassNotFoundException | IOException | URISyntaxException e) { - throw new MsoAdapterException("Could not retrieve authentication for " + this.identityAuthenticationType, e); - } - } else { // Fallback + } else { return new UsernamePassword(this.getMsoId(), this.getMsoPass()); } } diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactory.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactory.java index 85cb2967d6..c9be2c7949 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactory.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactory.java @@ -23,18 +23,13 @@ package org.openecomp.mso.cloud.authentication; -import java.io.IOException; -import java.net.URISyntaxException; +import com.woorea.openstack.keystone.model.Authentication; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; - import org.openecomp.mso.cloud.CloudIdentity; -import com.woorea.openstack.keystone.model.Authentication; - /** * This factory manages all the wrappers associated to authentication types. - * */ public final class AuthenticationMethodFactory { @@ -58,7 +53,7 @@ public final class AuthenticationMethodFactory { } } - public static final synchronized Authentication getAuthenticationFor(CloudIdentity cloudIdentity) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException, URISyntaxException { + public static final synchronized Authentication getAuthenticationFor(CloudIdentity cloudIdentity) { if (cloudIdentity == null) { throw new IllegalArgumentException("Cloud identity cannot be null"); } diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactoryTest.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactoryTest.java new file mode 100644 index 0000000000..2cfce276d8 --- /dev/null +++ b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactoryTest.java @@ -0,0 +1,103 @@ +/* + * ============LICENSE_START========================================== + * =================================================================== + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + * + */ + +package org.openecomp.mso.cloud.authentication; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.woorea.openstack.keystone.model.Authentication; +import org.junit.Test; +import org.openecomp.mso.cloud.CloudIdentity; +import org.openecomp.mso.cloud.CloudIdentity.IdentityAuthenticationType; +import org.openecomp.mso.cloud.authentication.wrappers.RackspaceAPIKeyWrapper; + +public class AuthenticationMethodFactoryTest { + + private static final Class WRAPPER_CLASS = RackspaceAPIKeyWrapper.class; + private static final String AUTHENTICATION_TYPE = "authenticationTest"; + + @Test + public void register_NoExceptionThrown() throws IllegalAccessException, InstantiationException { + AuthenticationMethodFactory.register(AUTHENTICATION_TYPE, WRAPPER_CLASS); + } + + @Test + public void register_throwExceptionWhenAuthTypeIsNull() throws InstantiationException, IllegalAccessException { + try { + AuthenticationMethodFactory.register(null, WRAPPER_CLASS); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage()).isNotEmpty().contains("Authentication Type to register cannot be null " + + "or an empty name string"); + } + } + + @Test + public void register_throwExceptionWhenAuthTypeIsEmpty() throws InstantiationException, IllegalAccessException { + try { + AuthenticationMethodFactory.register("", WRAPPER_CLASS); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage()).isNotEmpty().contains("Authentication Type to register cannot be null " + + "or an empty name string"); + } + } + + @Test + public void register_throwExceptionWhenWrapperIsNull() throws IllegalAccessException, InstantiationException { + try { + AuthenticationMethodFactory.register(AUTHENTICATION_TYPE, null); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage()).isNotEmpty() + .contains("Wrapper Class to register for Authentication cannot be null"); + } + } + + @Test + public void getAuthentication_NoExceptionThrown() { + CloudIdentity cloudIdentity = new CloudIdentity(); + cloudIdentity.setIdentityAuthenticationType(IdentityAuthenticationType.RACKSPACE_APIKEY); + cloudIdentity.setMsoId("msoIdTest"); + cloudIdentity.setMsoPass("123"); + Authentication result = AuthenticationMethodFactory.getAuthenticationFor(cloudIdentity); + assertThat(result).isNotNull(); + } + + @Test + public void getAuthentication_ThrowExWhenCloudSiteIsNull() { + try { + AuthenticationMethodFactory.getAuthenticationFor(null); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage()).isNotEmpty().contains("Cloud identity cannot be null"); + } + } + + @Test + public void getAuthentication_ThrowExWhenIdentityAuthenticationTypeIsNotSet() { + try { + AuthenticationMethodFactory.getAuthenticationFor(new CloudIdentity()); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage()).isNotEmpty() + .contains("Cloud identity authentication type cannot be null or empty"); + } + } + +} diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodTest.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodTest.java index ccfede7297..b6c1c7373f 100644 --- a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodTest.java +++ b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodTest.java @@ -25,16 +25,11 @@ package org.openecomp.mso.cloud.authentication; import static org.junit.Assert.assertTrue; -import java.io.IOException; -import java.net.URISyntaxException; - +import com.woorea.openstack.keystone.model.Authentication; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; import org.junit.Test; import org.openecomp.mso.cloud.CloudIdentity; import org.openecomp.mso.cloud.authentication.models.RackspaceAuthentication; -import org.openecomp.mso.openstack.exceptions.MsoException; - -import com.woorea.openstack.keystone.model.Authentication; -import com.woorea.openstack.keystone.model.authentication.UsernamePassword; /** * A few JUnit tests to evaluate the new factory that manages authentication @@ -50,52 +45,15 @@ public class AuthenticationMethodTest { public AuthenticationMethodTest() { // TODO Auto-generated constructor stub } - - @Test - public void testCustomRackspaceAuth() { - CloudIdentity ci = new CloudIdentity(); - ci.setIdentityAuthenticationType(CloudIdentity.IdentityAuthenticationType.RACKSPACE_APIKEY); - ci.setMsoPass("FD205490A48D48475607C36B9AD902BF"); - ci.setMsoId("test"); - - try { - Authentication auth = AuthenticationMethodFactory.getAuthenticationFor(ci); - assertTrue(RackspaceAuthentication.class.equals(auth.getClass())); - } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IOException - | URISyntaxException e) { - e.printStackTrace(); - } - } - - @Test - public void testCoreUsernamePasswordAuth() { - CloudIdentity ci = new CloudIdentity(); - ci.setIdentityAuthenticationType(CloudIdentity.IdentityAuthenticationType.USERNAME_PASSWORD); - ci.setMsoPass("FD205490A48D48475607C36B9AD902BF"); - ci.setMsoId("someuser"); - - try { - Authentication auth = AuthenticationMethodFactory.getAuthenticationFor(ci); - assertTrue(UsernamePassword.class.equals(auth.getClass())); - } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IOException - | URISyntaxException e) { - e.printStackTrace(); - } - } - + @Test public void testCustomRackspaceAuthFromCloudIdentity() { CloudIdentity ci = new CloudIdentity(); ci.setIdentityAuthenticationType(CloudIdentity.IdentityAuthenticationType.RACKSPACE_APIKEY); ci.setMsoPass("FD205490A48D48475607C36B9AD902BF"); ci.setMsoId("test"); - - try { - Authentication auth = ci.getAuthentication(); - assertTrue(RackspaceAuthentication.class.equals(auth.getClass())); - } catch (MsoException e) { - e.printStackTrace(); - } + Authentication auth = ci.getAuthentication(); + assertTrue(RackspaceAuthentication.class.equals(auth.getClass())); } @Test @@ -104,12 +62,7 @@ public class AuthenticationMethodTest { ci.setIdentityAuthenticationType(CloudIdentity.IdentityAuthenticationType.USERNAME_PASSWORD); ci.setMsoPass("FD205490A48D48475607C36B9AD902BF"); ci.setMsoId("someuser"); - - try { - Authentication auth = ci.getAuthentication(); - assertTrue(UsernamePassword.class.equals(auth.getClass())); - } catch (MsoException e) { - e.printStackTrace(); - } + Authentication auth = ci.getAuthentication(); + assertTrue(UsernamePassword.class.equals(auth.getClass())); } } -- cgit 1.2.3-korg