Java tutorial
/* * Copyright 2012-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.finance.it.micro_lending; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.util.Date; import java.util.List; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.time.DateUtils; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultHandler; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import com.fasterxml.jackson.databind.ObjectMapper; import com.finance.it.micro_lending.controller.rest.beans.LoanApplication; import com.finance.it.micro_lending.controller.rest.beans.LoanExtention; import com.finance.it.micro_lending.controller.rest.beans.Message; import com.finance.it.micro_lending.controller.rest.beans.UserApplication; import com.finance.it.micro_lending.domain.Loan; import com.finance.it.micro_lending.domain.User; import com.finance.it.micro_lending.repository.LoanRepository; import com.finance.it.micro_lending.repository.UserRepository; import com.finance.it.micro_lending.util.TestUtil; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = AppConfiguration.class) @WebAppConfiguration @Slf4j public class RestApplicationTests { @Autowired private WebApplicationContext context; private MockMvc mvc; @Autowired private LoanRepository loanRepository; private @Autowired UserRepository userRepository; @BeforeClass public static void setupClass() { Date currentDate = new Date(); System.setProperty("loan.thershold.start.time", TestUtil.addHour(currentDate, 1) + ":00"); System.setProperty("loan.thershold.end.time", TestUtil.addHour(currentDate, 1) + ":30"); System.setProperty("loan.maxAmount", "500"); System.setProperty("loan.day.maxAttempts", "3"); } @AfterClass public static void tearDownClass() { System.getProperties().remove("loan.thershold.start.time"); System.getProperties().remove("loan.thershold.end.time"); System.getProperties().remove("loan.maxAmount"); System.getProperties().remove("loan.day.maxAttempts"); } @Before public void setUp() { this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build(); } @Test public void testGetLoanOfInvalidUser() throws Exception { this.mvc.perform(get("/user/-4343/loan").contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()).andDo(new ResultHandler() { @Override public void handle(MvcResult result) throws Exception { String jsonResult = result.getResponse().getContentAsString(); log.info("Received Json str : " + jsonResult); } }); } @Test public void testAddUserWithInvalidPayload() throws Exception { this.mvc.perform(post("/user").contentType(MediaType.APPLICATION_JSON).content("{\"first\":\"4343\"}")) .andExpect(status().isBadRequest()).andDo(new ResultHandler() { @Override public void handle(MvcResult result) throws Exception { String jsonResult = result.getResponse().getContentAsString(); log.info("Received Json str : " + jsonResult); } }); } @Test public void testAddUserWithValidPayload() throws Exception { UserApplication userObj = new UserApplication("Test", "Test", "test_address", "email@cm.com", "Warsaw", "01-654", "Warsaw", "Poland", "4543fgfxxf"); final ObjectMapper mapper = new ObjectMapper(); log.info("Input parameter - User : " + mapper.writeValueAsString(userObj)); this.mvc.perform( post("/user").contentType(MediaType.APPLICATION_JSON).content(mapper.writeValueAsString(userObj))) .andExpect(status().isCreated()).andDo(new ResultHandler() { @Override public void handle(MvcResult result) throws Exception { String jsonOP = result.getResponse().getContentAsString(); log.info("Received Json str : " + jsonOP); jsonOP = jsonOP.substring(0, jsonOP.indexOf(",\"links\":[")) + "}"; final Message message = mapper.readValue(jsonOP, Message.class); mvc.perform( get("/user/" + message.getReferenceId()).contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()).andDo(new ResultHandler() { @Override public void handle(MvcResult result) throws Exception { String jsonResult = result.getResponse().getContentAsString(); log.info("Received Json str : " + jsonResult); jsonResult = jsonResult.substring(0, jsonResult.indexOf(",\"links\":[")) + "}"; mapper.readValue(jsonResult, User.class); } }); mvc.perform(get("/user/" + message.getReferenceId() + "/loan") .contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNoContent()) .andDo(new ResultHandler() { @Override public void handle(MvcResult result) throws Exception { String jsonResult = result.getResponse().getContentAsString(); log.info("Received Json str : " + jsonResult); } }); } }); } @Test public void testRequestLoan() throws Exception { UserApplication userObj = new UserApplication("Test", "Test", "test_address", "email@cm.com", "Warsaw", "01-654", "Warsaw", "Poland", "4543fgxxx"); final ObjectMapper mapper = new ObjectMapper(); this.mvc.perform( post("/user").contentType(MediaType.APPLICATION_JSON).content(mapper.writeValueAsString(userObj))) .andExpect(status().isCreated()).andDo(new ResultHandler() { @Override public void handle(MvcResult result) throws Exception { String jsonOP = result.getResponse().getContentAsString(); log.info("Received Json str : " + jsonOP); jsonOP = jsonOP.substring(0, jsonOP.indexOf(",\"links\":[")) + "}"; final Message message = mapper.readValue(jsonOP, Message.class); LoanApplication loanObj = new LoanApplication(150, 7); mvc.perform(post("/user/" + message.getReferenceId() + "/loan") .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(loanObj))).andExpect(status().isAccepted()) .andDo(new ResultHandler() { @Override public void handle(MvcResult result) throws Exception { String jsonResult = result.getResponse().getContentAsString(); log.info("Received Json str : " + jsonResult); mvc.perform(get("/user/" + message.getReferenceId() + "/loan") .contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()) .andDo(new ResultHandler() { @Override public void handle(MvcResult result) throws Exception { String jsonResult = result.getResponse() .getContentAsString(); log.info("Received Json str : " + jsonResult); } }); } }); } }); } @Test public void testRequestLoanExtention() throws Exception { UserApplication userObj = new UserApplication("Test", "Test", "test_address", "email@cm.com", "Warsaw", "01-654", "Warsaw", "Poland", "4543fgyyyy"); final ObjectMapper mapper = new ObjectMapper(); this.mvc.perform( post("/user").contentType(MediaType.APPLICATION_JSON).content(mapper.writeValueAsString(userObj))) .andExpect(status().isCreated()).andDo(new ResultHandler() { @Override public void handle(MvcResult result) throws Exception { String jsonOP = result.getResponse().getContentAsString(); log.info("Received Json str : " + jsonOP); jsonOP = jsonOP.substring(0, jsonOP.indexOf(",\"links\":[")) + "}"; Message message = mapper.readValue(jsonOP, Message.class); final LoanApplication loanObj = new LoanApplication(499, 7); final String userId = message.getReferenceId(); mvc.perform(post("/user/" + message.getReferenceId() + "/loan") .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(loanObj))).andExpect(status().isAccepted()) .andDo(new ResultHandler() { @Override public void handle(MvcResult result) throws Exception { String jsonOP = result.getResponse().getContentAsString(); log.info("Received Json str : " + jsonOP); jsonOP = jsonOP.substring(0, jsonOP.indexOf(",\"links\":[")) + "}"; Message message = mapper.readValue(jsonOP, Message.class); List<Loan> loan = loanRepository .getLoansByReferenceId(message.getReferenceId()); final Date loanTermDate = loan.get(0).getTerm(); LoanExtention loanExtention = new LoanExtention(5); final String loanRefId = message.getReferenceId(); mvc.perform(put("/user/" + userId + "/loan/" + message.getReferenceId()) .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(loanExtention))) .andExpect(status().isAccepted()).andDo(new ResultHandler() { @Override public void handle(MvcResult result) throws Exception { String jsonResult = result.getResponse() .getContentAsString(); log.info("Received Json str : " + jsonResult); } }); loan = loanRepository.getLoansByReferenceId(message.getReferenceId()); assertEquals(loan.size(), 1); assertTrue(loan.get(0).isExtended()); assertEquals(loan.get(0).getCreatedBy(), userRepository.findOne(Long.parseLong(userId))); assertEquals(DateUtils.addDays(loan.get(0).getTerm(), 0), DateUtils.addDays(loanTermDate, loanExtention.getTerm())); mvc.perform(post("/user/" + userId + "/loan") .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(loanObj))) .andExpect(status().isAccepted()).andDo(new ResultHandler() { @Override public void handle(MvcResult result) throws Exception { User user = userRepository.findOne(Long.parseLong(userId)); assertEquals(2, user.getLoans().size()); String jsonOP = result.getResponse().getContentAsString(); log.info("Received Json str : " + jsonOP); jsonOP = jsonOP.substring(0, jsonOP.indexOf(",\"links\":[")) + "}"; Message message = mapper.readValue(jsonOP, Message.class); assertTrue(user.getLoans().containsKey(loanRefId)); assertTrue(user.getLoans() .containsKey(message.getReferenceId())); } }); } }); } }); } }