Merge "Add unit tests for ExternalAPIUtil"
[so.git] / adapters / mso-adapter-utils / src / test / java / org / onap / so / cloud / authentication / AuthenticationMethodTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.cloud.authentication;
22
23 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
24 import static org.junit.Assert.assertThat;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30
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;
36
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;
42
43 /**
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
46  * only are tested.
47  *
48  */
49 public class AuthenticationMethodTest {
50
51         private AuthenticationMethodFactory authenticationMethodFactory = new AuthenticationMethodFactory();
52         /**
53          * 
54          */
55         public AuthenticationMethodTest() {
56                 // TODO Auto-generated constructor stub
57         }
58         
59         @Test
60         public void testCustomRackspaceAuth() {
61                 CloudIdentity ci = new CloudIdentity();
62                 ci.setIdentityAuthenticationType(AuthenticationType.RACKSPACE_APIKEY);
63                 ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
64                 ci.setMsoId("test");
65                 
66                 Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
67                 assertTrue(RackspaceAuthentication.class.equals(auth.getClass()));
68         
69         }
70         
71         @Test
72         public void testCoreUsernamePasswordAuth() {
73                 CloudIdentity ci = new CloudIdentity();
74                 ci.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD);
75                 ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
76                 ci.setMsoId("someuser");
77                 
78                 Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
79                 assertTrue(UsernamePassword.class.equals(auth.getClass()));
80                 
81         }
82         
83         @Test
84         public void testCustomRackspaceAuthFromCloudIdentity() {
85                 CloudIdentity ci = new CloudIdentity();
86                 ci.setIdentityAuthenticationType(AuthenticationType.RACKSPACE_APIKEY);
87                 ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
88                 ci.setMsoId("test");
89                 
90                 Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
91                 assertTrue(RackspaceAuthentication.class.equals(auth.getClass()));
92         }
93         
94         @Test
95         public void testCoreUsernamePasswordAuthFromCloudIdentity() {
96                 CloudIdentity ci = new CloudIdentity();
97                 ci.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD);
98                 ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
99                 ci.setMsoId("someuser");
100
101                 Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
102                 assertTrue(UsernamePassword.class.equals(auth.getClass()));
103         
104         }
105         
106         @Test
107         public void getAuthenticationForV3Test() throws JsonParseException, JsonMappingException, IOException {
108                 
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");
118                 
119                 assertThat(actual, sameBeanAs(expected));
120         }
121 }