2 * Copyright (c) 2014, 2017 Hewlett-Packard Development Company, L.P. and others. All rights reserved.
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
8 package org.opendaylight.aaa.datastore.h2;
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;
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;
27 public class DomainStoreTest {
29 private final Connection connectionMock = mock(Connection.class);
31 private final ConnectionProvider connectionFactoryMock = () -> connectionMock;
33 private final DomainStore domainStoreUnderTest = new DomainStore(connectionFactoryMock);
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);
46 Statement stmtMock = mock(Statement.class);
47 Mockito.when(connectionMock.createStatement()).thenReturn(stmtMock);
49 ResultSet rsMock = getMockedResultSet();
50 Mockito.when(stmtMock.executeQuery(anyString())).thenReturn(rsMock);
53 Domains domains = domainStoreUnderTest.getDomains();
56 assertTrue(domains.getDomains().size() == 1);
57 verify(stmtMock).close();
61 public void deleteDomainsTest() throws SQLException, Exception {
62 String domainId = "Testing12345";
65 Domain testDomain = new Domain();
66 testDomain.setDomainid(domainId);
67 testDomain.setName(domainId);
68 testDomain.setEnabled(Boolean.TRUE);
70 DomainStore ds = new DomainStore(
71 new IdmLightSimpleConnectionProvider(new IdmLightConfigBuilder().dbUser("foo").dbPwd("bar").build()));
73 ds.createDomain(testDomain);
74 assertEquals(ds.getDomain(domainId).getDomainid(), domainId);
75 ds.deleteDomain(domainId);
76 assertNull(ds.getDomain(domainId));
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);