---
refactoring tests
This commit is contained in:
psemenkov 2022-09-19 11:27:32 +03:00
parent 154c5cad89
commit a26f328d7f
5 changed files with 114 additions and 68 deletions

View file

@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hazelcast.core.IMap;
import com.hazelcast.map.listener.EntryRemovedListener;
import com.hazelcast.map.listener.EntryUpdatedListener;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.MockConsumer;
import org.apache.kafka.clients.consumer.OffsetResetStrategy;
@ -42,7 +43,7 @@ class ClearingMemberCategoryServiceTest {
private static final int PARTITION = 0;
private static final String TOPIC_MEMBER_CATEGORY_UPDATE = Consts.DESTINATION_CLEARING_MEMBER_CATEGORY_UPDATE;
private static final String TOPIC_MEMBER_CATEGORY_DELETE = Consts.DESTINATION_CLEARING_MEMBER_CATEGORY_DELETE;
private static Long currentId = 0L;
private static final Long ID = 0L;
@Autowired
@Qualifier("hazelcastServiceTest")
@ -58,15 +59,15 @@ class ClearingMemberCategoryServiceTest {
void clearingMemberCategoryUpdate() throws InterruptedException {
//ARRANGE
ClearingMemberCategory existsСlearingMemberCategory = new ClearingMemberCategory();
existsСlearingMemberCategory.setId(currentId);
existsСlearingMemberCategory.setId(ID);
existsСlearingMemberCategory.setClearingMemberCategory("0000");
ClearingMemberCategoryUpdateRequest memberCategoryUpdateRequest = new ClearingMemberCategoryUpdateRequest();
memberCategoryUpdateRequest.setId(currentId);
memberCategoryUpdateRequest.setId(ID);
memberCategoryUpdateRequest.setClearingMemberCategory("1234");
BaseRequest<ClearingMemberCategoryUpdateRequest> baseUpdateRequest = new BaseRequest<>();
baseUpdateRequest.setRequestPayload(memberCategoryUpdateRequest);
baseUpdateRequest.setId(currentId);
baseUpdateRequest.setId(ID);
baseUpdateRequest.setActionType(ActionType.UPDATE);
String jsonBaseForUpdatingRequest;
ObjectMapper objectMapper = new ObjectMapper();
@ -77,7 +78,7 @@ class ClearingMemberCategoryServiceTest {
}
ClearingMemberCategory predictableClearingMemberCategory = new ClearingMemberCategory();
predictableClearingMemberCategory.setId(currentId);
predictableClearingMemberCategory.setId(ID);
predictableClearingMemberCategory.setClearingMemberCategory("1234");
//ACT
@ -88,7 +89,7 @@ class ClearingMemberCategoryServiceTest {
clearingMemberCategoryService.afterPropertiesSet();
IMap<Long, ClearingMemberCategory> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_ClearingMemberCategory);
iMap.put(currentId, existsСlearingMemberCategory);
iMap.put(ID, existsСlearingMemberCategory);
//KAFKA
mockConsumer.schedulePollTask(() -> {
@ -100,21 +101,27 @@ class ClearingMemberCategoryServiceTest {
startOffsetsUpdating.put(tpUpdating, 0L);
mockConsumer.updateBeginningOffsets(startOffsetsUpdating);
//ASSERT
//waiting for hazelcast map item updates
Object waiter = new Object();
String listenerID = iMap.addEntryListener((EntryRemovedListener<Long, Contact>) entryEvent -> {
String listenerID = iMap.addEntryListener((EntryUpdatedListener<Long, Contact>) entryEvent -> {
System.out.println("Checking If removed..");
synchronized (waiter) {
try {
waiter.wait(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
waiter.notify();
}
}, false);
synchronized (waiter) {
waiter.wait(1000);
waiter.wait(100);
}
ClearingMemberCategory resultUpdating = iMap.get(currentId);
//ASSERT
ClearingMemberCategory resultUpdating = iMap.get(ID);
MEMBER_CATEGORY_MATCHER.assertMatch(resultUpdating, predictableClearingMemberCategory);
//preparing hazelcastImdgProvider for next test
@ -125,15 +132,15 @@ class ClearingMemberCategoryServiceTest {
void clearingMemberCategoryDelete() throws InterruptedException {
//ARRANGE
ClearingMemberCategory existsСlearingMemberCategory = new ClearingMemberCategory();
existsСlearingMemberCategory.setId(currentId);
existsСlearingMemberCategory.setId(ID);
existsСlearingMemberCategory.setClearingMemberCategory("0000");
CommonDeleteRequest memberCategoryDeleteRequest = new CommonDeleteRequest();
memberCategoryDeleteRequest.setId(currentId);
memberCategoryDeleteRequest.setId(ID);
BaseRequest<CommonDeleteRequest> baseDeleteRequest = new BaseRequest<>();
baseDeleteRequest.setRequestPayload(memberCategoryDeleteRequest);
baseDeleteRequest.setId(currentId);
baseDeleteRequest.setId(ID);
baseDeleteRequest.setActionType(ActionType.DELETE);
String jsonBaseForDeleteRequest;
ObjectMapper objectMapper = new ObjectMapper();
@ -151,7 +158,7 @@ class ClearingMemberCategoryServiceTest {
clearingMemberCategoryService.afterPropertiesSet();
IMap<Long, ClearingMemberCategory> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_ClearingMemberCategory);
iMap.put(currentId, existsСlearingMemberCategory);
iMap.put(ID, existsСlearingMemberCategory);
//KAFKA
mockConsumer.schedulePollTask(() -> {
@ -163,7 +170,7 @@ class ClearingMemberCategoryServiceTest {
startOffsetsUpdating.put(tpDeleting, 0L);
mockConsumer.updateBeginningOffsets(startOffsetsUpdating);
//ASSERT
//waiting for hazelcast map item removes
Object waiter = new Object();
String listenerID = iMap.addEntryListener((EntryRemovedListener<Long, Contact>) entryEvent -> {
System.out.println("Checking If removed..");
@ -177,6 +184,7 @@ class ClearingMemberCategoryServiceTest {
waiter.wait(1000);
}
//ASSERT
Assertions.assertEquals(0, iMap.size());
//preparing hazelcastImdgProvider for next test

View file

@ -3,6 +3,7 @@ package ru.spcex.clearing.company.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hazelcast.core.IMap;
import com.hazelcast.map.listener.EntryUpdatedListener;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.MockConsumer;
import org.apache.kafka.clients.consumer.OffsetResetStrategy;
@ -15,6 +16,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import ru.clearing.classes.statics.data.profile.CompanyInfo;
import ru.clearing.classes.statics.data.profile.Contact;
import ru.spcex.clearing.company.config.HazelcastServiceTestConfiguration;
import ru.spcex.clearing.company.utils.MatcherFactory;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
@ -37,7 +39,7 @@ class CompanyInfoServiceTest {
public static final MatcherFactory.Matcher<CompanyInfo> COMPANY_INFO_MATCHER = usingIgnoringFieldsComparator();
private static final int PARTITION = 0;
private static final String TOPIC_COMPANY_INFO_UPDATE = Consts.DESTINATION_COMPANY_INFO_UPDATE;
private static Long currentId = 0L;
private static final Long ID = 0L;
@Autowired
@Qualifier("hazelcastServiceTest")
@ -53,7 +55,8 @@ class CompanyInfoServiceTest {
void companyInfoUpdate() throws InterruptedException {
//ARRANGE
CompanyInfo existsCompanyInfo = new CompanyInfo();
existsCompanyInfo.setId(currentId);
existsCompanyInfo.setId(ID);
existsCompanyInfo.setCompanyId(ID);
existsCompanyInfo.setCorporationSoleType("0000");
existsCompanyInfo.setCountryCode("0000");
existsCompanyInfo.setDescription("exists description");
@ -67,7 +70,7 @@ class CompanyInfoServiceTest {
existsCompanyInfo.setFullName("exists fullName");
CompanyInfoUpdateRequest companyInfoUpdateRequest = new CompanyInfoUpdateRequest();
companyInfoUpdateRequest.setId(currentId);
companyInfoUpdateRequest.setId(ID);
companyInfoUpdateRequest.setCorporationSoleType("1234");
companyInfoUpdateRequest.setCountryCode("1234");
companyInfoUpdateRequest.setDescription("updated description");
@ -82,7 +85,7 @@ class CompanyInfoServiceTest {
BaseRequest<CompanyInfoUpdateRequest> baseUpdateRequest = new BaseRequest<>();
baseUpdateRequest.setRequestPayload(companyInfoUpdateRequest);
baseUpdateRequest.setId(currentId);
baseUpdateRequest.setId(ID);
baseUpdateRequest.setActionType(ActionType.UPDATE);
String jsonBaseForUpdatingRequest;
ObjectMapper objectMapper = new ObjectMapper();
@ -93,7 +96,8 @@ class CompanyInfoServiceTest {
}
CompanyInfo predictableCompanyInfo = new CompanyInfo();
predictableCompanyInfo.setId(currentId);
predictableCompanyInfo.setId(ID);
predictableCompanyInfo.setCompanyId(ID);
predictableCompanyInfo.setCorporationSoleType("1234");
predictableCompanyInfo.setCountryCode("1234");
predictableCompanyInfo.setDescription("updated description");
@ -109,13 +113,12 @@ class CompanyInfoServiceTest {
//ACT
//service set up
CompanyInfoService clearingMemberCategoryService = new CompanyInfoService(mockConsumer, hazelcastServiceTest);
Thread.sleep(10000);
//callbacks set up
clearingMemberCategoryService.afterPropertiesSet();
Thread.sleep(10000);
IMap<Long, CompanyInfo> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_Company);
iMap.put(currentId, existsCompanyInfo);
iMap.put(ID, existsCompanyInfo);
//KAFKA
mockConsumer.schedulePollTask(() -> {
@ -127,13 +130,25 @@ class CompanyInfoServiceTest {
startOffsetsUpdating.put(tpUpdating, 0L);
mockConsumer.updateBeginningOffsets(startOffsetsUpdating);
//waiting for hazelcast map updates
Object waiter = new Object();
String listenerID = iMap.addEntryListener((EntryUpdatedListener<Long, Contact>) entryEvent -> {
System.out.println("Checking If removed..");
synchronized (waiter) {
waiter.notify();
}
}, false);
synchronized (waiter) {
waiter.wait(1000);
}
//ASSERT
Thread.sleep(10000);
CompanyInfo resultUpdating = iMap.get(currentId);
CompanyInfo resultUpdating = iMap.get(ID);
COMPANY_INFO_MATCHER.assertMatch(resultUpdating, predictableCompanyInfo);
//preparing hazelcastImdgProvider for next test
iMap.clear();
currentId++;
iMap.removeEntryListener(listenerID);
}
}

View file

@ -3,6 +3,7 @@ package ru.spcex.clearing.company.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hazelcast.core.IMap;
import com.hazelcast.map.listener.EntryRemovedListener;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.MockConsumer;
import org.apache.kafka.clients.consumer.OffsetResetStrategy;
@ -17,6 +18,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import ru.clearing.classes.statics.data.company.Company;
import ru.clearing.classes.statics.data.generated.ClearingMemberCategory;
import ru.clearing.classes.statics.data.profile.Contact;
import ru.spcex.clearing.company.config.HazelcastServiceTestConfiguration;
import ru.spcex.clearing.company.utils.MatcherFactory;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
@ -39,7 +41,7 @@ class CompanyServiceTest {
public static final MatcherFactory.Matcher<ClearingMemberCategory> COMPANY_MATCHER = usingIgnoringFieldsComparator();
private static final int PARTITION = 0;
private static final String TOPIC_COMPANY_DELETE = Consts.DESTINATION_COMPANY_DELETE;
private static Long currentId = 0L;
private static final Long ID = 0L;
@Autowired
@Qualifier("hazelcastServiceTest")
@ -55,14 +57,14 @@ class CompanyServiceTest {
void deleteCompany() throws InterruptedException {
//ARRANGE
Company existsCompany = new Company();
existsCompany.setId(currentId);
existsCompany.setId(ID);
CommonDeleteRequest memberCategoryDeleteRequest = new CommonDeleteRequest();
memberCategoryDeleteRequest.setId(currentId);
memberCategoryDeleteRequest.setId(ID);
BaseRequest<CommonDeleteRequest> baseDeleteRequest = new BaseRequest<>();
baseDeleteRequest.setRequestPayload(memberCategoryDeleteRequest);
baseDeleteRequest.setId(currentId);
baseDeleteRequest.setId(ID);
baseDeleteRequest.setActionType(ActionType.DELETE);
String jsonBaseForDeleteRequest;
ObjectMapper objectMapper = new ObjectMapper();
@ -75,13 +77,12 @@ class CompanyServiceTest {
//ACT
//service set up
CompanyService companyService = new CompanyService(mockConsumer, hazelcastServiceTest);
Thread.sleep(10000);
//callbacks set up
companyService.afterPropertiesSet();
Thread.sleep(10000);
IMap<Long, Company> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_Company);
iMap.put(currentId, existsCompany);
iMap.put(ID, existsCompany);
//KAFKA
mockConsumer.schedulePollTask(() -> {
@ -93,12 +94,24 @@ class CompanyServiceTest {
startOffsetsUpdating.put(tpDeleting, 0L);
mockConsumer.updateBeginningOffsets(startOffsetsUpdating);
//waiting for hazelcast map item removes
Object waiter = new Object();
String listenerID = iMap.addEntryListener((EntryRemovedListener<Long, Contact>) entryEvent -> {
System.out.println("Checking If removed..");
synchronized (waiter) {
waiter.notify();
}
}, false);
synchronized (waiter) {
waiter.wait(100);
}
//ASSERT
Thread.sleep(10000);
Assertions.assertEquals(0, iMap.size());
//preparing hazelcastImdgProvider for next test
iMap.clear();
currentId++;
iMap.removeEntryListener(listenerID);
}
}

View file

@ -3,6 +3,7 @@ package ru.spcex.clearing.company.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hazelcast.core.IMap;
import com.hazelcast.map.listener.EntryUpdatedListener;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.MockConsumer;
import org.apache.kafka.clients.consumer.OffsetResetStrategy;
@ -15,6 +16,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import ru.clearing.classes.statics.data.company.CompanySymbols;
import ru.clearing.classes.statics.data.profile.Contact;
import ru.spcex.clearing.company.config.HazelcastServiceTestConfiguration;
import ru.spcex.clearing.company.utils.MatcherFactory;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
@ -37,7 +39,7 @@ class CompanySymbolServiceTest {
public static final MatcherFactory.Matcher<CompanySymbols> COMPANY_SYMBOL_MATCHER = usingIgnoringFieldsComparator();
private static final int PARTITION = 0;
private static final String TOPIC_COMPANY_SYMBOL_UPDATE = Consts.DESTINATION_COMPANY_SYMBOL_UPDATE;
private static Long currentId = 0L;
private static final Long ID = 0L;
@Autowired
@Qualifier("hazelcastServiceTest")
@ -53,20 +55,20 @@ class CompanySymbolServiceTest {
void companySymbolUpdate() throws InterruptedException {
//ARRANGE
CompanySymbols existsCompanySymbols = new CompanySymbols();
existsCompanySymbols.setId(currentId);
existsCompanySymbols.setCompanyId(currentId);
existsCompanySymbols.setId(ID);
existsCompanySymbols.setCompanyId(ID);
existsCompanySymbols.setCompanySymbol("0000");
existsCompanySymbols.setCompanySymbolValue("exists companySymbolValue");
CompanySymbolUpdateRequest companySymbolUpdateRequest = new CompanySymbolUpdateRequest();
companySymbolUpdateRequest.setId(currentId);
companySymbolUpdateRequest.setCompanyId(currentId);
companySymbolUpdateRequest.setId(ID);
companySymbolUpdateRequest.setCompanyId(ID);
companySymbolUpdateRequest.setCompanySymbol("1234");
companySymbolUpdateRequest.setCompanySymbolValue("new companySymbolValue");
BaseRequest<CompanySymbolUpdateRequest> baseUpdateRequest = new BaseRequest<>();
baseUpdateRequest.setRequestPayload(companySymbolUpdateRequest);
baseUpdateRequest.setId(currentId);
baseUpdateRequest.setId(ID);
baseUpdateRequest.setActionType(ActionType.UPDATE);
String jsonBaseForUpdatingRequest;
ObjectMapper objectMapper = new ObjectMapper();
@ -77,21 +79,20 @@ class CompanySymbolServiceTest {
}
CompanySymbols predictableCompanySymbols = new CompanySymbols();
predictableCompanySymbols.setId(currentId);
predictableCompanySymbols.setCompanyId(currentId);
predictableCompanySymbols.setId(ID);
predictableCompanySymbols.setCompanyId(ID);
predictableCompanySymbols.setCompanySymbol("0000");
predictableCompanySymbols.setCompanySymbolValue("new companySymbolValue");
//ACT
//service set up
CompanySymbolService companySymbolService = new CompanySymbolService(mockConsumer, hazelcastServiceTest);
Thread.sleep(10000);
//callbacks set up
companySymbolService.afterPropertiesSet();
Thread.sleep(10000);
IMap<Long, CompanySymbols> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_CompanySymbols);
iMap.put(currentId, existsCompanySymbols);
iMap.put(ID, existsCompanySymbols);
//KAFKA
mockConsumer.schedulePollTask(() -> {
@ -103,13 +104,25 @@ class CompanySymbolServiceTest {
startOffsetsUpdating.put(tpUpdating, 0L);
mockConsumer.updateBeginningOffsets(startOffsetsUpdating);
//waiting for hazelcast map item updates
Object waiter = new Object();
String listenerID = iMap.addEntryListener((EntryUpdatedListener<Long, Contact>) entryEvent -> {
System.out.println("Checking If removed..");
synchronized (waiter) {
waiter.notify();
}
}, false);
synchronized (waiter) {
waiter.wait(1000);
}
//ASSERT
Thread.sleep(10000);
CompanySymbols resultUpdating = iMap.get(currentId);
CompanySymbols resultUpdating = iMap.get(ID);
COMPANY_SYMBOL_MATCHER.assertMatch(resultUpdating, predictableCompanySymbols);
//preparing hazelcastImdgProvider for next test
iMap.clear();
currentId++;
iMap.removeEntryListener(listenerID);
}
}

View file

@ -3,7 +3,7 @@ package ru.spcex.clearing.company.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hazelcast.core.IMap;
import com.hazelcast.map.listener.EntryRemovedListener;
import com.hazelcast.map.listener.EntryUpdatedListener;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.MockConsumer;
import org.apache.kafka.clients.consumer.OffsetResetStrategy;
@ -38,7 +38,7 @@ class ContactServiceTest {
public static final MatcherFactory.Matcher<Contact> CONTACT_MATCHER = usingIgnoringFieldsComparator();
private static final int PARTITION = 0;
private static final String TOPIC_CONTACT_UPDATE = Consts.DESTINATION_CONTACT_UPDATE;
private static final Long currentId = 0L;
private static final Long ID = 0L;
@Autowired
@Qualifier("hazelcastServiceTest")
@ -54,20 +54,20 @@ class ContactServiceTest {
void contactUpdate() throws InterruptedException {
//ARRANGE
Contact existsContact = new Contact();
existsContact.setId(currentId);
existsContact.setCompanyId(currentId);
existsContact.setId(ID);
existsContact.setCompanyId(ID);
existsContact.setContactType("0000");
existsContact.setContactValue("exists ContactValue");
ContactUpdateRequest contactUpdateRequest = new ContactUpdateRequest();
contactUpdateRequest.setId(currentId);
contactUpdateRequest.setId(ID);
contactUpdateRequest.setCompanyId(1L);
contactUpdateRequest.setContactType("1234");
contactUpdateRequest.setContactValue("new ContactValue");
BaseRequest<ContactUpdateRequest> baseUpdateRequest = new BaseRequest<>();
baseUpdateRequest.setRequestPayload(contactUpdateRequest);
baseUpdateRequest.setId(currentId);
baseUpdateRequest.setId(ID);
baseUpdateRequest.setActionType(ActionType.UPDATE);
String jsonBaseForUpdatingRequest;
ObjectMapper objectMapper = new ObjectMapper();
@ -78,8 +78,8 @@ class ContactServiceTest {
}
Contact predictableContact = new Contact();
predictableContact.setId(currentId);
predictableContact.setCompanyId(currentId);
predictableContact.setId(ID);
predictableContact.setCompanyId(ID);
predictableContact.setContactType("0000");
predictableContact.setContactValue("new ContactValue");
@ -90,7 +90,7 @@ class ContactServiceTest {
//callbacks set up
IMap<Long, Contact> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_Contact);
iMap.put(currentId, existsContact);
iMap.put(ID, existsContact);
//KAFKA
mockConsumer.schedulePollTask(() -> {
@ -102,13 +102,10 @@ class ContactServiceTest {
startOffsetsUpdating.put(tpUpdating, 0L);
mockConsumer.updateBeginningOffsets(startOffsetsUpdating);
//ASSERT
//waiting for hazelcast map updates
Object waiter = new Object();
String listenerID = iMap.addEntryListener((EntryRemovedListener<Long, Contact>) entryEvent -> {
String listenerID = iMap.addEntryListener((EntryUpdatedListener<Long, Contact>) entryEvent -> {
System.out.println("Checking If removed..");
// resultUpdating = entryEvent.getValue();
// entryEvent.getKey();
// CONTACT_MATCHER.assertMatch(resultUpdating, predictableContact);
synchronized (waiter) {
waiter.notify();
@ -119,8 +116,8 @@ class ContactServiceTest {
waiter.wait(1000);
}
Contact resultUpdating = iMap.get(currentId);
//ASSERT
Contact resultUpdating = iMap.get(ID);
CONTACT_MATCHER.assertMatch(resultUpdating, predictableContact);
//preparing hazelcastImdgProvider for next test