5da64a671fd81d95f686f1c6d66dcf8d9493c5f9
[aaa.git] / aaa-shiro / impl / src / test / java / org / opendaylight / aaa / datastore / h2 / GrantStoreTest.java
1 /*
2  * Copyright (c) 2014, 2016 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.aaa.datastore.h2;
10
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.anyString;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
15
16 import java.sql.Connection;
17 import java.sql.DatabaseMetaData;
18 import java.sql.PreparedStatement;
19 import java.sql.ResultSet;
20 import java.sql.SQLException;
21
22 import org.junit.Test;
23 import org.mockito.Mockito;
24 import org.opendaylight.aaa.api.model.Grants;
25 import org.opendaylight.aaa.impl.datastore.h2.ConnectionProvider;
26 import org.opendaylight.aaa.impl.datastore.h2.GrantStore;
27
28 public class GrantStoreTest {
29
30     private final Connection connectionMock = mock(Connection.class);
31
32     private final ConnectionProvider connectionFactoryMock = () -> connectionMock;
33
34     private final GrantStore grantStoreUnderTest = new GrantStore(connectionFactoryMock);
35
36     private final String did = "5";
37     private final String uid = "5";
38
39     @Test
40     public void getGrantsTest() throws Exception {
41         // Setup Mock Behavior
42         String[] tableTypes = { "TABLE" };
43         Mockito.when(connectionMock.isClosed()).thenReturn(false);
44         DatabaseMetaData dbmMock = mock(DatabaseMetaData.class);
45         Mockito.when(connectionMock.getMetaData()).thenReturn(dbmMock);
46         ResultSet rsUserMock = mock(ResultSet.class);
47         Mockito.when(dbmMock.getTables(null, null, "GRANTS", tableTypes)).thenReturn(rsUserMock);
48         Mockito.when(rsUserMock.next()).thenReturn(true);
49
50         PreparedStatement pstmtMock = mock(PreparedStatement.class);
51         Mockito.when(connectionMock.prepareStatement(anyString())).thenReturn(pstmtMock);
52
53         ResultSet rsMock = getMockedResultSet();
54         Mockito.when(pstmtMock.executeQuery()).thenReturn(rsMock);
55
56         // Run Test
57         Grants grants = grantStoreUnderTest.getGrants(did, uid);
58
59         // Verify
60         assertTrue(grants.getGrants().size() == 1);
61         verify(pstmtMock).close();
62     }
63
64     public ResultSet getMockedResultSet() throws SQLException {
65         ResultSet rsMock = mock(ResultSet.class);
66         Mockito.when(rsMock.next()).thenReturn(true).thenReturn(false);
67         Mockito.when(rsMock.getInt(GrantStore.SQL_ID)).thenReturn(1);
68         Mockito.when(rsMock.getString(GrantStore.SQL_TENANTID)).thenReturn(did);
69         Mockito.when(rsMock.getString(GrantStore.SQL_USERID)).thenReturn(uid);
70         Mockito.when(rsMock.getString(GrantStore.SQL_ROLEID)).thenReturn("Role_1");
71
72         return rsMock;
73     }
74
75 }