From 4cbe484789c5738ef9915fdf872e30ab99e80433 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Fri, 10 Nov 2023 15:38:53 +0800 Subject: [PATCH 1/2] add baseHelper, pageService, add test --- .../controller/CommentControllerTest.java | 69 +++++++++++++++++++ springWarehouse/pom.xml | 6 ++ .../controller/IndexController.java | 2 +- .../springWarehouse/service/PageService.java | 30 ++++++++ .../springWarehouse/util/BaseQueryHelper.java | 4 ++ .../util/MerchantQueryHelper.java | 2 +- .../util/OrderQueryHelper.java | 2 +- .../util/ProductQueryHelper.java | 2 +- .../controller/ProductTypeControllerTest.java | 51 ++++++++++++++ .../service/impl/MerchantServiceTest.java | 68 ++++++++++++++++++ 10 files changed, 232 insertions(+), 4 deletions(-) create mode 100644 springBootBlog/src/test/java/com/yen/mdblog/controller/CommentControllerTest.java create mode 100644 springWarehouse/src/main/java/com/yen/springWarehouse/service/PageService.java create mode 100644 springWarehouse/src/main/java/com/yen/springWarehouse/util/BaseQueryHelper.java create mode 100644 springWarehouse/src/test/java/com/yen/springWarehouse/controller/ProductTypeControllerTest.java create mode 100644 springWarehouse/src/test/java/com/yen/springWarehouse/service/impl/MerchantServiceTest.java diff --git a/springBootBlog/src/test/java/com/yen/mdblog/controller/CommentControllerTest.java b/springBootBlog/src/test/java/com/yen/mdblog/controller/CommentControllerTest.java new file mode 100644 index 000000000..0dacf0140 --- /dev/null +++ b/springBootBlog/src/test/java/com/yen/mdblog/controller/CommentControllerTest.java @@ -0,0 +1,69 @@ +package com.yen.mdblog.controller; + +import com.yen.mdblog.entity.Po.Comment; +import com.yen.mdblog.service.CommentService; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; + +import static org.junit.jupiter.api.Assertions.*; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; + +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.*; +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.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +@WebMvcTest(CommentController.class) +class CommentControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private CommentService commentService; + + @Test + public void testCreateComment() throws Exception { + + // mock service (productService.getProduct) + when(commentService + .insertComment(anyString(), anyLong(), anyString())) + .thenReturn(true); + + // TODO : fix below + // test post call + // https://stackoverflow.com/questions/21749781/why-i-received-an-error-403-with-mockmvc-and-junit +// var TOKEN_ATTR_NAME = "org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository.CSRF_TOKEN"; +// var httpSessionCsrfTokenRepository = new HttpSessionCsrfTokenRepository(); +// var csrfToken = httpSessionCsrfTokenRepository.generateToken(new MockHttpServletRequest()); +// +// mockMvc.perform(post("/comment/create") +// .sessionAttr(TOKEN_ATTR_NAME, csrfToken) +// .param(csrfToken.getParameterName(), csrfToken.getToken()) +// ).andExpect(status().isOk()); + + } + +} diff --git a/springWarehouse/pom.xml b/springWarehouse/pom.xml index 3ba22b720..dd9378045 100644 --- a/springWarehouse/pom.xml +++ b/springWarehouse/pom.xml @@ -164,6 +164,12 @@ 1.7.0 + + junit + junit + test + + diff --git a/springWarehouse/src/main/java/com/yen/springWarehouse/controller/IndexController.java b/springWarehouse/src/main/java/com/yen/springWarehouse/controller/IndexController.java index 8b0783a54..6c050d9e3 100644 --- a/springWarehouse/src/main/java/com/yen/springWarehouse/controller/IndexController.java +++ b/springWarehouse/src/main/java/com/yen/springWarehouse/controller/IndexController.java @@ -18,7 +18,7 @@ public class IndexController { @GetMapping("/") public String main(Map map){ - log.info(">>> mainController, map = {}", map); + log.info("IndexController, map = {}", map); return "main"; } diff --git a/springWarehouse/src/main/java/com/yen/springWarehouse/service/PageService.java b/springWarehouse/src/main/java/com/yen/springWarehouse/service/PageService.java new file mode 100644 index 000000000..4929e2c45 --- /dev/null +++ b/springWarehouse/src/main/java/com/yen/springWarehouse/service/PageService.java @@ -0,0 +1,30 @@ +package com.yen.springWarehouse.service; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.yen.springWarehouse.util.BaseQueryHelper; +import org.apache.commons.lang3.StringUtils; + +import java.util.List; + +public class PageService { + + public Page getPage(BaseQueryHelper helper, BaseMapper baseMapper, Integer pageNo, Integer pageSize){ + + Page page = new Page<>(pageNo,pageSize); + QueryWrapper wrapper = new QueryWrapper<>(); + + IPage elementList = baseMapper.selectPage(page, wrapper); + + if(elementList.getTotal() > 0){ + page.setRecords((List) elementList); + return page; + } + + return new Page<>(); + } + +} diff --git a/springWarehouse/src/main/java/com/yen/springWarehouse/util/BaseQueryHelper.java b/springWarehouse/src/main/java/com/yen/springWarehouse/util/BaseQueryHelper.java new file mode 100644 index 000000000..752d1f3e7 --- /dev/null +++ b/springWarehouse/src/main/java/com/yen/springWarehouse/util/BaseQueryHelper.java @@ -0,0 +1,4 @@ +package com.yen.springWarehouse.util; + +public interface BaseQueryHelper { +} diff --git a/springWarehouse/src/main/java/com/yen/springWarehouse/util/MerchantQueryHelper.java b/springWarehouse/src/main/java/com/yen/springWarehouse/util/MerchantQueryHelper.java index d859ac5c8..04fc96e25 100644 --- a/springWarehouse/src/main/java/com/yen/springWarehouse/util/MerchantQueryHelper.java +++ b/springWarehouse/src/main/java/com/yen/springWarehouse/util/MerchantQueryHelper.java @@ -5,7 +5,7 @@ import java.io.Serializable; @Data -public class MerchantQueryHelper implements Serializable { +public class MerchantQueryHelper implements Serializable, BaseQueryHelper { private static final long serialVersionUID = -977205069L; diff --git a/springWarehouse/src/main/java/com/yen/springWarehouse/util/OrderQueryHelper.java b/springWarehouse/src/main/java/com/yen/springWarehouse/util/OrderQueryHelper.java index e38115b68..df733f3c3 100644 --- a/springWarehouse/src/main/java/com/yen/springWarehouse/util/OrderQueryHelper.java +++ b/springWarehouse/src/main/java/com/yen/springWarehouse/util/OrderQueryHelper.java @@ -2,7 +2,7 @@ import java.io.Serializable; -public class OrderQueryHelper implements Serializable { +public class OrderQueryHelper implements Serializable, BaseQueryHelper { private static final long serialVersionUID = -977444405069L; diff --git a/springWarehouse/src/main/java/com/yen/springWarehouse/util/ProductQueryHelper.java b/springWarehouse/src/main/java/com/yen/springWarehouse/util/ProductQueryHelper.java index 423a7c22d..52b4458cd 100644 --- a/springWarehouse/src/main/java/com/yen/springWarehouse/util/ProductQueryHelper.java +++ b/springWarehouse/src/main/java/com/yen/springWarehouse/util/ProductQueryHelper.java @@ -5,7 +5,7 @@ import java.io.Serializable; @Data -public class ProductQueryHelper implements Serializable { +public class ProductQueryHelper implements Serializable, BaseQueryHelper { private static final long serialVersionUID = -9777777557205069L; diff --git a/springWarehouse/src/test/java/com/yen/springWarehouse/controller/ProductTypeControllerTest.java b/springWarehouse/src/test/java/com/yen/springWarehouse/controller/ProductTypeControllerTest.java new file mode 100644 index 000000000..2e5e88431 --- /dev/null +++ b/springWarehouse/src/test/java/com/yen/springWarehouse/controller/ProductTypeControllerTest.java @@ -0,0 +1,51 @@ +//package com.yen.springWarehouse.controller; +// +//import com.yen.springWarehouse.bean.ProductType; +//import com.yen.springWarehouse.service.ProductTypeService; +//import org.junit.runner.RunWith; +//import org.mybatis.spring.annotation.MapperScan; +//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +// +//import static org.junit.jupiter.api.Assertions.*; +// +//import com.fasterxml.jackson.databind.ObjectMapper; +//import org.junit.jupiter.api.Test; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +//import org.springframework.boot.test.context.SpringBootTest; +//import org.springframework.boot.test.mock.mockito.MockBean; +//import org.springframework.http.MediaType; +//import org.springframework.test.context.junit4.SpringRunner; +//import org.springframework.test.web.servlet.MockMvc; +// +//import static org.mockito.ArgumentMatchers.anyString; +//import static org.mockito.BDDMockito.given; +//import static org.mockito.Mockito.*; +//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.result.MockMvcResultMatchers.jsonPath; +//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +// +// +//@WebMvcTest(ProductTypeController.class) +//class ProductTypeControllerTest { +// +// @Autowired +// private MockMvc mockMvc; +// +// @MockBean +// private ProductTypeService productTypeService; +// +// @Test +// public void testInputType() throws Exception { +// +// // mock service +// when(productTypeService.save(new ProductType(123, "some_type_name"))). +// thenReturn(true); +// +// // controller call +//// mockMvc.perform(get("/productType/create")) +//// .andExpect(status().isOk()); +// } +// +//} \ No newline at end of file diff --git a/springWarehouse/src/test/java/com/yen/springWarehouse/service/impl/MerchantServiceTest.java b/springWarehouse/src/test/java/com/yen/springWarehouse/service/impl/MerchantServiceTest.java new file mode 100644 index 000000000..4b4562f78 --- /dev/null +++ b/springWarehouse/src/test/java/com/yen/springWarehouse/service/impl/MerchantServiceTest.java @@ -0,0 +1,68 @@ +package com.yen.springWarehouse.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.yen.springWarehouse.bean.Merchant; +import com.yen.springWarehouse.mapper.MerchantMapper; +import com.yen.springWarehouse.service.MerchantService; +import com.yen.springWarehouse.util.MerchantQueryHelper; +import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.BeforeEach; + +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.verify; + +@RunWith(SpringRunner.class) +@SpringBootTest +//@ExtendWith(MockitoExtension.class) +public class MerchantServiceTest { + +// private MerchantServiceImpl merchantService; + + @Mock + private MerchantMapper merchantMapper; + + @BeforeEach + public void setUp(){ + // TODO : check it + //merchantService = new MerchantServiceImpl(merchantMapper); + } + + @Test + public void testGetMerchantPage(){ + + // init + Page page = new Page<>(1,2); + QueryWrapper merchantWrapper = new QueryWrapper<>(); + merchantWrapper.like("name", new MerchantQueryHelper().getQryMerchantName()); + + // arrange + given(merchantMapper.getMerchantList(page, merchantWrapper)).willReturn(new ArrayList<>()); + + // act and assert +// MerchantQueryHelper helper = new MerchantQueryHelper(); +// Page merchantPage = merchantService.getMerchantPage(helper,1,1); +// assertThat(merchantPage.getPages()).isEqualTo(1); + } + +} \ No newline at end of file From 2b28cb65947bf8b15685e77e60e5c587671aae1f Mon Sep 17 00:00:00 2001 From: yennanliu Date: Fri, 10 Nov 2023 15:52:45 +0800 Subject: [PATCH 2/2] use page logic from service in controller --- .../controller/MerchantController.java | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/springWarehouse/src/main/java/com/yen/springWarehouse/controller/MerchantController.java b/springWarehouse/src/main/java/com/yen/springWarehouse/controller/MerchantController.java index c24e00731..e6bff8a8f 100644 --- a/springWarehouse/src/main/java/com/yen/springWarehouse/controller/MerchantController.java +++ b/springWarehouse/src/main/java/com/yen/springWarehouse/controller/MerchantController.java @@ -7,6 +7,7 @@ import com.yen.springWarehouse.bean.Merchant; import com.yen.springWarehouse.bean.ProductType; import com.yen.springWarehouse.service.MerchantService; +import com.yen.springWarehouse.util.MerchantQueryHelper; import com.yen.springWarehouse.util.csvUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -25,6 +26,8 @@ @RequestMapping("/merchant") public class MerchantController { + private final int PAGINATIONSIZE = 3; + @Autowired MerchantService merchantService; @@ -78,30 +81,33 @@ public String createBatch(@RequestParam("file") MultipartFile file, Map map, @RequestParam(value="pageNo", required=false, defaultValue="1") String pageNoStr) { - - int pageNo; + public String list(Map map, + @RequestParam(value = "pageNum", defaultValue = "0") int pageNum, + @RequestParam(value="pageSize", defaultValue = "0" + PAGINATIONSIZE) int pageSize) { - // check pageNo - pageNo = Integer.parseInt(pageNoStr); - if(pageNo < 1){ - pageNo = 1; + // check pageNum + if(pageNum < 1){ + pageNum = 1; } - /* - * 1st param:which page - * 2nd param : record count per page - */ - log.info("pageNo = {}", pageNo); - Page page = new Page<>(pageNo,3); - QueryWrapper queryWrapper = new QueryWrapper<>(); - IPage iPage = merchantService.page(page, - new LambdaQueryWrapper() - .orderByAsc(Merchant::getId) - ); - log.info("iPage.total = {}, iPage.getPages = {} iPage = {}", iPage.getTotal(), iPage.getPages(), iPage); +// +// /* +// * 1st param:which page +// * 2nd param : record count per page +// */ +// log.info("pageNo = {}", pageNo); +// Page page = new Page<>(pageNo,3); +// QueryWrapper queryWrapper = new QueryWrapper<>(); +// IPage iPage = merchantService.page(page, +// new LambdaQueryWrapper() +// .orderByAsc(Merchant::getId) +// ); +// log.info("iPage.total = {}, iPage.getPages = {} iPage = {}", iPage.getTotal(), iPage.getPages(), iPage); +// map.put("page", iPage); + + MerchantQueryHelper helper = new MerchantQueryHelper(); + IPage iPage = merchantService.getMerchantPage(helper, pageNum, pageSize); map.put("page", iPage); - return "merchant/list_merchant"; }