Split out datastore implementation from aaa-shiro
[aaa.git] / aaa-idm-store-h2 / src / test / java / org / opendaylight / aaa / datastore / h2 / DomainStoreTest.java
1 /*
2  * Copyright (c) 2014, 2017 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 package org.opendaylight.aaa.datastore.h2;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.anyString;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.verify;
16
17 import java.sql.Connection;
18 import java.sql.DatabaseMetaData;
19 import java.sql.ResultSet;
20 import java.sql.SQLException;
21 import java.sql.Statement;
22 import org.junit.Test;
23 import org.mockito.Mockito;
24 import org.opendaylight.aaa.api.model.Domain;
25 import org.opendaylight.aaa.api.model.Domains;
26
27 public class DomainStoreTest {
28
29     private final Connection connectionMock = mock(Connection.class);
30
31     private final ConnectionProvider connectionFactoryMock = () -> connectionMock;
32
33     private final DomainStore domainStoreUnderTest = new DomainStore(connectionFactoryMock);
34
35     @Test
36     public void getDomainsTest() throws SQLException, Exception {
37         // Setup Mock Behavior
38         String[] tableTypes = { "TABLE" };
39         Mockito.when(connectionMock.isClosed()).thenReturn(false);
40         DatabaseMetaData dbmMock = mock(DatabaseMetaData.class);
41         Mockito.when(connectionMock.getMetaData()).thenReturn(dbmMock);
42         ResultSet rsUserMock = mock(ResultSet.class);
43         Mockito.when(dbmMock.getTables(null, null, "DOMAINS", tableTypes)).thenReturn(rsUserMock);
44         Mockito.when(rsUserMock.next()).thenReturn(true);
45
46         Statement stmtMock = mock(Statement.class);
47         Mockito.when(connectionMock.createStatement()).thenReturn(stmtMock);
48
49         ResultSet rsMock = getMockedResultSet();
50         Mockito.when(stmtMock.executeQuery(anyString())).thenReturn(rsMock);
51
52         // Run Test
53         Domains domains = domainStoreUnderTest.getDomains();
54
55         // Verify
56         assertTrue(domains.getDomains().size() == 1);
57         verify(stmtMock).close();
58     }
59
60     @Test
61     public void deleteDomainsTest() throws SQLException, Exception {
62         String domainId = "Testing12345";
63
64         // Run Test
65         Domain testDomain = new Domain();
66         testDomain.setDomainid(domainId);
67         testDomain.setName(domainId);
68         testDomain.setEnabled(Boolean.TRUE);
69
70         DomainStore ds = new DomainStore(
71                 new IdmLightSimpleConnectionProvider(new IdmLightConfigBuilder().dbUser("foo").dbPwd("bar").build()));
72
73         ds.createDomain(testDomain);
74         assertEquals(ds.getDomain(domainId).getDomainid(), domainId);
75         ds.deleteDomain(domainId);
76         assertNull(ds.getDomain(domainId));
77     }
78
79     public ResultSet getMockedResultSet() throws SQLException {
80         ResultSet rsMock = mock(ResultSet.class);
81         Mockito.when(rsMock.next()).thenReturn(true).thenReturn(false);
82         Mockito.when(rsMock.getInt(DomainStore.SQL_ID)).thenReturn(1);
83         Mockito.when(rsMock.getString(DomainStore.SQL_NAME)).thenReturn("DomainName_1");
84         Mockito.when(rsMock.getString(DomainStore.SQL_DESCR)).thenReturn("Desc_1");
85         Mockito.when(rsMock.getInt(DomainStore.SQL_ENABLED)).thenReturn(1);
86         return rsMock;
87     }
88 }