refactoring test

This commit is contained in:
ggmaleva 2021-09-24 19:02:17 +02:00
parent b8111a41a1
commit 8e9b9f891d
No known key found for this signature in database
GPG Key ID: 0E412B78565B108F
6 changed files with 76 additions and 71 deletions

View File

@ -27,6 +27,7 @@
<shared.resources.version>0.3.7</shared.resources.version>
<wb.list.proto.version>1.33-554d59c</wb.list.proto.version>
<kafka.common.lib.version>0.1.4</kafka.common.lib.version>
<testcontainers.annotations.version>1.3.0</testcontainers.annotations.version>
</properties>
<dependencies>
@ -125,6 +126,12 @@
<version>1.15.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.rbkmoney</groupId>
<artifactId>testcontainers-annotations</artifactId>
<version>${testcontainers.annotations.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -19,6 +19,8 @@ import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.junit.jupiter.Container;
@ -49,6 +51,11 @@ public abstract class KafkaAbstractTest {
.withEmbeddedZookeeper()
.withStartupTimeout(Duration.ofMinutes(2));
@DynamicPropertySource
static void connectionConfigs(DynamicPropertyRegistry registry) {
registry.add("riak.port", () -> RiakContainerExtension.RIAK.getMappedPort(8087));
}
public static <T> Consumer<String, T> createConsumer() {
Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers());
@ -72,8 +79,7 @@ public abstract class KafkaAbstractTest {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues
.of("kafka.bootstrap.servers=" + kafka.getBootstrapServers(),
"riak.port=" + RiakContainerExtension.RIAK.getMappedPort(8087))
.of("kafka.bootstrap.servers=" + kafka.getBootstrapServers())
.applyTo(configurableApplicationContext.getEnvironment());
initTopic("wb-list-command");
initTopic("wb-list-event-sink");

View File

@ -0,0 +1,36 @@
package com.rbkmoney.wb.list.manager;
import com.rbkmoney.damsel.wb_list.Command;
import com.rbkmoney.damsel.wb_list.IdInfo;
import com.rbkmoney.damsel.wb_list.ListType;
import com.rbkmoney.damsel.wb_list.PaymentId;
import com.rbkmoney.wb.list.manager.utils.ChangeCommandWrapper;
import java.util.UUID;
public abstract class TestObjectFactory {
public static com.rbkmoney.damsel.wb_list.Row testRow() {
com.rbkmoney.damsel.wb_list.Row row = new com.rbkmoney.damsel.wb_list.Row();
row.setId(IdInfo.payment_id(new PaymentId()
.setShopId(randomString())
.setPartyId(randomString())
));
row.setListType(ListType.black);
row.setListName(randomString());
row.setValue(randomString());
return row;
}
public static String randomString() {
return UUID.randomUUID().toString();
}
public static ChangeCommandWrapper testCommand() {
ChangeCommandWrapper changeCommand = new ChangeCommandWrapper();
changeCommand.setCommand(Command.CREATE);
com.rbkmoney.damsel.wb_list.Row row = testRow();
changeCommand.setRow(row);
return changeCommand;
}
}

View File

@ -123,17 +123,17 @@ public class WbListManagerApplicationTest extends KafkaAbstractTest {
row.setListType(ListType.grey);
//check without partyId and shop id
createRow(Instant.now().toString(), null, null);
createRow(Instant.now().toString());
RowInfo rowInfo = iface.getRowInfo(row).getRowInfo();
assertEquals(5, rowInfo.getCountInfo().getCount());
//check without partyId
createRow(Instant.now().toString(), null, SHOP_ID);
createRow(Instant.now().toString());
rowInfo = iface.getRowInfo(row).getRowInfo();
assertEquals(5, rowInfo.getCountInfo().getCount());
//check full key field
createRow(Instant.now().toString(), PARTY_ID, SHOP_ID);
createRow(Instant.now().toString());
rowInfo = iface.getRowInfo(row).getRowInfo();
assertEquals(5, rowInfo.getCountInfo().getCount());
@ -181,17 +181,17 @@ public class WbListManagerApplicationTest extends KafkaAbstractTest {
private RowInfo checkCreateWithCountInfo(WbListServiceSrv.Iface iface, String startTimeCount, String partyId,
String shopId)
throws InterruptedException, java.util.concurrent.ExecutionException, TException {
Row rowWithCountInfo = createRow(startTimeCount, partyId, shopId);
Row rowWithCountInfo = createRow(startTimeCount);
return iface.getRowInfo(rowWithCountInfo).getRowInfo();
}
private Row createRow(String startTimeCount, String partyId, String shopId)
private Row createRow(String startTimeCount)
throws InterruptedException, java.util.concurrent.ExecutionException {
Producer<String, ChangeCommand> producer;
ChangeCommand changeCommand;
ProducerRecord<String, ChangeCommand> producerRecord;
producer = createProducer();
Row rowWithCountInfo = createRowWithCountInfo(startTimeCount, partyId, shopId);
Row rowWithCountInfo = createRowWithCountInfo(startTimeCount);
changeCommand = createCommand(rowWithCountInfo);
producerRecord = new ProducerRecord<>(topic, changeCommand.getRow().getValue(), changeCommand);
producer.send(producerRecord).get();
@ -232,8 +232,7 @@ public class WbListManagerApplicationTest extends KafkaAbstractTest {
return row;
}
private com.rbkmoney.damsel.wb_list.Row createRowWithCountInfo(String startTimeCount, String partyId,
String shopId) {
private com.rbkmoney.damsel.wb_list.Row createRowWithCountInfo(String startTimeCount) {
com.rbkmoney.damsel.wb_list.Row row = new com.rbkmoney.damsel.wb_list.Row();
row.setId(IdInfo.payment_id(new PaymentId()
.setShopId(SHOP_ID)

View File

@ -1,10 +1,10 @@
package com.rbkmoney.wb.list.manager;
import com.rbkmoney.damsel.wb_list.*;
import com.rbkmoney.damsel.wb_list.ChangeCommand;
import com.rbkmoney.damsel.wb_list.Command;
import com.rbkmoney.kafka.common.serialization.ThriftSerializer;
import com.rbkmoney.wb.list.manager.exception.RiakExecutionException;
import com.rbkmoney.wb.list.manager.repository.ListRepository;
import com.rbkmoney.wb.list.manager.utils.ChangeCommandWrapper;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
@ -31,11 +31,6 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
@TestPropertySource(properties = {"retry.timeout=100"})
public class WbListSafetyApplicationTest extends KafkaAbstractTest {
private static final String VALUE = "value";
private static final String SHOP_ID = "shopId";
private static final String PARTY_ID = "partyId";
private static final String LIST_NAME = "listName";
@Value("${kafka.wblist.topic.command}")
public String topic;
@ -62,7 +57,7 @@ public class WbListSafetyApplicationTest extends KafkaAbstractTest {
.when(listRepository).create(any());
Producer<String, ChangeCommand> producerNew = createProducer();
ChangeCommand changeCommand = createCommand();
ChangeCommand changeCommand = TestObjectFactory.testCommand();
changeCommand.setCommand(Command.CREATE);
ProducerRecord<String, ChangeCommand> producerRecordCommand =
new ProducerRecord<>(topic, changeCommand.getRow().getValue(), changeCommand);
@ -74,24 +69,4 @@ public class WbListSafetyApplicationTest extends KafkaAbstractTest {
Mockito.verify(listRepository, times(3)).create(any());
}
private ChangeCommandWrapper createCommand() {
ChangeCommandWrapper changeCommand = new ChangeCommandWrapper();
changeCommand.setCommand(Command.CREATE);
com.rbkmoney.damsel.wb_list.Row row = createRow();
changeCommand.setRow(row);
return changeCommand;
}
private com.rbkmoney.damsel.wb_list.Row createRow() {
com.rbkmoney.damsel.wb_list.Row row = new com.rbkmoney.damsel.wb_list.Row();
row.setId(IdInfo.payment_id(new PaymentId()
.setShopId(SHOP_ID)
.setPartyId(PARTY_ID))
);
row.setListName(LIST_NAME);
row.setListType(ListType.black);
row.setValue(VALUE);
return row;
}
}

View File

@ -1,10 +1,8 @@
package com.rbkmoney.wb.list.manager.handler;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rbkmoney.damsel.wb_list.IdInfo;
import com.rbkmoney.damsel.wb_list.ListType;
import com.rbkmoney.damsel.wb_list.PaymentId;
import com.rbkmoney.damsel.wb_list.Result;
import com.rbkmoney.wb.list.manager.TestObjectFactory;
import com.rbkmoney.wb.list.manager.model.Row;
import com.rbkmoney.wb.list.manager.repository.ListRepository;
import org.apache.thrift.TException;
@ -18,30 +16,30 @@ import org.mockito.junit.jupiter.MockitoExtension;
import java.util.ArrayList;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
@ExtendWith(MockitoExtension.class)
public class WbListServiceHandlerTest {
public static final String VALUE = "value";
public static final String PARTY_ID = "partyId";
public static final String SHOP_ID = "shopId";
public static final String LIST_NAME = "listName";
private WbListServiceHandler wbListServiceHandler;
@Mock
private ListRepository listRepository;
private com.rbkmoney.damsel.wb_list.Row row;
@BeforeEach
void setUp() {
wbListServiceHandler = new WbListServiceHandler(listRepository, new ObjectMapper());
Row rowValue = new Row();
rowValue.setValue(TestObjectFactory.randomString());
row = TestObjectFactory.testRow();
row.setValue(rowValue.getValue());
Mockito.when(listRepository.get(anyString())).thenReturn(Optional.of(rowValue));
}
@Test
void isExist() throws TException {
com.rbkmoney.damsel.wb_list.Row row = createRow();
boolean exist = wbListServiceHandler.isExist(row);
assertTrue(exist);
@ -52,9 +50,8 @@ public class WbListServiceHandlerTest {
@Test
void getRowInfo() {
com.rbkmoney.damsel.wb_list.Row row = createRow();
Result result = wbListServiceHandler.getRowInfo(row);
assertFalse(result == null);
assertNotNull(result);
}
@Test
@ -63,8 +60,8 @@ public class WbListServiceHandlerTest {
boolean exist = wbListServiceHandler.isAllExist(list);
assertTrue(exist);
list.add(createRow());
list.add(createRow());
list.add(row);
list.add(row);
Mockito.when(listRepository.get(anyString())).thenReturn(Optional.of(new Row()));
assertTrue(wbListServiceHandler.isAllExist(list));
@ -80,8 +77,8 @@ public class WbListServiceHandlerTest {
boolean exist = wbListServiceHandler.isAnyExist(list);
assertFalse(exist);
list.add(createRow());
list.add(createRow());
list.add(row);
list.add(row);
Mockito.when(listRepository.get(anyString()))
.thenReturn(Optional.of(new Row()))
.thenReturn(Optional.empty());
@ -99,8 +96,8 @@ public class WbListServiceHandlerTest {
boolean exist = wbListServiceHandler.isNotOneExist(list);
assertTrue(exist);
list.add(createRow());
list.add(createRow());
list.add(row);
list.add(row);
Mockito.when(listRepository.get(anyString()))
.thenReturn(Optional.empty())
.thenReturn(Optional.empty());
@ -111,19 +108,4 @@ public class WbListServiceHandlerTest {
.thenReturn(Optional.of(new Row()));
assertFalse(wbListServiceHandler.isNotOneExist(list));
}
private com.rbkmoney.damsel.wb_list.Row createRow() {
Row value = new Row();
value.setValue(VALUE);
Mockito.when(listRepository.get(anyString())).thenReturn(Optional.of(value));
com.rbkmoney.damsel.wb_list.Row row = new com.rbkmoney.damsel.wb_list.Row();
row.setId(IdInfo.payment_id(new PaymentId()
.setShopId(SHOP_ID)
.setPartyId(PARTY_ID)
));
row.setListType(ListType.black);
row.setListName(LIST_NAME);
row.setValue(VALUE);
return row;
}
}