blob: eb2c39c3c29bf1822f724d2a6054d9f994e6e819 [file] [log] [blame]
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.druid.metadata;
import io.druid.java.util.common.StringUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.tweak.HandleCallback;
import java.util.LinkedList;
public class SQLMetadataConnectorTest
{
@Rule
public final TestDerbyConnector.DerbyConnectorRule derbyConnectorRule = new TestDerbyConnector.DerbyConnectorRule();
private TestDerbyConnector connector;
private MetadataStorageTablesConfig tablesConfig;
@Before
public void setUp() throws Exception
{
connector = derbyConnectorRule.getConnector();
tablesConfig = derbyConnectorRule.metadataTablesConfigSupplier().get();
}
@Test
public void testCreateTables() throws Exception
{
final LinkedList<String> tables = new LinkedList<String>();
final String entryType = tablesConfig.getTaskEntryType();
tables.add(tablesConfig.getConfigTable());
tables.add(tablesConfig.getSegmentsTable());
tables.add(tablesConfig.getRulesTable());
tables.add(tablesConfig.getLockTable(entryType));
tables.add(tablesConfig.getLogTable(entryType));
tables.add(tablesConfig.getEntryTable(entryType));
tables.add(tablesConfig.getAuditTable());
tables.add(tablesConfig.getSupervisorTable());
connector.createSegmentTable();
connector.createConfigTable();
connector.createRulesTable();
connector.createTaskTables();
connector.createAuditTable();
connector.createSupervisorsTable();
connector.getDBI().withHandle(
new HandleCallback<Void>()
{
@Override
public Void withHandle(Handle handle) throws Exception
{
for (String table : tables) {
Assert.assertTrue(
StringUtils.format("table %s was not created!", table),
connector.tableExists(handle, table)
);
}
return null;
}
}
);
for (String table : tables) {
dropTable(table);
}
}
@Test
public void testInsertOrUpdate() throws Exception
{
final String tableName = "test";
connector.createConfigTable(tableName);
Assert.assertNull(connector.lookup(tableName, "name", "payload", "emperor"));
connector.insertOrUpdate(
tableName,
"name",
"payload",
"emperor",
StringUtils.toUtf8("penguin")
);
Assert.assertArrayEquals(
StringUtils.toUtf8("penguin"),
connector.lookup(tableName, "name", "payload", "emperor")
);
connector.insertOrUpdate(
tableName,
"name",
"payload",
"emperor",
StringUtils.toUtf8("penguin chick")
);
Assert.assertArrayEquals(
StringUtils.toUtf8("penguin chick"),
connector.lookup(tableName, "name", "payload", "emperor")
);
dropTable(tableName);
}
private void dropTable(final String tableName)
{
connector.getDBI().withHandle(
new HandleCallback<Void>()
{
@Override
public Void withHandle(Handle handle) throws Exception
{
handle.createStatement(StringUtils.format("DROP TABLE %s", tableName))
.execute();
return null;
}
}
);
}
}