2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.cloud.authentication;
23 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
24 import static org.junit.Assert.assertThat;
25 import static org.junit.Assert.assertTrue;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
31 import org.junit.Test;
32 import org.onap.so.cloud.authentication.models.RackspaceAuthentication;
33 import org.onap.so.db.catalog.beans.AuthenticationType;
34 import org.onap.so.db.catalog.beans.CloudIdentity;
35 import org.onap.so.utils.CryptoUtils;
37 import com.fasterxml.jackson.core.JsonParseException;
38 import com.fasterxml.jackson.databind.JsonMappingException;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40 import com.woorea.openstack.keystone.model.Authentication;
41 import com.woorea.openstack.keystone.model.authentication.UsernamePassword;
44 * A few JUnit tests to evaluate the new factory that manages authentication
45 * types and their associated wrapper classes. Here it is assumed that core types
49 public class AuthenticationMethodTest {
51 private AuthenticationMethodFactory authenticationMethodFactory = new AuthenticationMethodFactory();
55 public AuthenticationMethodTest() {
56 // TODO Auto-generated constructor stub
60 public void testCustomRackspaceAuth() {
61 CloudIdentity ci = new CloudIdentity();
62 ci.setIdentityAuthenticationType(AuthenticationType.RACKSPACE_APIKEY);
63 ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
66 Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
67 assertTrue(RackspaceAuthentication.class.equals(auth.getClass()));
72 public void testCoreUsernamePasswordAuth() {
73 CloudIdentity ci = new CloudIdentity();
74 ci.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD);
75 ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
76 ci.setMsoId("someuser");
78 Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
79 assertTrue(UsernamePassword.class.equals(auth.getClass()));
84 public void testCustomRackspaceAuthFromCloudIdentity() {
85 CloudIdentity ci = new CloudIdentity();
86 ci.setIdentityAuthenticationType(AuthenticationType.RACKSPACE_APIKEY);
87 ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
90 Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
91 assertTrue(RackspaceAuthentication.class.equals(auth.getClass()));
95 public void testCoreUsernamePasswordAuthFromCloudIdentity() {
96 CloudIdentity ci = new CloudIdentity();
97 ci.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD);
98 ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
99 ci.setMsoId("someuser");
101 Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
102 assertTrue(UsernamePassword.class.equals(auth.getClass()));
107 public void getAuthenticationForV3Test() throws JsonParseException, JsonMappingException, IOException {
109 CloudIdentity identity = new CloudIdentity();
110 identity.setMsoId("my-username");
111 identity.setMsoPass(CryptoUtils.encryptCloudConfigPassword("my-password"));
112 identity.setProjectDomainName("test-domain");
113 identity.setUserDomainName("user-domain");
114 ObjectMapper mapper = new ObjectMapper();
115 com.woorea.openstack.keystone.v3.model.Authentication expected =
116 mapper.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/__files/KeystoneV3Payload.json"))), com.woorea.openstack.keystone.v3.model.Authentication.class);
117 com.woorea.openstack.keystone.v3.model.Authentication actual = authenticationMethodFactory.getAuthenticationForV3(identity, "project-x");
119 assertThat(actual, sameBeanAs(expected));