sonar issue, AuthenticationMethodTest use assertNotEquals instead
[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  * Copyright (C) 2022 Samsung Electronics.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.so.cloud.authentication;
23
24 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.assertEquals;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import org.junit.Test;
31 import org.onap.so.cloud.authentication.models.RackspaceAuthentication;
32 import org.onap.so.db.catalog.beans.AuthenticationType;
33 import org.onap.so.db.catalog.beans.CloudIdentity;
34 import org.onap.so.utils.CryptoUtils;
35 import com.fasterxml.jackson.core.JsonParseException;
36 import com.fasterxml.jackson.databind.JsonMappingException;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import com.woorea.openstack.keystone.model.Authentication;
39 import com.woorea.openstack.keystone.model.authentication.UsernamePassword;
40
41 /**
42  * A few JUnit tests to evaluate the new factory that manages authentication types and their associated wrapper classes.
43  * Here it is assumed that core types only are tested.
44  *
45  */
46 public class AuthenticationMethodTest {
47
48     private AuthenticationMethodFactory authenticationMethodFactory = new AuthenticationMethodFactory();
49
50     /**
51      * 
52      */
53     public AuthenticationMethodTest() {
54         // TODO Auto-generated constructor stub
55     }
56
57     @Test
58     public void testCustomRackspaceAuth() {
59         CloudIdentity ci = new CloudIdentity();
60         ci.setIdentityAuthenticationType(AuthenticationType.RACKSPACE_APIKEY);
61         ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
62         ci.setMsoId("test");
63
64         Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
65         assertEquals(RackspaceAuthentication.class, auth.getClass());
66
67     }
68
69     @Test
70     public void testCoreUsernamePasswordAuth() {
71         CloudIdentity ci = new CloudIdentity();
72         ci.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD);
73         ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
74         ci.setMsoId("someuser");
75
76         Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
77         assertEquals(UsernamePassword.class, auth.getClass());
78
79     }
80
81     @Test
82     public void testCustomRackspaceAuthFromCloudIdentity() {
83         CloudIdentity ci = new CloudIdentity();
84         ci.setIdentityAuthenticationType(AuthenticationType.RACKSPACE_APIKEY);
85         ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
86         ci.setMsoId("test");
87
88         Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
89         assertEquals(RackspaceAuthentication.class, auth.getClass());
90     }
91
92     @Test
93     public void testCoreUsernamePasswordAuthFromCloudIdentity() {
94         CloudIdentity ci = new CloudIdentity();
95         ci.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD);
96         ci.setMsoPass("FD205490A48D48475607C36B9AD902BF");
97         ci.setMsoId("someuser");
98
99         Authentication auth = authenticationMethodFactory.getAuthenticationFor(ci);
100         assertEquals(UsernamePassword.class, auth.getClass());
101
102     }
103
104     @Test
105     public void getAuthenticationForV3Test() throws JsonParseException, JsonMappingException, IOException {
106
107         CloudIdentity identity = new CloudIdentity();
108         identity.setMsoId("my-username");
109         identity.setMsoPass(CryptoUtils.encryptCloudConfigPassword("my-password"));
110         identity.setProjectDomainName("test-domain");
111         identity.setUserDomainName("user-domain");
112         ObjectMapper mapper = new ObjectMapper();
113         com.woorea.openstack.keystone.v3.model.Authentication expected = mapper.readValue(
114                 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/KeystoneV3Payload.json"))),
115                 com.woorea.openstack.keystone.v3.model.Authentication.class);
116         com.woorea.openstack.keystone.v3.model.Authentication actual =
117                 authenticationMethodFactory.getAuthenticationForV3(identity, "project-x");
118
119         assertThat(actual, sameBeanAs(expected));
120     }
121 }