I have a method which does multiple validations which are dependent on earlier one. This is purely a REST Service with no form/frontend. e.g.
public Json processPayment(User user, Amount amount, CardData cardData) {
Json result = new Json();
Json userResult = validateUser(user);
if (userResult.isNotValid())
result.put("errorCode", userResult.get("errorCode");
result.put("message", userResult.get("message");
return result;
}
Merchant merchant = getMerchant(user);
Json merchantResult = validateMerchant(user);
if (merchantResult.isNotValid())
result.put("errorCode", merchantResult.get("errorCode");
result.put("message", merchantResult.get("message");
return result;
}
Json limitsResult = validateLimits(user, merchant, amount);
if (limitsResult.isNotValid())
result.put("errorCode", limitsResult.get("errorCode");
result.put("message", limitsResult.get("message");
return result;
}
// Like above there are few more steps.
.
.
.
// All validations are fine process transaction.
Json transactionResult = processTransaction(user, merchant, amount, cardData);
if (transactionResult.isNotValid())
result.put("errorCode", transactionResult.get("errorCode");
result.put("message", transactionResult.get("message");
} else {
result.put("message", "Transaction Successful");
result.put("referenceNumber, transactionResult.get("rrn");
}
return result;
}
In each step, if the results are invalid then it should return immediately with the error message otherwise continue to next step.
Due to multiple steps, this method has become too big and almost impossible to do unit testing.
I want to break this method into smaller ones. I have already moved all the business logic of each step into separate methods but still the flow remains in this big method.
Sonarlint CC is 47 which is a big worry.
Please suggest what would be the right approach to handle this.
Thank you.
Here is a little example which could be a solution for you.
The main idea is each validation step shares one common context. This context holds every information of your validation process.
Next you have a queue of validators. Each represents one validation step. A validator changes the context (like adding the merchant object), calls your validation methods and changes the result of the context if necessary.
The validation process itself just iterates over the queue looking for a failing validator.
Just run this code. Maybe it helps:
import java.util.*;
interface PaymentValidatorInterface {
public boolean validate(PaymentValidationContext context);
}
class PaymentValidationContext {
String result = "";
String user;
int cardData;
String merchant;
public PaymentValidationContext(String user, int cardData) {
this.user = user;
this.cardData = cardData;
}
}
class PaymentValidator {
public static boolean validateUser(PaymentValidationContext context) {
if (context.user == null) {
context.result += "User is wrong\n";
return false;
}
return true;
}
public static boolean validateMerchant(PaymentValidationContext context) {
context.merchant = context.user + "#" + context.cardData;
if (context.merchant.length() <= 3) {
context.result += "Marchant is wrong\n";
return false;
}
return true;
}
public static boolean finishValidation(PaymentValidationContext context) {
context.result += "Everything is fine.\n";
return true;
}
}
public class Processor {
private final static Queue<PaymentValidatorInterface> validators = new LinkedList<>();
static {
validators.add(PaymentValidator::validateUser);
validators.add(PaymentValidator::validateMerchant);
validators.add(PaymentValidator::finishValidation);
}
public String processPayment(String user, int cardData) {
PaymentValidationContext context = new PaymentValidationContext(user, cardData);
validators.stream().anyMatch(validator -> !validator.validate(context));
return context.result;
}
// For testing -------
public static void main(String[] args) {
Processor p = new Processor();
System.out.print(p.processPayment("Foobar", 1337)); // ok
System.out.print(p.processPayment(null, 1337)); // fails
System.out.print(p.processPayment("", 1)); // fails
}
}
You can write doValidation() function like the following.
private doValidation(Json validationResult, Json result) {
if (validationResult.isNotValid())
result.put("errorCode", validationResult.get("errorCode");
result.put("message", validationResult.get("message");
return false;//validation failed
}
return true;//validation passed
}
and Call this method from processPayment() method.
public Json processPayment(User user, Amount amount, CardData cardData) {
Json result = new Json();
if( !doAllValidations(user,amount,cardData, result) )
return result;
// All validations are fine process transaction.
Json transactionResult = processTransaction(user, merchant, amount, cardData);
if (transactionResult.isNotValid())
result.put("errorCode", transactionResult.get("errorCode");
result.put("message", transactionResult.get("message");
} else {
result.put("message", "Transaction Successful");
result.put("referenceNumber, transactionResult.get("rrn");
}
return result;
}
Finally you can move all validations to some other method if you want.
public bool doAllValidations(User user, Amount amount, CardData cardData, result) {
Json userResult = validateUser(user);
if (!doValidation(userResult, result))
return result;
Merchant merchant = getMerchant(user);
Json merchantResult = validateMerchant(user);
if (!doValidation(merchantResult, result))
return result;
Json limitsResult = validateLimits(user, merchant, amount);
if (!doValidation(limitsResult, result))
return result;
....
}
Related
I have a class name HibernateSessionManager which have static method
public static HibernateSessionManager current;
I trying to mock
public Mbc_session getMBCSessionByGuid(String sessionGuid) {
try {
return HibernateSessionManager.current.withSession(hibernateSession -> {
return hibernateSession.get(Mbc_session.class, sessionGuid);
});
}
catch (Exception e) {
logger.error().logFormattedMessage(Constants.MBC_SESSION_GET_ERROR_STRING,
e.getMessage()); throw new DAOException(ErrorCode.MBC_1510.getCode(), ErrorCode.MBC_1510.getErrorMessage() + ",Operation: getMBCSessionByGuid");
}
}
i am using following function in #before
public static void initMocks(Session session) {
HibernateSessionManager.current = mock(HibernateSessionManager.class,Mockito.RETURNS_DEEP_STUBS);
HibernateTransactionManager.current = mock(HibernateTransactionManager.class,Mockito.RETURNS_DEEP_STUBS);
doCallRealMethod().when(HibernateTransactionManager.current).withTransaction(any(), any());
doCallRealMethod().when(HibernateSessionManager.current).withSession(any(Consumer.class));
// Mockito.when(HibernateSessionManager.current.withSession((Consumer<Session>) any(Function.class))).thenCallRealMethod();
when(HibernateSessionManager.current.getSession()).thenReturn(session);
}
My test case is following
#Test public void test_getMBCSessionByGuid() {
Mbc_session mbcSession = new Mbc_session();
String sessionGuid = "session GUID";
when(HibernateSessionManager.current.getSession()).thenReturn(session);
// when(sessionFactory.getCurrentSession()).thenReturn(session);
when(session.get(Mbc_session.class, sessionGuid)).thenReturn(mbcSession);
Mbc_session mbcSession2 = mbc_sessionDao.getMBCSessionByGuid(sessionGuid);
assertNull(mbcSession2);
}
it passed but coverage is not touching following code
return hibernateSession.get(Mbc_session.class, sessionGuid);
here is my withSession code
public void withSession(Consumer<Session> task) {
Session hibernateSession = getSession();
try {
task.accept(hibernateSession);
} finally {
HibernateSessionManager.current.closeSession(hibernateSession);
}
}
openSession
public Session getSession() {
Session threadCachedSession = threadSession.get();
if (threadCachedSession != null) {
if (!threadCachedSession.isOpen()) { throw new
IllegalStateException("Session closed outside of
HibernateSessionManager.");
}
return threadCachedSession;
} return sessionFactory.openSession();
}
Looking at the code and assuming it compiles, I believe the problem is that you have two withSession(...) methods and in the code posted you are trying to mock the wrong one. Here are their signatures:
// You should NOT mock this one
void withSession(Consumer<Session> task) {
...
}
// You should mock this one instead
Mbc_session withSession(Function<Session, Mbc_session> task) {
...
}
It was easy to guess as the getMBCSessionByGuid method contains the snippet below with the Function<Session, Mbc_session> being passed as an argument to withSession(...) instead of Consumer<Session>:
return HibernateSessionManager.current.withSession(hibernateSession -> {
// something is returned which means a Function is passed, not a Consumer
return hibernateSession.get(Mbc_session.class, sessionGuid);
});
As an easy fix, you can just add the following to the test:
doCallRealMethod().when(HibernateSessionManager.current).withSession(any(Function.class));
and remove the existing mock configuration with a Consumer:
doCallRealMethod().when(HibernateSessionManager.current).withSession(any(Consumer.class));
P.S. Just in case, I can easily reproduce the issue on my machine.
I want to create a unit test for a client service.
The function of the client service is to call the webservice, get data, and update the database as scheduled.
The scheduled method return void.
How to create unit test for a
client service
schedule method
void returning methods
The client is like this:
#ApplicationScoped
public class ClientClass {
private static final Logger LOGGER = Logger.getLogger(VdsClient.class);
#Inject
Client client;
VMR vmr;
CommandService commandService;
public VdsClient(VMR vmr,
CommandService commandService) {
this.vmr = vmr;
this.commandService = commandService;
}
#Scheduled(XXX)
public void getVal() {
var monitoringStateFilter =
new VMF.vmf(true, true);
var monoResultList =
vmr.fvms(monitoringStateFilter)
.collectList();
if (monoResultList != null) {
var resultList = monoResultList.block();
if (resultList != null) {
resultList.stream()
.map(row -> row.getValue("val", val.class))
.map(vin -> this.updateEstimate(val.getValue()))
}
}
}
public Tuple2<String, Boolean> updateEstimate(String val) {
List<route> routeList;
try {
routeList = vdsClient.getval(val)
.getItem();
boolean hasDealerDestination = false;
for (Route route : routeList) {
if (vd.DestLocationType._00.value()
.equals(route.getTransportConnectionPointTyp())) {
hasDealerDestination = true;
var estimate = DateTimeUtil.convertToInstantWithOffset(route.getArrivalDate(),
route.getArrivalTime(), route.getTimezone(), DateTimeUtil.PATTERN_LONG_DATE);
if (estimate == null) {
return Tuples.of(val, false);
}
var result = this.updateVehicleEstimate(val, estimate);
return Tuples.of(val, result);
}
}
if (!hasDealerDestination) {
return Tuples.of(val, false);
} else {
return Tuples.of(val, false);
}
} catch (route e) {
return Tuples.of(val, false);
} catch (Exception e) {
return Tuples.of(val, false);
}
}
public Boolean updateVehicleEstimate(String val, Instant estimate) {
var vehicleUpdate = vu.vuc.builder()
.val(new Val(val))
.Estimate(estimate)
.build();
return (Boolean) cs.ec(vu).block();
}
A unit should only be testing that a particular unit of code is working fine. In your case, for the unit test, you should assume that the webservice will return the data and the db updation works fine. We can accomplish this through mocking the response for each of these calls.
For void returning methods, you can actually verify if the call was indeed made or not.
For example:
Mockito.verify(mockedObject, Mockito.times(1)).callWebService(mockedParameter1, mockedParameter2);
There is another way, though I personally don't prefer that:
You can declare a class variable and make sure the value updates itself whenever the scheduled method reaches the end of the code. Read that value in your test and assert on its value. If the value is updated, then your code worked fine, else NO and it's a failure.
Also, in case you want to actually make sure the webservice returned the correct response / db entry was updated, then those should be part of integration tests and not unit tests.
I can easily query the Alfresco audit log in REST using this query:
http://localhost:8080/alfresco/service/api/audit/query/audit-custom?verbose=true
But how to perform the same request in Java within Alfresco module?
It must be synchronous.
A lazy solution would be to call the REST URL in Java, but it would probably be inefficient, and more importantly it would require me to store an admin's password somewhere.
I noticed AuditService has a auditQuery method so I am trying to call it. Unfortunately it seems to be for asynchronous operations? I don't need callbacks, as I need to wait until the queried data is ready before going on to the next step.
Here is my implementation, mostly copied from the source code of the REST API:
int maxResults = 10000;
if (!auditService.isAuditEnabled(AUDIT_APPLICATION, ("/" + AUDIT_APPLICATION))) {
throw new WebScriptException(
"Auditing for " + AUDIT_APPLICATION + " is disabled!");
}
final List<Map<String, Object>> entries =
new ArrayList<Map<String,Object>>(limit);
AuditQueryCallback callback = new AuditQueryCallback() {
#Override
public boolean valuesRequired() {
return true; // true = verbose
}
#Override
public boolean handleAuditEntryError(
Long entryId, String errorMsg, Throwable error) {
return true;
}
#Override
public boolean handleAuditEntry(
Long entryId,
String applicationName,
String user,
long time,
Map<String, Serializable> values) {
// Convert values to Strings
Map<String, String> valueStrings =
new HashMap<String, String>(values.size() * 2);
for (Map.Entry<String, Serializable> mapEntry : values.entrySet()) {
String key = mapEntry.getKey();
Serializable value = mapEntry.getValue();
try {
String valueString = DefaultTypeConverter.INSTANCE.convert(
String.class, value);
valueStrings.put(key, valueString);
}
catch (TypeConversionException e) {
// Use the toString()
valueStrings.put(key, value.toString());
}
}
entry.put(JSON_KEY_ENTRY_VALUES, valueStrings);
}
entries.add(entry);
return true;
}
};
AuditQueryParameters params = new AuditQueryParameters();
params.setApplicationName(AUDIT_APPLICATION);
params.setForward(true);
auditService.auditQuery(callback, params, maxResults);
Though the callback might it look asynchronous, it is not.
I have a method inside a class that I am mocking that returns a boolean value.
#Service
public boolean writeToHDFS(HashMap<String, String> tableHistoryMap, String text, String tableName, String siteName, String customer,
boolean incremental){
try{
String absoluteFilepath = hdfsService.generateHDFSAbsolutePathNoDate(tableHistoryMap, tableName, siteName, customer, incremental);
Path writeFile = new Path(absoluteFilepath);
if (!hdfsService.HDFSFileExists(writeFile)) {
writer = hdfs.create(writeFile, false);
logger.info("hdfs file create set up");
}
else {
writer = hdfs.append(writeFile);
logger.info("hdfs file append set up");
//TODO: WRITING HERE
//IF stripping capability is enabled, strip headers then
text = hdfsService.parseText(stripHeaders, text, tableName);
}
if(hdfsService.writerHDFS(writer,text)){
return true;
}
}
catch(Exception ex){
logger.error(ex.getMessage());
}
return false;
}
#MockBean
WriteDataIntoHDFSService writeDataIntoHDFSService
#Test
public void testDepositData(){
KafkaMessage kafkaMessage = new KafkaMessage();
kafkaMessage.setInitialLoadRunning(true);
kafkaMessage.setCustomer("customer");
kafkaMessage.setMessageContent("message");
kafkaMessage.setTableName("table");
kafkaMessage.setInitialLoadComplete(false);
HashMap<String, String> tableMap = new HashMap<>();
tableMap.put("Test","Test");
when(writeDataIntoHDFSService.writeToHDFS(tableMap, kafkaMessage.getMessageContent(), kafkaMessage.getTableName(), "Site", kafkaMessage.getCustomer(),
false)).thenReturn(true);
Assert.assertTrue(kafkaService.depositData(kafkaMessage,"test",kafkaMessage.getCustomer(),tableMap));
}
The assertion fails and I debugged it and it returns WrongTypOfReturnValue at when()...thenReturn(true). It says boolean cannot be returned by toString().
Log:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
Boolean cannot be returned by toString()
toString() should return String
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
dataDeposit:
public boolean depositData(KafkaMessage kafkaMessage, String topic, HashMap<String, String> tableHistoryMap){
String customer = getCustomer(topic);
String site = getSite(topic);
if (kafkaMessage.isInitialLoadRunning() && !kafkaMessage.isInitialLoadComplete()) {
//runInitial(kafkaMessage, splitKafkaTopic[2]);
if (writeDataIntoHDFSService
.writeToHDFS(tableHistoryMap, kafkaMessage.getMessageContent(), kafkaMessage.getTableName(), site,
kafkaMessage.getCustomer(), false)) {
logger.info("Wrote initial data into HDFS file for: " + kafkaMessage.getTableName());
synchronized (lock) {
kafkaConsumer.commitSync();
logger.info("Kafka commit sync done for initial load.");
}
return true;
}
}
return false;
}
UPDATE: not even sure what I did but it seems to be working now...
I am writing a controller, that I need to make it asynchronous. How can I deal with a list of ListenableFuture? Because I have a list of URLs that I need to send GET request one by one, what is the best solution for it?
#RequestMapping(value = "/repositories", method = RequestMethod.GET)
private void getUsername(#RequestParam(value = "username") String username) {
System.out.println(username);
List<ListenableFuture> futureList = githubRestAsync.getRepositoryLanguages(username);
System.out.println(futureList.size());
}
In the service I use List<ListanbleFuture> which seems does not work, since it is asynchronous, in the controller method I cannot have the size of futureList to run a for loop on it for the callbacks.
public List<ListenableFuture> getRepositoryLanguages(String username){
return getRepositoryLanguages(username, getUserRepositoriesFuture(username));
}
private ListenableFuture getUserRepositoriesFuture(String username) throws HttpClientErrorException {
HttpEntity entity = new HttpEntity(httpHeaders);
ListenableFuture future = restTemplate.exchange(githubUsersUrl + username + "/repos", HttpMethod.GET, entity, String.class);
return future;
}
private List<ListenableFuture> getRepositoryLanguages(final String username, ListenableFuture<ResponseEntity<String>> future) {
final List<ListenableFuture> futures = new ArrayList<>();
future.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
#Override
public void onSuccess(ResponseEntity<String> response) {
ObjectMapper mapper = new ObjectMapper();
try {
repositories = mapper.readValue(response.getBody(), new TypeReference<List<Repositories>>() {
});
HttpEntity entity = new HttpEntity(httpHeaders);
System.out.println("Repo size: " + repositories.size());
for (int i = 0; i < repositories.size(); i++) {
futures.add(restTemplate.exchange(githubReposUrl + username + "/" + repositories.get(i).getName() + "/languages", HttpMethod.GET, entity, String.class));
}
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onFailure(Throwable throwable) {
System.out.println("FAILURE in getRepositoryLanguages: " + throwable.getMessage());
}
});
return futures;
}
Should I use something like ListenableFuture<List> instead of List<ListenableFuture> ?
It seems like you have a List<ListenableFuture<Result>>, but you want a ListenableFuture<List<Result>>, so you can take one action when all of the futures are complete.
public static <T> ListenableFuture<List<T>> allOf(final List<? extends ListenableFuture<? extends T>> futures) {
// we will return this ListenableFuture, and modify it from within callbacks on each input future
final SettableListenableFuture<List<T>> groupFuture = new SettableListenableFuture<>();
// use a defensive shallow copy of the futures list, to avoid errors that could be caused by
// someone inserting/removing a future from `futures` list after they call this method
final List<? extends ListenableFuture<? extends T>> futuresCopy = new ArrayList<>(futures);
// Count the number of completed futures with an AtomicInt (to avoid race conditions)
final AtomicInteger resultCount = new AtomicInteger(0);
for (int i = 0; i < futuresCopy.size(); i++) {
futuresCopy.get(i).addCallback(new ListenableFutureCallback<T>() {
#Override
public void onSuccess(final T result) {
int thisCount = resultCount.incrementAndGet();
// if this is the last result, build the ArrayList and complete the GroupFuture
if (thisCount == futuresCopy.size()) {
List<T> resultList = new ArrayList<T>(futuresCopy.size());
try {
for (ListenableFuture<? extends T> future : futuresCopy) {
resultList.add(future.get());
}
groupFuture.set(resultList);
} catch (Exception e) {
// this should never happen, but future.get() forces us to deal with this exception.
groupFuture.setException(e);
}
}
}
#Override
public void onFailure(final Throwable throwable) {
groupFuture.setException(throwable);
// if one future fails, don't waste effort on the others
for (ListenableFuture future : futuresCopy) {
future.cancel(true);
}
}
});
}
return groupFuture;
}
Im not quite sure if you are starting a new project or working on a legacy one, but if the main requirement for you is none blocking and asynchronous rest service I would suggest you to have a look into upcoming Spring Framework 5 and it integration with reactive streams. Particularly Spring 5 will allow you to create fully reactive and asynchronous web services with little of coding.
So for example fully functional version of your code can be written with this small code snippet.
#RestController
public class ReactiveController {
#GetMapping(value = "/repositories")
public Flux<String> getUsername(#RequestParam(value = "username") String username) {
WebClient client = WebClient.create(new ReactorClientHttpConnector());
ClientRequest<Void> listRepoRequest = ClientRequest.GET("https://api.github.com/users/{username}/repos", username)
.accept(MediaType.APPLICATION_JSON).header("user-agent", "reactive.java").build();
return client.exchange(listRepoRequest).flatMap(response -> response.bodyToFlux(Repository.class)).flatMap(
repository -> client
.exchange(ClientRequest
.GET("https://api.github.com/repos/{username}/{repo}/languages", username,
repository.getName())
.accept(MediaType.APPLICATION_JSON).header("user-agent", "reactive.java").build())
.map(r -> r.bodyToMono(String.class)))
.concatMap(Flux::merge);
}
static class Repository {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
To run this code locally just clone the spring-boot-starter-web-reactive and copy the code into it.
The result is something like {"Java":50563,"JavaScript":11541,"CSS":1177}{"Java":50469}{"Java":130182}{"Shell":21222,"Makefile":7169,"JavaScript":1156}{"Java":30754,"Shell":7058,"JavaScript":5486,"Batchfile":5006,"HTML":4865} still you can map it to something more usable in asynchronous way :)