Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #709 from zalando/ARUHA-882
Browse files Browse the repository at this point in the history
ARUHA-882 Fix Indeterministic tests
  • Loading branch information
antban authored Aug 1, 2017
2 parents 8697022 + 70b3fd8 commit 50ef88a
Show file tree
Hide file tree
Showing 73 changed files with 1,033 additions and 1,132 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ task fullAcceptanceTest(type: GradleBuild) {
task acceptanceTest(type: Test) {
testClassesDir = sourceSets.acceptanceTest.output.classesDir
classpath = sourceSets.acceptanceTest.runtimeClasspath
maxParallelForks = Runtime.runtime.availableProcessors()
}

test {
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ services:
KAFKA_ZOOKEEPER_CONNECT: localhost:2181
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'false'
KAFKA_DELETE_TOPIC_ENABLE: 'true'
KAFKA_CREATE_TOPICS: "test-topic:8:1"
KAFKA_NUM_PARTITIONS: 8
KAFKA_BROKER_ID: 0
volumes:
- /var/run/docker.sock:/var/run/docker.sock
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.zalando.nakadi.repository.db;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.After;
import org.junit.Before;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.zalando.nakadi.config.JsonConfig;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.stream.Stream;

import static org.zalando.nakadi.webservice.BaseAT.POSTGRES_PWD;
import static org.zalando.nakadi.webservice.BaseAT.POSTGRES_URL;
Expand All @@ -20,35 +17,20 @@ public abstract class AbstractDbRepositoryTest {

protected JdbcTemplate template;
protected Connection connection;
protected ObjectMapper mapper;
protected String[] repositoryTables;

public AbstractDbRepositoryTest(final String... repositoryTables) {
this.repositoryTables = repositoryTables;
}

@Before
public void setUp() throws Exception {
try {
mapper = (new JsonConfig()).jacksonObjectMapper();
final DataSource datasource = new DriverManagerDataSource(POSTGRES_URL, POSTGRES_USER, POSTGRES_PWD);
template = new JdbcTemplate(datasource);
connection = datasource.getConnection();
clearRepositoryTables();
} catch (final SQLException e) {
e.printStackTrace();
}
}

@After
public void tearDown() throws SQLException {
clearRepositoryTables();
connection.close();
}

private void clearRepositoryTables() {
Stream.of(repositoryTables)
.map(table -> "DELETE FROM " + table)
.forEach(template::execute);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.zalando.nakadi.repository.db;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import org.junit.Before;
import org.junit.Test;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.zalando.nakadi.config.JsonConfig;
import org.zalando.nakadi.domain.EventCategory;
import org.zalando.nakadi.domain.EventType;
import org.zalando.nakadi.domain.EventTypeSchema;
Expand All @@ -13,30 +13,27 @@
import org.zalando.nakadi.exceptions.NakadiException;
import org.zalando.nakadi.exceptions.NoSuchEventTypeException;
import org.zalando.nakadi.repository.EventTypeRepository;
import org.zalando.nakadi.utils.TestUtils;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.zalando.nakadi.utils.TestUtils.buildDefaultEventType;
import static org.zalando.nakadi.utils.TestUtils.randomUUID;
import static org.zalando.nakadi.utils.TestUtils.randomValidEventTypeName;

public class EventTypeDbRepositoryTest extends AbstractDbRepositoryTest {

private EventTypeRepository repository;

public EventTypeDbRepositoryTest() {
super("zn_data.event_type_schema", "zn_data.event_type");
}

@Before
public void setUp() throws Exception {
super.setUp();
repository = new EventTypeDbRepository(template, mapper);
repository = new EventTypeDbRepository(template, TestUtils.OBJECT_MAPPER);
}

@Test
Expand All @@ -45,18 +42,14 @@ public void whenCreateNewEventTypePersistItInTheDatabase() throws Exception {

repository.saveEventType(eventType);

final int rows = template.queryForObject("SELECT count(*) FROM zn_data.event_type", Integer.class);
assertThat("Number of rows should increase", rows, equalTo(1));

final SqlRowSet rs =
template.queryForRowSet("SELECT et_name, et_topic, et_event_type_object FROM zn_data.event_type");
template.queryForRowSet("SELECT et_topic, et_event_type_object FROM zn_data.event_type WHERE et_name=?",
eventType.getName());
rs.next();

assertThat("Name is persisted", rs.getString(1), equalTo(eventType.getName()));
assertThat("Topic is persisted", rs.getString(2), equalTo(eventType.getTopic()));
assertThat("Topic is persisted", rs.getString(1), equalTo(eventType.getTopic()));

final ObjectMapper mapper = (new JsonConfig()).jacksonObjectMapper();
final EventType persisted = mapper.readValue(rs.getString(3), EventType.class);
final EventType persisted = TestUtils.OBJECT_MAPPER.readValue(rs.getString(2), EventType.class);

assertThat(persisted.getCategory(), equalTo(eventType.getCategory()));
assertThat(persisted.getName(), equalTo(eventType.getName()));
Expand All @@ -70,17 +63,17 @@ public void whenCreateNewEventTypeAlsoInsertIntoSchemaTable() throws Exception {

repository.saveEventType(eventType);

final int rows = template.queryForObject("SELECT count(*) FROM zn_data.event_type_schema", Integer.class);
final int rows = template.queryForObject(
"SELECT count(*) FROM zn_data.event_type_schema where ets_event_type_name=?",
Integer.class, eventType.getName());
assertThat("Number of rows should increase", rows, equalTo(1));

final SqlRowSet rs =
template.queryForRowSet("SELECT ets_event_type_name, ets_schema_object FROM zn_data.event_type_schema");
final SqlRowSet rs = template.queryForRowSet(
"SELECT ets_schema_object FROM zn_data.event_type_schema where ets_event_type_name=?",
eventType.getName());
rs.next();

assertThat("Name is persisted", rs.getString(1), equalTo(eventType.getName()));

final ObjectMapper mapper = (new JsonConfig()).jacksonObjectMapper();
final EventTypeSchema persisted = mapper.readValue(rs.getString(2), EventTypeSchema.class);
final EventTypeSchema persisted = TestUtils.OBJECT_MAPPER.readValue(rs.getString(1), EventTypeSchema.class);

assertThat(persisted.getVersion(), equalTo(eventType.getSchema().getVersion()));
assertThat(persisted.getCreatedAt(), notNullValue());
Expand Down Expand Up @@ -135,16 +128,16 @@ public void whenUpdateExistingEventTypeItUpdates() throws NakadiException, IOExc

repository.update(eventType);

final int rows = template.queryForObject("SELECT count(*) FROM zn_data.event_type_schema", Integer.class);
final int rows = template.queryForObject(
"SELECT count(*) FROM zn_data.event_type_schema WHERE ets_event_type_name=?",
Integer.class, eventType.getName());
assertThat("Number of rows should increase", rows, equalTo(1));

final SqlRowSet rs = template.queryForRowSet("SELECT et_name, et_event_type_object FROM zn_data.event_type");
final SqlRowSet rs = template.queryForRowSet(
"SELECT et_event_type_object FROM zn_data.event_type WHERE et_name=?", eventType.getName());
rs.next();

assertThat("Name is persisted", rs.getString(1), equalTo(eventType.getName()));

final ObjectMapper mapper = (new JsonConfig()).jacksonObjectMapper();
final EventType persisted = mapper.readValue(rs.getString(2), EventType.class);
final EventType persisted = TestUtils.OBJECT_MAPPER.readValue(rs.getString(1), EventType.class);

assertThat(persisted.getCategory(), equalTo(eventType.getCategory()));
assertThat(persisted.getOwningApplication(), equalTo(eventType.getOwningApplication()));
Expand All @@ -164,7 +157,9 @@ public void whenUpdateDifferentSchemaVersionThenInsertIt() throws NakadiExceptio

repository.update(eventType);

final int rows = template.queryForObject("SELECT count(*) FROM zn_data.event_type_schema", Integer.class);
final int rows = template.queryForObject(
"SELECT count(*) FROM zn_data.event_type_schema where ets_event_type_name=?",
Integer.class, eventType.getName());
assertThat("Number of rows should increase", rows, equalTo(2));
}

Expand All @@ -176,35 +171,46 @@ public void whenListExistingEventTypesAreListed() throws NakadiException {
repository.saveEventType(eventType1);
repository.saveEventType(eventType2);

final List<EventType> eventTypes = repository.list();
final List<EventType> eventTypes = repository.list().stream()
.filter(et -> et.getName() != null)
.filter(et -> et.getName().equals(eventType1.getName()) || et.getName().equals(eventType2.getName()))
.collect(Collectors.toList());

assertThat(eventTypes, hasSize(2));
}

@Test
public void whenRemoveThenDeleteFromDatabase() throws Exception {
final EventType eventType = buildDefaultEventType();

insertEventType(eventType);

repository.removeEventType(eventType.getName());
int rows = template.queryForObject("SELECT count(*) FROM zn_data.event_type where et_name=?", Integer.class,
eventType.getName());
assertThat("After inserting event type it is present in db", rows, equalTo(1));

final int rows = template.queryForObject("SELECT count(*) FROM zn_data.event_type", Integer.class);
assertThat("Number of rows should encrease", rows, equalTo(0));
repository.removeEventType(eventType.getName());

final int schemaRows = template.queryForObject("SELECT count(*) FROM zn_data.event_type_schema", Integer.class);
assertThat("Number of rows should decrease", schemaRows, equalTo(0));
rows = template.queryForObject("SELECT count(*) FROM zn_data.event_type where et_name=?", Integer.class,
eventType.getName());
assertThat("After deleting event type it is not present in db", rows, equalTo(0));
}

@Test
public void unknownAttributesAreIgnoredWhenDesserializing() throws Exception {
final String eventTypeName = randomValidEventTypeName();
final EventType eventType = buildDefaultEventType();
final ObjectNode node = (ObjectNode) TestUtils.OBJECT_MAPPER.readTree(
TestUtils.OBJECT_MAPPER.writeValueAsString(eventType));
node.set("unknown_attribute", new TextNode("will just be ignored"));

final String eventTypeName = eventType.getName();
final String topic = randomUUID();
final String insertSQL = "INSERT INTO zn_data.event_type (et_name, et_topic, et_event_type_object) " +
"VALUES (?, ?, to_json(?::json))";
template.update(insertSQL,
eventTypeName,
topic,
"{\"unknow_attribute\": \"will just be ignored\"}");
TestUtils.OBJECT_MAPPER.writeValueAsString(node));

final EventType persistedEventType = repository.findByName(eventTypeName);

Expand All @@ -217,6 +223,6 @@ private void insertEventType(final EventType eventType) throws Exception {
template.update(insertSQL,
eventType.getName(),
eventType.getTopic(),
mapper.writer().writeValueAsString(eventType));
TestUtils.OBJECT_MAPPER.writer().writeValueAsString(eventType));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.zalando.nakadi.repository.db;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.List;
import org.joda.time.DateTime;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -10,49 +11,48 @@
import org.zalando.nakadi.domain.EventTypeSchemaBase;
import org.zalando.nakadi.domain.Version;
import org.zalando.nakadi.utils.EventTypeTestBuilder;

import java.util.List;
import org.zalando.nakadi.utils.TestUtils;
import static org.zalando.nakadi.utils.TestUtils.randomUUID;

public class SchemaRepositoryTest extends AbstractDbRepositoryTest {

private SchemaRepository repository;

public SchemaRepositoryTest() {
super(new String[]{"zn_data.event_type_schema"});
}

@Before
public void setUp() throws Exception {
super.setUp();
repository = new SchemaRepository(template, mapper);
repository = new SchemaRepository(template, TestUtils.OBJECT_MAPPER);
}

@Test
public void whenListVersionsListedOrdered() throws Exception {
buildEventWithMultipleSchemas("test_et_name_schemarepositorytest");
final String name = randomUUID();
buildEventWithMultipleSchemas(name);

final List<EventTypeSchema> schemas = repository.getSchemas("test_et_name_schemarepositorytest", 0, 3);
final List<EventTypeSchema> schemas = repository.getSchemas(name, 0, 3);
Assert.assertEquals(3, schemas.size());
Assert.assertEquals(new Version("10.0.0"), schemas.get(0).getVersion());
Assert.assertEquals(new Version("2.10.3"), schemas.get(1).getVersion());
Assert.assertEquals(new Version("1.0.2"), schemas.get(2).getVersion());

final int count = repository.getSchemasCount("test_et_name_schemarepositorytest");
final int count = repository.getSchemasCount(name);
Assert.assertEquals(3, count);
}

@Test
public void whenGetLatestSchemaReturnLatest() throws Exception {
buildEventWithMultipleSchemas("test_latest_schema_event");
final EventTypeSchema schema = repository.getSchemaVersion("test_latest_schema_event", "10.0.0");
final String name = randomUUID();
buildEventWithMultipleSchemas(name);
final EventTypeSchema schema = repository.getSchemaVersion(name, "10.0.0");
Assert.assertEquals("10.0.0", schema.getVersion().toString());
Assert.assertEquals("schema", schema.getSchema());
}

@Test
public void whenGetOldSchemaReturnOld() throws Exception {
buildEventWithMultipleSchemas("test_old_schema_event");
final EventTypeSchema schema = repository.getSchemaVersion("test_old_schema_event", "1.0.2");
final String name = randomUUID();
buildEventWithMultipleSchemas(name);
final EventTypeSchema schema = repository.getSchemaVersion(name, "1.0.2");
Assert.assertEquals("1.0.2", schema.getVersion().toString());
Assert.assertEquals("schema", schema.getSchema());
}
Expand All @@ -75,7 +75,7 @@ private void insertSchema(final EventType eventType) throws JsonProcessingExcept
template.update(
"INSERT INTO zn_data.event_type_schema (ets_event_type_name, ets_schema_object) VALUES (?, ?::jsonb)",
eventType.getName(),
mapper.writer().writeValueAsString(eventType.getSchema()));
TestUtils.OBJECT_MAPPER.writer().writeValueAsString(eventType.getSchema()));
}

private void insertEventType(final EventType eventType) throws Exception {
Expand All @@ -84,7 +84,7 @@ private void insertEventType(final EventType eventType) throws Exception {
template.update(insertSQL,
eventType.getName(),
eventType.getTopic(),
mapper.writer().writeValueAsString(eventType));
TestUtils.OBJECT_MAPPER.writer().writeValueAsString(eventType));
}

}
Loading

0 comments on commit 50ef88a

Please sign in to comment.