replace all fixed wiremock ports
[so.git] / adapters / mso-adapter-utils / src / test / java / org / onap / so / openstack / utils / MsoKeystoneUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 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.openstack.utils;
22
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.onap.so.StubOpenStack;
27 import org.onap.so.BaseTest;
28 import org.onap.so.openstack.beans.MsoTenant;
29 import org.onap.so.openstack.exceptions.MsoException;
30 import org.springframework.beans.factory.annotation.Autowired;
31
32 import java.io.IOException;
33 import java.util.HashMap;
34
35 public class MsoKeystoneUtilsTest extends BaseTest {
36
37     @Autowired
38     private MsoKeystoneUtils msoKeystoneUtils;
39
40     @Before
41     public void before() throws IOException {
42         StubOpenStack.mockOpenStackResponseAccess(wireMockServer, wireMockPort);
43     }
44
45     @Test
46     public void createTenantTest() throws Exception {
47         StubOpenStack.mockOpenStackPostTenantWithBodyFile_200(wireMockServer);
48
49         StubOpenStack.mockOpenStackGetUserById(wireMockServer, "john");
50         StubOpenStack.mockOpenStackGetRoles_200(wireMockServer, "OS-KSADM");
51         String response = msoKeystoneUtils.createTenant("tenant", "MTN13", new HashMap<>(), true);
52
53         Assert.assertEquals("tenantId", response);
54     }
55
56     @Test
57     public void createTenantTest_FindUserByName() throws Exception {
58         StubOpenStack.mockOpenStackPostTenantWithBodyFile_200(wireMockServer);
59
60         StubOpenStack.mockOpenStackGetUserByName(wireMockServer, "john");
61         StubOpenStack.mockOpenStackGetRoles_200(wireMockServer, "OS-KSADM");
62         String response = msoKeystoneUtils.createTenant("tenant", "MTN13", new HashMap<>(), true);
63         Assert.assertEquals("tenantId", response);
64
65     }
66
67     @Test
68     public void createTenantTest_Exception() throws Exception {
69         expectedException.expect(MsoException.class);
70         StubOpenStack.mockOpenStackPostTenantWithBodyFile_200(wireMockServer);
71         StubOpenStack.mockOpenStackGetUserByName_500(wireMockServer, "john");
72         StubOpenStack.mockOpenStackGetRoles_200(wireMockServer, "OS-KSADM");
73         msoKeystoneUtils.createTenant("tenant", "Test", new HashMap<>(), true);
74     }
75
76     @Test
77     public void queryTenantTest() throws Exception {
78         StubOpenStack.mockOpenStackGetTenantById(wireMockServer, "tenantId");
79
80         MsoTenant msoTenant = msoKeystoneUtils.queryTenant("tenantId", "MTN13");
81
82         Assert.assertEquals("testingTenantName", msoTenant.getTenantName());
83     }
84
85     @Test
86     public void queryTenantByNameTest() throws Exception {
87         StubOpenStack.mockOpenStackGetTenantByName(wireMockServer, "tenant");
88
89         MsoTenant msoTenant = msoKeystoneUtils.queryTenantByName("tenant", "MTN13");
90
91         Assert.assertEquals("testingTenantName", msoTenant.getTenantName());
92     }
93
94     @Test
95     public void deleteTenantTest() throws Exception {
96         StubOpenStack.mockOpenStackGetTenantById(wireMockServer, "tenantId");
97         StubOpenStack.mockOpenStackDeleteTenantById_200(wireMockServer, "tenantId");
98         boolean result = msoKeystoneUtils.deleteTenant("tenantId", "MTN13");
99
100         Assert.assertTrue(result);
101     }
102
103     @Test
104     public void deleteTenantByNameTest() throws Exception {
105         StubOpenStack.mockOpenStackGetTenantByName(wireMockServer, "tenant");
106         StubOpenStack.mockOpenStackDeleteTenantById_200(wireMockServer, "tenantId");
107         boolean result = msoKeystoneUtils.deleteTenantByName("tenant", "MTN13");
108
109         Assert.assertTrue(result);
110     }
111 }