fix failing test cases

This commit is contained in:
Vishwas Babu A J
2015-03-04 22:41:44 +05:30
parent d9ce1745da
commit a16265eb92
7 changed files with 55 additions and 76 deletions
@@ -105,25 +105,29 @@ public class LoanTransactionHelper {
} }
public HashMap approveLoan(final String approvalDate, final Integer loanID) { public HashMap approveLoan(final String approvalDate, final Integer loanID) {
return performLoanTransaction(createLoanOperationURL(APPROVE_LOAN_COMMAND, loanID), getApproveLoanAsJSON(approvalDate)); String loanApprovalCommand = createLoanOperationURL(APPROVE_LOAN_COMMAND, loanID);
String loanApprovalRequest = getApproveLoanAsJSON(approvalDate);
System.out.println("Loan approval command:" + loanApprovalCommand);
System.out.println("Loan approval request:" + loanApprovalRequest);
return performLoanTransaction(loanApprovalCommand, loanApprovalRequest);
} }
public HashMap approveLoanWithApproveAmount(final String approvalDate, final String expectedDisbursementDate, final String approvalAmount, final Integer loanID, public HashMap approveLoanWithApproveAmount(final String approvalDate, final String expectedDisbursementDate,
List<HashMap> tranches) { final String approvalAmount, final Integer loanID, List<HashMap> tranches) {
return performLoanTransaction(createLoanOperationURL(APPROVE_LOAN_COMMAND, loanID), return performLoanTransaction(createLoanOperationURL(APPROVE_LOAN_COMMAND, loanID),
getApproveLoanAsJSON(approvalDate, expectedDisbursementDate, approvalAmount, tranches)); getApproveLoanAsJSON(approvalDate, expectedDisbursementDate, approvalAmount, tranches));
} }
public List<HashMap<String, Object>> approveLoanForTranches(final String approvalDate, final String expectedDisbursementDate, final String approvalAmount, final Integer loanID, public List<HashMap<String, Object>> approveLoanForTranches(final String approvalDate, final String expectedDisbursementDate,
List<HashMap> tranches, final String responseAttribute) { final String approvalAmount, final Integer loanID, List<HashMap> tranches, final String responseAttribute) {
return (List<HashMap<String, Object>>) performLoanTransaction(createLoanOperationURL(APPROVE_LOAN_COMMAND, loanID), return (List<HashMap<String, Object>>) performLoanTransaction(createLoanOperationURL(APPROVE_LOAN_COMMAND, loanID),
getApproveLoanAsJSON(approvalDate, expectedDisbursementDate, approvalAmount, tranches), responseAttribute); getApproveLoanAsJSON(approvalDate, expectedDisbursementDate, approvalAmount, tranches), responseAttribute);
} }
public Object approveLoan(final String approvalDate, final String approvalAmount, final Integer loanID, final String responseAttribute) { public Object approveLoan(final String approvalDate, final String approvalAmount, final Integer loanID, final String responseAttribute) {
final String approvalURL = createLoanOperationURL(APPROVE_LOAN_COMMAND, loanID); final String approvalURL = createLoanOperationURL(APPROVE_LOAN_COMMAND, loanID);
final String approvalJSONData = getApproveLoanAsJSON(approvalDate,null, approvalAmount, null); final String approvalJSONData = getApproveLoanAsJSON(approvalDate, null, approvalAmount, null);
return performLoanTransaction(approvalURL, approvalJSONData, responseAttribute); return performLoanTransaction(approvalURL, approvalJSONData, responseAttribute);
} }
@@ -233,11 +237,11 @@ public class LoanTransactionHelper {
} }
private String getApproveLoanAsJSON(final String approvalDate) { private String getApproveLoanAsJSON(final String approvalDate) {
return getApproveLoanAsJSON(approvalDate,null, null, null); return getApproveLoanAsJSON(approvalDate, null, null, null);
} }
private String getApproveLoanAsJSON(final String approvalDate, final String expectedDisbursementDate, final String approvalAmount, private String getApproveLoanAsJSON(final String approvalDate, final String expectedDisbursementDate, final String approvalAmount,
List<HashMap> tranches) { List<HashMap> tranches) {
final HashMap<String, Object> map = new HashMap<>(); final HashMap<String, Object> map = new HashMap<>();
map.put("locale", "en"); map.put("locale", "en");
map.put("dateFormat", "dd MMMM yyyy"); map.put("dateFormat", "dd MMMM yyyy");
@@ -245,11 +249,11 @@ public class LoanTransactionHelper {
map.put("approvedLoanAmount", approvalAmount); map.put("approvedLoanAmount", approvalAmount);
} }
map.put("approvedOnDate", approvalDate); map.put("approvedOnDate", approvalDate);
if(expectedDisbursementDate != null){ if (expectedDisbursementDate != null) {
map.put("expectedDisbursementDate", expectedDisbursementDate); map.put("expectedDisbursementDate", expectedDisbursementDate);
} }
if(tranches != null && tranches.size() > 0){ if (tranches != null && tranches.size() > 0) {
map.put("disbursementData",tranches); map.put("disbursementData", tranches);
} }
map.put("note", "Approval NOTE"); map.put("note", "Approval NOTE");
return new Gson().toJson(map); return new Gson().toJson(map);
@@ -407,7 +411,7 @@ public class LoanTransactionHelper {
private Object performLoanTransaction(final String postURLForLoanTransaction, final String jsonToBeSent, final String responseAttribute) { private Object performLoanTransaction(final String postURLForLoanTransaction, final String jsonToBeSent, final String responseAttribute) {
return Utils.performServerPost(this.requestSpec, this.responseSpec, postURLForLoanTransaction, jsonToBeSent, responseAttribute); return Utils.performServerPost(this.requestSpec, this.responseSpec, postURLForLoanTransaction, jsonToBeSent, responseAttribute);
} }
public Object adjustLoanTransaction(final Integer loanId, final Integer transactionId, final String date, public Object adjustLoanTransaction(final Integer loanId, final Integer transactionId, final String date,
final String transactionAmount, final String responseAttribute) { final String transactionAmount, final String responseAttribute) {
return adjustLoanTransaction(loanId, transactionId, getAdjustTransactionJSON(date, transactionAmount), responseAttribute); return adjustLoanTransaction(loanId, transactionId, getAdjustTransactionJSON(date, transactionAmount), responseAttribute);
@@ -36,7 +36,6 @@ public class JournalEntryData {
private final BigDecimal amount; private final BigDecimal amount;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final CurrencyData currency; private final CurrencyData currency;
@SuppressWarnings("unused")
private final String transactionId; private final String transactionId;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final Boolean manualEntry; private final Boolean manualEntry;
@@ -25,7 +25,6 @@ public class CommandWrapperBuilder {
private String transactionId; private String transactionId;
private Long productId; private Long productId;
private Long templateId; private Long templateId;
private Long tellerId;
public CommandWrapper build() { public CommandWrapper build() {
return new CommandWrapper(this.officeId, this.groupId, this.clientId, this.loanId, this.savingsId, this.actionName, return new CommandWrapper(this.officeId, this.groupId, this.clientId, this.loanId, this.savingsId, this.actionName,
@@ -62,11 +61,6 @@ public class CommandWrapperBuilder {
return this; return this;
} }
public CommandWrapperBuilder withTellerId(final Long withTellerId) {
this.tellerId = withTellerId;
return this;
}
public CommandWrapperBuilder withJson(final String withJson) { public CommandWrapperBuilder withJson(final String withJson) {
this.json = withJson; this.json = withJson;
return this; return this;
@@ -744,7 +738,7 @@ public class CommandWrapperBuilder {
this.actionName = "UPDATE"; this.actionName = "UPDATE";
this.entityName = "DISBURSEMENTDETAIL"; this.entityName = "DISBURSEMENTDETAIL";
this.entityId = null; this.entityId = null;
this.loanId = loanId; this.loanId = loanId;
this.href = "/loans/" + loanId + "/editdisbursementdetails/"; this.href = "/loans/" + loanId + "/editdisbursementdetails/";
return this; return this;
} }
@@ -14,7 +14,6 @@ public class CodeValueData implements Serializable {
private final Long id; private final Long id;
@SuppressWarnings("unused")
private final String name; private final String name;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@@ -53,7 +52,7 @@ public class CodeValueData implements Serializable {
public Long getId() { public Long getId() {
return this.id; return this.id;
} }
public String getName() { public String getName() {
return this.name; return this.name;
} }
@@ -74,7 +74,6 @@ import org.mifosplatform.portfolio.loanaccount.exception.LoanApplicationDateExce
import org.mifosplatform.portfolio.loanaccount.exception.LoanApplicationNotInSubmittedAndPendingApprovalStateCannotBeDeleted; import org.mifosplatform.portfolio.loanaccount.exception.LoanApplicationNotInSubmittedAndPendingApprovalStateCannotBeDeleted;
import org.mifosplatform.portfolio.loanaccount.exception.LoanApplicationNotInSubmittedAndPendingApprovalStateCannotBeModified; import org.mifosplatform.portfolio.loanaccount.exception.LoanApplicationNotInSubmittedAndPendingApprovalStateCannotBeModified;
import org.mifosplatform.portfolio.loanaccount.exception.LoanNotFoundException; import org.mifosplatform.portfolio.loanaccount.exception.LoanNotFoundException;
import org.mifosplatform.portfolio.loanaccount.exception.MultiDisbursementDataRequiredException;
import org.mifosplatform.portfolio.loanaccount.loanschedule.domain.AprCalculator; import org.mifosplatform.portfolio.loanaccount.loanschedule.domain.AprCalculator;
import org.mifosplatform.portfolio.loanaccount.loanschedule.domain.LoanScheduleModel; import org.mifosplatform.portfolio.loanaccount.loanschedule.domain.LoanScheduleModel;
import org.mifosplatform.portfolio.loanaccount.loanschedule.service.LoanScheduleAssembler; import org.mifosplatform.portfolio.loanaccount.loanschedule.service.LoanScheduleAssembler;
@@ -140,7 +139,6 @@ public class LoanApplicationWritePlatformServiceJpaRepositoryImpl implements Loa
private final ConfigurationDomainService configurationDomainService; private final ConfigurationDomainService configurationDomainService;
private final LoanScheduleAssembler loanScheduleAssembler; private final LoanScheduleAssembler loanScheduleAssembler;
@Autowired @Autowired
public LoanApplicationWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context, final FromJsonHelper fromJsonHelper, public LoanApplicationWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context, final FromJsonHelper fromJsonHelper,
final LoanApplicationTransitionApiJsonValidator loanApplicationTransitionApiJsonValidator, final LoanApplicationTransitionApiJsonValidator loanApplicationTransitionApiJsonValidator,
@@ -740,21 +738,17 @@ public class LoanApplicationWritePlatformServiceJpaRepositoryImpl implements Loa
.build(); .build();
} }
public void validateMultiDisbursementData(final JsonCommand command, LocalDate expectedDisbursementDate){ public void validateMultiDisbursementData(final JsonCommand command, LocalDate expectedDisbursementDate) {
final String json = command.json(); final String json = command.json();
final JsonElement element = this.fromJsonHelper.parse(json); final JsonElement element = this.fromJsonHelper.parse(json);
final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("loan"); final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("loan");
final JsonArray disbursementDataArray = command.arrayOfParameterNamed(LoanApiConstants.disbursementDataParameterName); final BigDecimal principal = this.fromJsonHelper.extractBigDecimalWithLocaleNamed("approvedLoanAmount", element);
if(disbursementDataArray == null || disbursementDataArray.size() == 0){
final String errorMessage = "For this loan product, disbursement details must be provided";
throw new MultiDisbursementDataRequiredException(LoanApiConstants.disbursementDataParameterName, errorMessage);
}
final BigDecimal principal = this.fromJsonHelper.extractBigDecimalWithLocaleNamed("approvedLoanAmount", element);
fromApiJsonDeserializer.validateLoanMultiDisbursementdate(element, baseDataValidator, expectedDisbursementDate, principal); fromApiJsonDeserializer.validateLoanMultiDisbursementdate(element, baseDataValidator, expectedDisbursementDate, principal);
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); } if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); }
} }
@Transactional @Transactional
@Override @Override
public CommandProcessingResult approveApplication(final Long loanId, final JsonCommand command) { public CommandProcessingResult approveApplication(final Long loanId, final JsonCommand command) {
@@ -763,43 +757,43 @@ public class LoanApplicationWritePlatformServiceJpaRepositoryImpl implements Loa
LocalDate expectedDisbursementDate = null; LocalDate expectedDisbursementDate = null;
this.loanApplicationTransitionApiJsonValidator.validateApproval(command.json()); this.loanApplicationTransitionApiJsonValidator.validateApproval(command.json());
final Loan loan = retrieveLoanBy(loanId); final Loan loan = retrieveLoanBy(loanId);
final JsonArray disbursementDataArray = command.arrayOfParameterNamed(LoanApiConstants.disbursementDataParameterName); final JsonArray disbursementDataArray = command.arrayOfParameterNamed(LoanApiConstants.disbursementDataParameterName);
expectedDisbursementDate = command.localDateValueOfParameterNamed(LoanApiConstants.disbursementDateParameterName); expectedDisbursementDate = command.localDateValueOfParameterNamed(LoanApiConstants.disbursementDateParameterName);
if(expectedDisbursementDate == null){ if (expectedDisbursementDate == null) {
expectedDisbursementDate = loan.getExpectedDisbursedOnLocalDate(); expectedDisbursementDate = loan.getExpectedDisbursedOnLocalDate();
} }
if(loan.loanProduct().isMultiDisburseLoan()){ if (loan.loanProduct().isMultiDisburseLoan()) {
this.validateMultiDisbursementData(command, expectedDisbursementDate); this.validateMultiDisbursementData(command, expectedDisbursementDate);
} }
checkClientOrGroupActive(loan); checkClientOrGroupActive(loan);
// validate expected disbursement date against meeting date // validate expected disbursement date against meeting date
if (loan.isSyncDisbursementWithMeeting() && (loan.isGroupLoan() || loan.isJLGLoan())) { if (loan.isSyncDisbursementWithMeeting() && (loan.isGroupLoan() || loan.isJLGLoan())) {
final CalendarInstance calendarInstance = this.calendarInstanceRepository.findCalendarInstaneByEntityId(loan.getId(), final CalendarInstance calendarInstance = this.calendarInstanceRepository.findCalendarInstaneByEntityId(loan.getId(),
CalendarEntityType.LOANS.getValue()); CalendarEntityType.LOANS.getValue());
final Calendar calendar = calendarInstance.getCalendar(); final Calendar calendar = calendarInstance.getCalendar();
this.loanScheduleAssembler.validateDisbursementDateWithMeetingDates(expectedDisbursementDate, calendar); this.loanScheduleAssembler.validateDisbursementDateWithMeetingDates(expectedDisbursementDate, calendar);
} }
final Map<String, Object> changes = loan.loanApplicationApproval(currentUser, command, disbursementDataArray, defaultLoanLifecycleStateMachine()); final Map<String, Object> changes = loan.loanApplicationApproval(currentUser, command, disbursementDataArray,
defaultLoanLifecycleStateMachine());
if (!changes.isEmpty()) { if (!changes.isEmpty()) {
// If loan approved amount less than loan demanded amount, then need // If loan approved amount less than loan demanded amount, then need
// to recompute the schedule // to recompute the schedule
if (changes.containsKey(LoanApiConstants.approvedLoanAmountParameterName)) { if (changes.containsKey(LoanApiConstants.approvedLoanAmountParameterName)) {
ScheduleGeneratorDTO scheduleGeneratorDTO = loanAccountDomainService.buildScheduleGeneratorDTO(loan); ScheduleGeneratorDTO scheduleGeneratorDTO = loanAccountDomainService.buildScheduleGeneratorDTO(loan);
loan.regenerateRepaymentSchedule(scheduleGeneratorDTO, currentUser); loan.regenerateRepaymentSchedule(scheduleGeneratorDTO, currentUser);
} }
this.loanRepository.save(loan); this.loanRepository.save(loan);
final String noteText = command.stringValueOfParameterNamed("note"); final String noteText = command.stringValueOfParameterNamed("note");
if (StringUtils.isNotBlank(noteText)) { if (StringUtils.isNotBlank(noteText)) {
final Note note = Note.loanNote(loan, noteText); final Note note = Note.loanNote(loan, noteText);
@@ -23,7 +23,6 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
import org.mifosplatform.infrastructure.core.service.ThreadLocalContextUtil;
import org.mifosplatform.template.domain.Template; import org.mifosplatform.template.domain.Template;
import org.mifosplatform.template.domain.TemplateFunctions; import org.mifosplatform.template.domain.TemplateFunctions;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
@@ -49,19 +48,15 @@ public class TemplateMergeService {
this.authToken = authToken; this.authToken = authToken;
} }
public String compile(final Template template, public String compile(final Template template, final Map<String, Object> scopes) throws MalformedURLException, IOException {
final Map<String, Object> scopes) throws MalformedURLException,
IOException {
this.scopes = scopes; this.scopes = scopes;
this.scopes.put("static", new TemplateFunctions()); this.scopes.put("static", new TemplateFunctions());
final MustacheFactory mf = new DefaultMustacheFactory(); final MustacheFactory mf = new DefaultMustacheFactory();
final Mustache mustache = mf.compile( final Mustache mustache = mf.compile(new StringReader(template.getText()), template.getName());
new StringReader(template.getText()), template.getName());
final Map<String, Object> mappers = getCompiledMapFromMappers(template final Map<String, Object> mappers = getCompiledMapFromMappers(template.getMappersAsMap());
.getMappersAsMap());
this.scopes.putAll(mappers); this.scopes.putAll(mappers);
final StringWriter stringWriter = new StringWriter(); final StringWriter stringWriter = new StringWriter();
@@ -70,16 +65,14 @@ public class TemplateMergeService {
return stringWriter.toString(); return stringWriter.toString();
} }
private Map<String, Object> getCompiledMapFromMappers( private Map<String, Object> getCompiledMapFromMappers(final Map<String, String> data) {
final Map<String, String> data) {
final MustacheFactory mf = new DefaultMustacheFactory(); final MustacheFactory mf = new DefaultMustacheFactory();
if (data != null) { if (data != null) {
for (final Map.Entry<String, String> entry : data.entrySet()) { for (final Map.Entry<String, String> entry : data.entrySet()) {
final Mustache mappersMustache = mf.compile(new StringReader( final Mustache mappersMustache = mf.compile(new StringReader(entry.getValue()), "");
entry.getValue()), "");
final StringWriter stringWriter = new StringWriter(); final StringWriter stringWriter = new StringWriter();
mappersMustache.execute(stringWriter, this.scopes); mappersMustache.execute(stringWriter, this.scopes);
@@ -100,13 +93,11 @@ public class TemplateMergeService {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Map<String, Object> getMapFromUrl(final String url) private Map<String, Object> getMapFromUrl(final String url) throws MalformedURLException, IOException {
throws MalformedURLException, IOException {
final HttpURLConnection connection = getConnection(url); final HttpURLConnection connection = getConnection(url);
final String response = getStringFromInputStream(connection final String response = getStringFromInputStream(connection.getInputStream());
.getInputStream());
HashMap<String, Object> result = new HashMap<>(); HashMap<String, Object> result = new HashMap<>();
if (connection.getContentType().equals("text/plain")) { if (connection.getContentType().equals("text/plain")) {
result.put("src", response); result.put("src", response);
@@ -119,17 +110,14 @@ public class TemplateMergeService {
private HttpURLConnection getConnection(final String url) { private HttpURLConnection getConnection(final String url) {
if (this.authToken == null) { if (this.authToken == null) {
final String name = SecurityContextHolder.getContext() final String name = SecurityContextHolder.getContext().getAuthentication().getName();
.getAuthentication().getName(); final String password = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString();
final String password = SecurityContextHolder.getContext()
.getAuthentication().getCredentials().toString();
Authenticator.setDefault(new Authenticator() { Authenticator.setDefault(new Authenticator() {
@Override @Override
protected PasswordAuthentication getPasswordAuthentication() { protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(name, password return new PasswordAuthentication(name, password.toCharArray());
.toCharArray());
} }
}); });
} }
@@ -138,8 +126,7 @@ public class TemplateMergeService {
try { try {
connection = (HttpURLConnection) new URL(url).openConnection(); connection = (HttpURLConnection) new URL(url).openConnection();
if (this.authToken != null) { if (this.authToken != null) {
connection.setRequestProperty("Authorization", "Basic " connection.setRequestProperty("Authorization", "Basic " + this.authToken);
+ this.authToken);
} }
TrustModifier.relaxHostChecking(connection); TrustModifier.relaxHostChecking(connection);
@@ -0,0 +1,2 @@
INSERT INTO `job` (`name`, `display_name`, `cron_expression`, `create_time`) VALUES ('Generate Mandatory Savings Schedule', 'Generate Mandatory Savings Schedule', '0 5 0 1/1 * ? *', now());