Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
Refactor Postgres and MySQL unit tests (#2279)
Browse files Browse the repository at this point in the history
* refactor Postgres and MySQL unit tests

* refactor Postgres and MySQL unit tests
  • Loading branch information
michaelpaliy authored Jun 30, 2021
1 parent b8ca41c commit 15c0c14
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 362 deletions.
2 changes: 1 addition & 1 deletion mysql-persistence/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies {
implementation "org.flywaydb:flyway-core"

testImplementation "org.testcontainers:mysql:${revTestContainer}"
testImplementation "org.testinfected.hamcrest-matchers:all-matchers:${revHamcrestAllMatchers}"

testImplementation project(':conductor-core').sourceSets.test.output
testImplementation project(':conductor-common').sourceSets.test.output
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
package com.netflix.conductor.mysql.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.conductor.dao.ExecutionDAO;
import com.netflix.conductor.dao.MetadataDAO;
import com.netflix.conductor.dao.QueueDAO;
import com.netflix.conductor.mysql.dao.MySQLExecutionDAO;
import com.netflix.conductor.mysql.dao.MySQLMetadataDAO;
import com.netflix.conductor.mysql.dao.MySQLQueueDAO;
Expand All @@ -39,19 +36,19 @@ public class MySQLConfiguration {

@Bean
@DependsOn({"flyway", "flywayInitializer"})
public MetadataDAO mySqlMetadataDAO(ObjectMapper objectMapper, DataSource dataSource, MySQLProperties properties) {
public MySQLMetadataDAO mySqlMetadataDAO(ObjectMapper objectMapper, DataSource dataSource, MySQLProperties properties) {
return new MySQLMetadataDAO(objectMapper, dataSource, properties);
}

@Bean
@DependsOn({"flyway", "flywayInitializer"})
public ExecutionDAO mySqlExecutionDAO(ObjectMapper objectMapper, DataSource dataSource) {
public MySQLExecutionDAO mySqlExecutionDAO(ObjectMapper objectMapper, DataSource dataSource) {
return new MySQLExecutionDAO(objectMapper, dataSource);
}

@Bean
@DependsOn({"flyway", "flywayInitializer"})
public QueueDAO mySqlQueueDAO(ObjectMapper objectMapper, DataSource dataSource) {
public MySQLQueueDAO mySqlQueueDAO(ObjectMapper objectMapper, DataSource dataSource) {
return new MySQLQueueDAO(objectMapper, dataSource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,43 @@
*/
package com.netflix.conductor.mysql.dao;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.conductor.common.config.TestObjectMapperConfiguration;
import com.netflix.conductor.common.metadata.workflow.WorkflowDef;
import com.netflix.conductor.common.run.Workflow;
import com.netflix.conductor.dao.ExecutionDAO;
import com.netflix.conductor.dao.ExecutionDAOTest;
import com.netflix.conductor.mysql.util.MySQLDAOTestUtil;
import org.junit.After;
import com.netflix.conductor.mysql.config.MySQLConfiguration;
import org.flywaydb.core.Flyway;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.utility.DockerImageName;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@ContextConfiguration(classes = {TestObjectMapperConfiguration.class})
@ContextConfiguration(classes = {TestObjectMapperConfiguration.class, MySQLConfiguration.class, FlywayAutoConfiguration.class})
@RunWith(SpringRunner.class)
@SpringBootTest
public class MySQLExecutionDAOTest extends ExecutionDAOTest {

private MySQLDAOTestUtil testUtil;
@Autowired
private MySQLExecutionDAO executionDAO;

@Autowired
private ObjectMapper objectMapper;

@Rule
public TestName name = new TestName();

public MySQLContainer<?> mySQLContainer;
Flyway flyway;

// clean the database between tests.
@Before
public void setup() {
mySQLContainer = new MySQLContainer<>(DockerImageName.parse("mysql")).withDatabaseName(name.getMethodName());
mySQLContainer.start();
testUtil = new MySQLDAOTestUtil(mySQLContainer, objectMapper);
executionDAO = new MySQLExecutionDAO(testUtil.getObjectMapper(), testUtil.getDataSource());
}

@After
public void teardown() {
testUtil.getDataSource().close();
public void before() {
flyway.clean();
flyway.migrate();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,22 @@
*/
package com.netflix.conductor.mysql.dao;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.conductor.common.config.TestObjectMapperConfiguration;
import com.netflix.conductor.common.metadata.events.EventHandler;
import com.netflix.conductor.common.metadata.tasks.TaskDef;
import com.netflix.conductor.common.metadata.workflow.WorkflowDef;
import com.netflix.conductor.core.exception.ApplicationException;
import com.netflix.conductor.mysql.util.MySQLDAOTestUtil;
import com.netflix.conductor.mysql.config.MySQLConfiguration;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.junit.After;
import org.flywaydb.core.Flyway;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.utility.DockerImageName;

import java.util.Arrays;
import java.util.List;
Expand All @@ -42,66 +38,52 @@

import static com.netflix.conductor.core.exception.ApplicationException.Code.CONFLICT;
import static com.netflix.conductor.core.exception.ApplicationException.Code.NOT_FOUND;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

@ContextConfiguration(classes = {TestObjectMapperConfiguration.class})
@ContextConfiguration(classes = {TestObjectMapperConfiguration.class, MySQLConfiguration.class, FlywayAutoConfiguration.class})
@RunWith(SpringRunner.class)
@SpringBootTest
public class MySQLMetadataDAOTest {

private MySQLDAOTestUtil testUtil;
@Autowired
private MySQLMetadataDAO metadataDAO;

@Autowired
private ObjectMapper objectMapper;

@Rule
public TestName name = new TestName();

@Rule
public ExpectedException thrown = ExpectedException.none();

public MySQLContainer<?> mySQLContainer;
Flyway flyway;

// clean the database between tests.
@Before
public void setup() {
mySQLContainer = new MySQLContainer<>(DockerImageName.parse("mysql")).withDatabaseName(name.getMethodName());
mySQLContainer.start();
testUtil = new MySQLDAOTestUtil(mySQLContainer, objectMapper);
metadataDAO = new MySQLMetadataDAO(testUtil.getObjectMapper(), testUtil.getDataSource(),
testUtil.getTestProperties());
}

@After
public void teardown() {
testUtil.getDataSource().close();
public void before() {
flyway.clean();
flyway.migrate();
}

@Test
public void testDuplicateWorkflowDef() {
thrown.expect(ApplicationException.class);
thrown.expectMessage("Workflow with testDuplicate.1 already exists!");
thrown.expect(hasProperty("code", is(CONFLICT)));

WorkflowDef def = new WorkflowDef();
def.setName("testDuplicate");
def.setVersion(1);

metadataDAO.createWorkflowDef(def);
metadataDAO.createWorkflowDef(def);

ApplicationException applicationException = assertThrows(ApplicationException.class,
() -> metadataDAO.createWorkflowDef(def));
assertEquals("Workflow with testDuplicate.1 already exists!", applicationException.getMessage());
assertEquals(CONFLICT, applicationException.getCode());

}

@Test
public void testRemoveNotExistingWorkflowDef() {
thrown.expect(ApplicationException.class);
thrown.expectMessage("No such workflow definition: test version: 1");
thrown.expect(hasProperty("code", is(NOT_FOUND)));

metadataDAO.removeWorkflowDef("test", 1);
ApplicationException applicationException = assertThrows(ApplicationException.class,
() -> metadataDAO.removeWorkflowDef("test", 1));
assertEquals("No such workflow definition: test version: 1", applicationException.getMessage());
assertEquals(NOT_FOUND, applicationException.getCode());
}

@Test
Expand Down Expand Up @@ -190,7 +172,7 @@ public void testWorkflowDefOperations() {
}

@Test
public void testTaskDefOperations() throws Exception {
public void testTaskDefOperations() {
TaskDef def = new TaskDef("taskA");
def.setDescription("description");
def.setCreatedBy("unit_test");
Expand Down Expand Up @@ -244,11 +226,10 @@ public void testTaskDefOperations() throws Exception {

@Test
public void testRemoveNotExistingTaskDef() {
thrown.expect(ApplicationException.class);
thrown.expectMessage("No such task definition");
thrown.expect(hasProperty("code", is(NOT_FOUND)));

metadataDAO.removeTaskDef("test" + UUID.randomUUID().toString());
ApplicationException applicationException = assertThrows(ApplicationException.class,
() -> metadataDAO.removeTaskDef("test" + UUID.randomUUID().toString()));
assertEquals("No such task definition", applicationException.getMessage());
assertEquals(NOT_FOUND, applicationException.getCode());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,22 @@
import com.google.common.collect.ImmutableList;
import com.netflix.conductor.common.config.TestObjectMapperConfiguration;
import com.netflix.conductor.core.events.queue.Message;
import com.netflix.conductor.mysql.util.MySQLDAOTestUtil;
import com.netflix.conductor.mysql.config.MySQLConfiguration;
import com.netflix.conductor.mysql.util.Query;
import org.junit.After;
import org.flywaydb.core.Flyway;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.utility.DockerImageName;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -46,37 +45,31 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

@ContextConfiguration(classes = {TestObjectMapperConfiguration.class})
@ContextConfiguration(classes = {TestObjectMapperConfiguration.class, MySQLConfiguration.class, FlywayAutoConfiguration.class})
@RunWith(SpringRunner.class)
@SpringBootTest
public class MySQLQueueDAOTest {

private static final Logger LOGGER = LoggerFactory.getLogger(MySQLQueueDAOTest.class);

private MySQLDAOTestUtil testUtil;
@Autowired
private MySQLQueueDAO queueDAO;

@Autowired
private ObjectMapper objectMapper;

@Rule
public TestName name = new TestName();

@Rule
public ExpectedException expected = ExpectedException.none();
@Qualifier("dataSource")
@Autowired
private DataSource dataSource;

public MySQLContainer<?> mySQLContainer;
@Autowired
Flyway flyway;

// clean the database between tests.
@Before
public void setup() {
mySQLContainer = new MySQLContainer<>(DockerImageName.parse("mysql")).withDatabaseName(name.getMethodName());
mySQLContainer.start();
testUtil = new MySQLDAOTestUtil(mySQLContainer, objectMapper);
queueDAO = new MySQLQueueDAO(testUtil.getObjectMapper(), testUtil.getDataSource());
}

@After
public void teardown() {
testUtil.getDataSource().close();
public void before() {
flyway.clean();
flyway.migrate();
}

@Test
Expand Down Expand Up @@ -218,9 +211,9 @@ public void pollMessagesTest() {

// Assert that our un-popped messages match our expected size
final long expectedSize = totalSize - firstPollSize - secondPollSize;
try (Connection c = testUtil.getDataSource().getConnection()) {
try (Connection c = dataSource.getConnection()) {
String UNPOPPED = "SELECT COUNT(*) FROM queue_message WHERE queue_name = ? AND popped = false";
try (Query q = new Query(testUtil.getObjectMapper(), c, UNPOPPED)) {
try (Query q = new Query(objectMapper, c, UNPOPPED)) {
long count = q.addParameter(queueName).executeCount();
assertEquals("Remaining queue size mismatch", expectedSize, count);
}
Expand Down Expand Up @@ -300,9 +293,9 @@ public void pollDeferredMessagesTest() throws InterruptedException {

// Assert that our un-popped messages match our expected size
final long expectedSize = totalSize - firstPollSize - secondPollSize;
try (Connection c = testUtil.getDataSource().getConnection()) {
try (Connection c = dataSource.getConnection()) {
String UNPOPPED = "SELECT COUNT(*) FROM queue_message WHERE queue_name = ? AND popped = false";
try (Query q = new Query(testUtil.getObjectMapper(), c, UNPOPPED)) {
try (Query q = new Query(objectMapper, c, UNPOPPED)) {
long count = q.addParameter(queueName).executeCount();
assertEquals("Remaining queue size mismatch", expectedSize, count);
}
Expand Down
Loading

0 comments on commit 15c0c14

Please sign in to comment.