add junit for AuthenticationMethodFactory 57/30857/1
authorLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Thu, 8 Feb 2018 12:00:12 +0000 (13:00 +0100)
committerLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Thu, 8 Feb 2018 12:00:12 +0000 (13:00 +0100)
Change-Id: I115792b73c62c50362a39fe45936fe294fe575dc
Issue-ID: SO-414
Signed-off-by: Lukasz Muszkieta <lukasz.muszkieta@nokia.com>
adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudIdentity.java
adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactory.java
adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodFactoryTest.java [new file with mode: 0644]
adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/authentication/AuthenticationMethodTest.java

index d0ba7e0..ba9a7d5 100644 (file)
 
 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());
        }
     }
index 85cb296..c9be2c7 100644 (file)
 \r
 package org.openecomp.mso.cloud.authentication;\r
 \r
-import java.io.IOException;\r
-import java.net.URISyntaxException;\r
+import com.woorea.openstack.keystone.model.Authentication;\r
 import java.util.Map;\r
 import java.util.concurrent.ConcurrentHashMap;\r
-\r
 import org.openecomp.mso.cloud.CloudIdentity;\r
 \r
-import com.woorea.openstack.keystone.model.Authentication;\r
-\r
 /**\r
  * This factory manages all the wrappers associated to authentication types.\r
- *\r
  */\r
 public final class AuthenticationMethodFactory {\r
 \r
@@ -58,7 +53,7 @@ public final class AuthenticationMethodFactory {
                }\r
        }\r
        \r
-       public static final synchronized Authentication getAuthenticationFor(CloudIdentity cloudIdentity) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException, URISyntaxException {\r
+       public static final synchronized Authentication getAuthenticationFor(CloudIdentity cloudIdentity) {\r
                if (cloudIdentity == null) {\r
                        throw new IllegalArgumentException("Cloud identity cannot be null");\r
                }\r
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 (file)
index 0000000..2cfce27
--- /dev/null
@@ -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");
+        }
+    }
+
+}
index ccfede7..b6c1c73 100644 (file)
@@ -25,16 +25,11 @@ package org.openecomp.mso.cloud.authentication;
 \r
 import static org.junit.Assert.assertTrue;\r
 \r
-import java.io.IOException;\r
-import java.net.URISyntaxException;\r
-\r
+import com.woorea.openstack.keystone.model.Authentication;\r
+import com.woorea.openstack.keystone.model.authentication.UsernamePassword;\r
 import org.junit.Test;\r
 import org.openecomp.mso.cloud.CloudIdentity;\r
 import org.openecomp.mso.cloud.authentication.models.RackspaceAuthentication;\r
-import org.openecomp.mso.openstack.exceptions.MsoException;\r
-\r
-import com.woorea.openstack.keystone.model.Authentication;\r
-import com.woorea.openstack.keystone.model.authentication.UsernamePassword;\r
 \r
 /**\r
  * A few JUnit tests to evaluate the new factory that manages authentication\r
@@ -50,52 +45,15 @@ public class AuthenticationMethodTest {
        public AuthenticationMethodTest() {\r
                // TODO Auto-generated constructor stub\r
        }\r
-       \r
-       @Test\r
-       public void testCustomRackspaceAuth() {\r
-               CloudIdentity ci = new CloudIdentity();\r
-               ci.setIdentityAuthenticationType(CloudIdentity.IdentityAuthenticationType.RACKSPACE_APIKEY);\r
-               ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");\r
-               ci.setMsoId("test");\r
-               \r
-               try {\r
-                       Authentication auth = AuthenticationMethodFactory.getAuthenticationFor(ci);\r
-                       assertTrue(RackspaceAuthentication.class.equals(auth.getClass()));\r
-               } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IOException\r
-                               | URISyntaxException e) {\r
-                       e.printStackTrace();\r
-               }\r
-       }\r
-       \r
-       @Test\r
-       public void testCoreUsernamePasswordAuth() {\r
-               CloudIdentity ci = new CloudIdentity();\r
-               ci.setIdentityAuthenticationType(CloudIdentity.IdentityAuthenticationType.USERNAME_PASSWORD);\r
-               ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");\r
-               ci.setMsoId("someuser");\r
-               \r
-               try {\r
-                       Authentication auth = AuthenticationMethodFactory.getAuthenticationFor(ci);\r
-                       assertTrue(UsernamePassword.class.equals(auth.getClass()));\r
-               } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IOException\r
-                               | URISyntaxException e) {\r
-                       e.printStackTrace();\r
-               }\r
-       }\r
-       \r
+\r
        @Test\r
        public void testCustomRackspaceAuthFromCloudIdentity() {\r
                CloudIdentity ci = new CloudIdentity();\r
                ci.setIdentityAuthenticationType(CloudIdentity.IdentityAuthenticationType.RACKSPACE_APIKEY);\r
                ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");\r
                ci.setMsoId("test");\r
-               \r
-               try {\r
-                       Authentication auth = ci.getAuthentication();\r
-                       assertTrue(RackspaceAuthentication.class.equals(auth.getClass()));\r
-               } catch (MsoException e) {\r
-                       e.printStackTrace();\r
-               }\r
+               Authentication auth = ci.getAuthentication();\r
+               assertTrue(RackspaceAuthentication.class.equals(auth.getClass()));\r
        }\r
        \r
        @Test\r
@@ -104,12 +62,7 @@ public class AuthenticationMethodTest {
                ci.setIdentityAuthenticationType(CloudIdentity.IdentityAuthenticationType.USERNAME_PASSWORD);\r
                ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");\r
                ci.setMsoId("someuser");\r
-               \r
-               try {\r
-                       Authentication auth = ci.getAuthentication();\r
-                       assertTrue(UsernamePassword.class.equals(auth.getClass()));\r
-               } catch (MsoException e) {\r
-                       e.printStackTrace();\r
-               }\r
+               Authentication auth = ci.getAuthentication();\r
+               assertTrue(UsernamePassword.class.equals(auth.getClass()));\r
        }\r
 }\r