Skip to content
Snippets Groups Projects
Commit 834d324e authored by Josh Sun's avatar Josh Sun
Browse files

Merge branch '19-search-controller-and-service-unit-test' into 'master'

Resolve "search controller and service - unit test"

Closes #19

See merge request !21
parents 74634a9e dde09711
No related branches found
No related tags found
1 merge request!21Resolve "search controller and service - unit test"
......@@ -30,7 +30,16 @@
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.0.111-beta</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
......@@ -61,6 +70,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
......@@ -12,8 +12,6 @@ import com.example.freshonline.service.StockedGoodsService;
import com.example.freshonline.utils.PicUtils;
import com.example.freshonline.utils.RespBuilder;
import com.example.freshonline.utils.ValidationChecker;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
......@@ -23,7 +21,6 @@ import com.example.freshonline.model.joined_tables.GoodsCategory;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......
......@@ -37,7 +37,10 @@ public class SearchParams implements VerifyRequestData<SearchParams> {
public SearchParams verifyParams(Map<String, String> param){
SearchParams output = new SearchParams();
ValidationChecker vc = new ValidationChecker();
if (param.containsKey("page")){ output.setPage(vc.str2int(param.get("page"), -1)); }
if (param.containsKey("page")){
int tmpPage = vc.str2int(param.get("page"), 1);
output.setPage(tmpPage > 0 ? tmpPage: 1);
}
if (param.containsKey("price_low")) { output.setPrice_low(vc.str2int(param.get("price_low"), -1)); }
if (param.containsKey("price_high")) { output.setPrice_high(vc.str2int(param.get("price_high"), -1)); }
if (param.containsKey("brands")) {
......@@ -79,10 +82,9 @@ public class SearchParams implements VerifyRequestData<SearchParams> {
@Override
public String toString(){
return "price_low = " + price_low + " price_high = " + price_high
+ "\nbrands = " + brands + " keyword = " + keyword
+ "\nsort_type = " + sort_type + "\ncategory_id = " + category_id
+ "\npage = " + page + " item_low = " + item_low + " item_high = " + item_high;
return " category_id = " + category_id + " keyword = " + keyword
+ "\n selected brands = " + brands + " price_low = " + price_low + " price_high = " + price_high + " sort_type = " + sort_type
+ "\n page = " + page + " item_low = " + item_low + " item_high = " + item_high;
}
}
......@@ -7,7 +7,6 @@ import com.example.freshonline.dto.SearchParams;
import com.example.freshonline.dto.SearchResultInfo;
import com.example.freshonline.model.SaledGoodsExample;
import com.example.freshonline.model.StockedGoods;
import com.example.freshonline.model.StockedGoodsExample;
import com.example.freshonline.utils.PicUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -15,12 +14,7 @@ import org.springframework.stereotype.Service;
import com.example.freshonline.dao.GoodsCategoryMapper;
import com.example.freshonline.model.joined_tables.GoodsCategory;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
import java.sql.ResultSet;
@Service
public class StockedGoodsService {
......
package com.example.freshonline.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.freshonline.FreshOnlineApplication;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
/**
* How to run junit test?
* run with configuration, select junit and class
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {FreshOnlineApplication.class})
@AutoConfigureMockMvc
public class StockedGoodsControllerTests {
@Autowired
private StockedGoodsController stockedGoodsController;
@Autowired
private MockMvc mockMvc;
/**
* mockMvc: lib for testing controller, simulate web requests
*/
@Before
public void setup(){
mockMvc = MockMvcBuilders.standaloneSetup(stockedGoodsController).build();
}
/**
* @author Josh Sun
* @throws Exception
*/
@Test
public void testGetSearch() throws Exception{
String[] urls = new String[]{
"http://localhost:8080/goods", "http://localhost:8080/goods?",
"http://localhost:8080/goods?min_price=3",
"http://localhost:8080/goods?min_price=-1",
"http://localhost:8080/goods?min_price=-9.99",
"http://localhost:8080/goods?min_price=1000",
"http://localhost:8080/goods?max_price=20",
"http://localhost:8080/goods?max_price=-1.98",
"http://localhost:8080/goods?min_price=-9.99&max_price=-2.9",
"http://localhost:8080/goods?sort_type=0",
"http://localhost:8080/goods?sort_type=1",
"http://localhost:8080/goods?sort_type=5",
"http://localhost:8080/goods?sort_type=-5.98%min_price=abc",
"http://localhost:8080/goods?sort_type=-tnt%min_price=abc",
"http://localhost:8080/goods?category_id=shu",
"http://localhost:8080/goods?category_id=123,124",
"http://localhost:8080/goods?category_id=125&keyword=abc",
"http://localhost:8080/goods?category_id=125&page=-1",
"http://localhost:8080/goods?category_id=125&page=5252.32",
"http://localhost:8080/goods?brands=abc.tnt&keyword=beef",
}; // some test urls here
for (String url : urls){
System.out.println("testing getSearch, test url = " + url);
MvcResult mvcResult = mockMvc.perform(get(url)).andReturn();
System.out.println("response type: " + mvcResult.getResponse().getContentType());
JSONObject resp = JSONObject.parseObject(mvcResult.getResponse().getContentAsString());
Assert.assertNotNull(resp);
System.out.println("resp: " + resp);
System.out.println("data:" + resp.get("data"));
System.out.println("test finish, url = " + url);
}
}
}
package com.example.freshonline.dao;
import com.example.freshonline.FreshOnlineApplication;
import com.example.freshonline.dto.SearchParams;
import com.example.freshonline.dto.SearchResultInfo;
import com.example.freshonline.model.StockedGoods;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {FreshOnlineApplication.class})
class StockedGoodsMapperTests {
@Autowired
private StockedGoodsMapper stockedGoodsMapper;
@Test
public void testSelectByFilter() {
SearchParams param = new SearchParams();
param.setCategory_id("(123,124)");
param.setPage(1);
param.setBrands("('tnt','abc')");
List<StockedGoods> goods = stockedGoodsMapper.selectByFilter(param);
for (StockedGoods good : goods){
System.out.println(good);
}
}
@Test
public void testSearchInfo() {
SearchParams param = new SearchParams();
param.setCategory_id("(123,124)");
param.setPage(1);
param.setBrands("('tnt','abc')");
SearchResultInfo info = stockedGoodsMapper.searchInfo(param);
System.out.println(info);
}
}
\ No newline at end of file
package com.example.freshonline.service;
import com.alibaba.fastjson.JSONObject;
import com.example.freshonline.FreshOnlineApplication;
import com.example.freshonline.dto.SearchParams;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {FreshOnlineApplication.class})
class StockedGoodsServiceTests {
@Autowired
private StockedGoodsService stockedGoodsService;
/**
* @author Josh Sun
*/
@Test
public void testGetSearch() {
SearchParams param = new SearchParams();
param.setCategory_id("(123,124)");
param.setPage(1);
param.setBrands("('tnt','abc')");
JSONObject json = stockedGoodsService.getSearch(param);
System.out.println("testGetSearch: " + json);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment