Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warehouse-dev-004-code-refactor - refactor paging, service, add tests #116

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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());

}

}
6 changes: 6 additions & 0 deletions springWarehouse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@
<version>1.7.0</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class IndexController {
@GetMapping("/")
public String main(Map<String, Object> map){

log.info(">>> mainController, map = {}", map);
log.info("IndexController, map = {}", map);
return "main";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +26,8 @@
@RequestMapping("/merchant")
public class MerchantController {

private final int PAGINATIONSIZE = 3;

@Autowired
MerchantService merchantService;

Expand Down Expand Up @@ -78,30 +81,33 @@ public String createBatch(@RequestParam("file") MultipartFile file, Map<String,


@GetMapping("/list")
public String list(Map<String, Object> map, @RequestParam(value="pageNo", required=false, defaultValue="1") String pageNoStr) {

int pageNo;
public String list(Map<String, Object> 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<Merchant> page = new Page<>(pageNo,3);
QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
IPage<Merchant> iPage = merchantService.page(page,
new LambdaQueryWrapper<Merchant>()
.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<Merchant> page = new Page<>(pageNo,3);
// QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
// IPage<Merchant> iPage = merchantService.page(page,
// new LambdaQueryWrapper<Merchant>()
// .orderByAsc(Merchant::getId)
// );
// log.info("iPage.total = {}, iPage.getPages = {} iPage = {}", iPage.getTotal(), iPage.getPages(), iPage);
// map.put("page", iPage);

MerchantQueryHelper helper = new MerchantQueryHelper();
IPage<Merchant> iPage = merchantService.getMerchantPage(helper, pageNum, pageSize);
map.put("page", iPage);

return "merchant/list_merchant";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> {

public Page<T> getPage(BaseQueryHelper helper, BaseMapper baseMapper, Integer pageNo, Integer pageSize){

Page<T> page = new Page<>(pageNo,pageSize);
QueryWrapper<T> wrapper = new QueryWrapper<>();

IPage elementList = baseMapper.selectPage(page, wrapper);

if(elementList.getTotal() > 0){
page.setRecords((List<T>) elementList);
return page;
}

return new Page<>();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.yen.springWarehouse.util;

public interface BaseQueryHelper {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
// }
//
//}
Original file line number Diff line number Diff line change
@@ -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<Merchant> page = new Page<>(1,2);
QueryWrapper<Merchant> 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<Merchant> merchantPage = merchantService.getMerchantPage(helper,1,1);
// assertThat(merchantPage.getPages()).isEqualTo(1);
}

}