41aac0084c8a04b36111449dc30fe2aa6e5da317
[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 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29 import org.junit.Test;
30 import org.onap.so.cloud.authentication.models.RackspaceAuthentication;
31 import org.onap.so.db.catalog.beans.AuthenticationType;
32 import org.onap.so.db.catalog.beans.CloudIdentity;
33 import org.onap.so.utils.CryptoUtils;
34 import com.fasterxml.jackson.core.JsonParseException;
35 import com.fasterxml.jackson.databind.JsonMappingException;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.woorea.openstack.keystone.model.Authentication;
38 import com.woorea.openstack.keystone.model.authentication.UsernamePassword;
39
40 /**
41  * A few JUnit tests to evaluate the new factory that manages authentication types and their associated wrapper classes.
42  * Here it is assumed that core types only are tested.
43  *
44  */
45 public class AuthenticationMethodTest {
46
47     private AuthenticationMethodFactory authenticationMethodFactory = new AuthenticationMethodFactory();
48
49     /**
50      * 
51      */
52     public AuthenticationMethodTest() {
53         // TODO Auto-generated constructor stub
54     }
55
56     @Test
57     public void testCustomRackspaceAuth() {
58         CloudIdentity ci = new CloudIdentity();
59         ci.setIdentityAuthenticationType(AuthenticationType.RACKSPACE_APIKEY);
60         ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
61         ci.setMsoId("test");
62
63         Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
64         assertTrue(RackspaceAuthentication.class.equals(auth.getClass()));
65
66     }
67
68     @Test
69     public void testCoreUsernamePasswordAuth() {
70         CloudIdentity ci = new CloudIdentity();
71         ci.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD);
72         ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
73         ci.setMsoId("someuser");
74
75         Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
76         assertTrue(UsernamePassword.class.equals(auth.getClass()));
77
78     }
79
80     @Test
81     public void testCustomRackspaceAuthFromCloudIdentity() {
82         CloudIdentity ci = new CloudIdentity();
83         ci.setIdentityAuthenticationType(AuthenticationType.RACKSPACE_APIKEY);
84         ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
85         ci.setMsoId("test");
86
87         Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
88         assertTrue(RackspaceAuthentication.class.equals(auth.getClass()));
89     }
90
91     @Test
92     public void testCoreUsernamePasswordAuthFromCloudIdentity() {
93         CloudIdentity ci = new CloudIdentity();
94         ci.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD);
95         ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
96         ci.setMsoId("someuser");
97
98         Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
99         assertTrue(UsernamePassword.class.equals(auth.getClass()));
100
101     }
102
103     @Test
104     public void getAuthenticationForV3Test() throws JsonParseException, JsonMappingException, IOException {
105
106         CloudIdentity identity = new CloudIdentity();
107         identity.setMsoId("my-username");
108         identity.setMsoPass(CryptoUtils.encryptCloudConfigPassword("my-password"));
109         identity.setProjectDomainName("test-domain");
110         identity.setUserDomainName("user-domain");
111         ObjectMapper mapper = new ObjectMapper();
112         com.woorea.openstack.keystone.v3.model.Authentication expected = mapper.readValue(
113                 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/KeystoneV3Payload.json"))),
114                 com.woorea.openstack.keystone.v3.model.Authentication.class);
115         com.woorea.openstack.keystone.v3.model.Authentication actual =
116                 authenticationMethodFactory.getAuthenticationForV3(identity, "project-x");
117
118         assertThat(actual, sameBeanAs(expected));
119     }
120 }