Add new methods for list check (#13)

* Add new methods for list check
This commit is contained in:
Kostya 2019-07-12 15:59:44 +03:00 committed by GitHub
parent 83891709a4
commit ef144cbe83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 114 additions and 10 deletions

View File

@ -10,7 +10,7 @@
</parent>
<artifactId>wb-list-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>wb-list-manager</name>
@ -24,7 +24,7 @@
<dockerfile.base.service.tag>bc95d0d6dc13c693acd2b274531a7d604b877bf3</dockerfile.base.service.tag>
<dockerfile.registry>dr2.rbkmoney.com</dockerfile.registry>
<shared.resources.version>0.3.6</shared.resources.version>
<wb.list.proto.version>1.20-2c2fb6a</wb.list.proto.version>
<wb.list.proto.version>1.21-aa59fcf</wb.list.proto.version>
<kafka.streams.version>2.1.0</kafka.streams.version>
<kafka.clients.version>2.1.0</kafka.clients.version>
<kafka.common.lib.version>0.0.9</kafka.common.lib.version>

View File

@ -9,6 +9,8 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import java.util.List;
@Slf4j
@RequiredArgsConstructor
public class WbListServiceHandler implements WbListServiceSrv.Iface {
@ -19,10 +21,49 @@ public class WbListServiceHandler implements WbListServiceSrv.Iface {
public boolean isExist(Row row) throws TException {
String key = KeyGenerator.generateKey(row);
try {
return listRepository.get(key).isPresent();
boolean present = listRepository.get(key).isPresent();
log.info("WbListServiceHandler isExist row: {} result: {}", row, present);
return present;
} catch (RiakExecutionException e) {
log.info("WbListServiceHandler error when isExist row: {} e: ", row, e);
throw new TException(e);
}
}
@Override
public boolean isAllExist(List<Row> list) throws TException {
if (list != null && !list.isEmpty()) {
for (Row row : list) {
if (!isExist(row)) {
return false;
}
}
}
return true;
}
@Override
public boolean isAnyExist(List<Row> list) throws TException {
if (list != null && !list.isEmpty()) {
for (Row row : list) {
if (isExist(row)) {
return true;
}
}
}
return false;
}
@Override
public boolean isNotOneExist(List<Row> list) throws TException {
if (list != null && !list.isEmpty()) {
for (Row row : list) {
if (isExist(row)) {
return false;
}
}
}
return true;
}
}

View File

@ -36,7 +36,7 @@ public class WbListStreamFactory {
StreamsBuilder builder = new StreamsBuilder();
builder.stream(readTopic, Consumed.with(Serdes.String(), commandSerde))
.filter((s, changeCommand) -> changeCommand != null && changeCommand.getCommand() != null)
.peek((s, changeCommand) -> log.debug("Command stream check command: {}", changeCommand))
.peek((s, changeCommand) -> log.info("Command stream check command: {}", changeCommand))
.mapValues(command ->
retryTemplate.execute(args -> commandService.apply(command)))
.to(resultTopic, Produced.with(Serdes.String(), eventSerde));

View File

@ -4,6 +4,7 @@ import com.rbkmoney.damsel.wb_list.ListType;
import com.rbkmoney.wb.list.manager.model.Row;
import com.rbkmoney.wb.list.manager.repository.ListRepository;
import org.apache.thrift.TException;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -11,6 +12,7 @@ import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import java.util.ArrayList;
import java.util.Optional;
import static org.mockito.ArgumentMatchers.anyString;
@ -34,6 +36,72 @@ public class WbListServiceHandlerTest {
@Test
public void isExist() throws TException {
com.rbkmoney.damsel.wb_list.Row row = createRow();
boolean exist = wbListServiceHandler.isExist(row);
Assert.assertTrue(exist);
Mockito.when(listRepository.get(anyString())).thenReturn(Optional.empty());
exist = wbListServiceHandler.isExist(row);
Assert.assertFalse(exist);
}
@Test
public void isAllExist() throws TException {
ArrayList<com.rbkmoney.damsel.wb_list.Row> list = new ArrayList<>();
boolean exist = wbListServiceHandler.isAllExist(list);
Assert.assertTrue(exist);
list.add(createRow());
list.add(createRow());
Mockito.when(listRepository.get(anyString())).thenReturn(Optional.of(new Row()));
Assert.assertTrue(wbListServiceHandler.isAllExist(list));
Mockito.when(listRepository.get(anyString()))
.thenReturn(Optional.of(new Row()))
.thenReturn(Optional.empty());
Assert.assertFalse(wbListServiceHandler.isAllExist(list));
}
@Test
public void isAnyExist() throws TException {
ArrayList<com.rbkmoney.damsel.wb_list.Row> list = new ArrayList<>();
boolean exist = wbListServiceHandler.isAnyExist(list);
Assert.assertFalse(exist);
list.add(createRow());
list.add(createRow());
Mockito.when(listRepository.get(anyString()))
.thenReturn(Optional.of(new Row()))
.thenReturn(Optional.empty());
Assert.assertTrue(wbListServiceHandler.isAnyExist(list));
Mockito.when(listRepository.get(anyString()))
.thenReturn(Optional.empty())
.thenReturn(Optional.empty());
Assert.assertFalse(wbListServiceHandler.isAnyExist(list));
}
@Test
public void isNoOneExist() throws TException {
ArrayList<com.rbkmoney.damsel.wb_list.Row> list = new ArrayList<>();
boolean exist = wbListServiceHandler.isNotOneExist(list);
Assert.assertTrue(exist);
list.add(createRow());
list.add(createRow());
Mockito.when(listRepository.get(anyString()))
.thenReturn(Optional.empty())
.thenReturn(Optional.empty());
Assert.assertTrue(wbListServiceHandler.isNotOneExist(list));
Mockito.when(listRepository.get(anyString()))
.thenReturn(Optional.empty())
.thenReturn(Optional.of(new Row()));
Assert.assertFalse(wbListServiceHandler.isNotOneExist(list));
}
@NotNull
private com.rbkmoney.damsel.wb_list.Row createRow() {
Row value = new Row();
value.setValue(VALUE);
Mockito.when(listRepository.get(anyString())).thenReturn(Optional.of(value));
@ -43,11 +111,6 @@ public class WbListServiceHandlerTest {
row.setListType(ListType.black);
row.setListName(LIST_NAME);
row.setValue(VALUE);
boolean exist = wbListServiceHandler.isExist(row);
Assert.assertTrue(exist);
Mockito.when(listRepository.get(anyString())).thenReturn(Optional.empty());
exist = wbListServiceHandler.isExist(row);
Assert.assertFalse(exist);
return row;
}
}