Skip to content

Commit

Permalink
[#4] Add missing WebMvcTests
Browse files Browse the repository at this point in the history
  • Loading branch information
isbel8ai committed Jun 10, 2024
1 parent 3a3bc27 commit 9bc808d
Show file tree
Hide file tree
Showing 10 changed files with 480 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@ public Product replaceProduct(@PathVariable Long productId, @Valid @RequestBody
public void deleteProduct(@PathVariable Long productId) {
productService.deleteProduct(productId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,4 @@ public Shop replaceShop(@PathVariable Long shopId, @Valid @RequestBody Shop modi
public void deleteShop(@PathVariable Long shopId) {
shopService.deleteShop(shopId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.List;

import static com.i8ai.training.store.util.TestUtils.*;
import static org.mockito.Mockito.when;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -20,15 +20,15 @@
@WebMvcTest(BalanceController.class)
class BalanceControllerTest {

@MockBean
BalanceService balanceService;

@Autowired
MockMvc mockMvc;

@MockBean
BalanceService balanceService;

@Test
void getNetBalance() throws Exception {
when(balanceService.getNetBalance(null, null)).thenReturn(
given(balanceService.getNetBalance(null, null)).willReturn(
Balance.builder().spent(NET_SALES_EXPENSES).income(NET_SALES_INCOME).build()
);

Expand All @@ -40,7 +40,7 @@ void getNetBalance() throws Exception {

@Test
void getBalancesPerProduct() throws Exception {
when(balanceService.getBalancesPerProduct(null, null)).thenReturn(List.of(
given(balanceService.getBalancesPerProduct(null, null)).willReturn(List.of(
Balance.builder().spent(PRODUCT_A_EXPENSES).income(PRODUCT_A_INCOME).product(PRODUCT_A).build(),
Balance.builder().spent(PRODUCT_B_EXPENSES).income(PRODUCT_B_INCOME).product(PRODUCT_B).build()
));
Expand All @@ -57,7 +57,7 @@ void getBalancesPerProduct() throws Exception {

@Test
void getBalanceByProduct() throws Exception {
when(balanceService.getBalanceByProduct(PRODUCT_A.getId(), null, null)).thenReturn(
given(balanceService.getBalanceByProduct(PRODUCT_A.getId(), null, null)).willReturn(
Balance.builder().spent(PRODUCT_A_EXPENSES).income(PRODUCT_A_INCOME).product(PRODUCT_A).build()
);

Expand All @@ -70,7 +70,7 @@ void getBalanceByProduct() throws Exception {

@Test
void getBalancesByProductPerShop() throws Exception {
when(balanceService.getBalancesByProductPerShop(PRODUCT_B.getId(), null, null)).thenReturn(List.of(
given(balanceService.getBalancesByProductPerShop(PRODUCT_B.getId(), null, null)).willReturn(List.of(
Balance.builder().spent(PACK1A_SALES_EXPENSES).income(PACK1A_SALES_INCOME).product(PRODUCT_A).shop(SHOP1).build(),
Balance.builder().spent(PACK2B_SALES_EXPENSES).income(PACK2B_SALES_INCOME).product(PRODUCT_B).shop(SHOP2).build()
));
Expand All @@ -89,7 +89,7 @@ void getBalancesByProductPerShop() throws Exception {

@Test
void getBalancesPerShop() throws Exception {
when(balanceService.getBalancesPerShop(null, null)).thenReturn(List.of(
given(balanceService.getBalancesPerShop(null, null)).willReturn(List.of(
Balance.builder().spent(SHOP1_EXPENSES).income(SHOP1_INCOME).shop(SHOP1).build(),
Balance.builder().spent(SHOP2_EXPENSES).income(SHOP2_INCOME).shop(SHOP2).build()
));
Expand All @@ -106,8 +106,8 @@ void getBalancesPerShop() throws Exception {

@Test
void getBalanceByShop() throws Exception {
when(balanceService.getBalanceByShop(SHOP1.getId(), null, null))
.thenReturn(Balance.builder().spent(SHOP1_EXPENSES).income(SHOP1_INCOME).shop(SHOP1).build());
given(balanceService.getBalanceByShop(SHOP1.getId(), null, null))
.willReturn(Balance.builder().spent(SHOP1_EXPENSES).income(SHOP1_INCOME).shop(SHOP1).build());

mockMvc.perform(get("/balance/shop/" + SHOP1.getId()))
.andExpect(status().isOk())
Expand All @@ -118,7 +118,7 @@ void getBalanceByShop() throws Exception {

@Test
void getBalancesByShopPerProduct() throws Exception {
when(balanceService.getBalancesByShopPerProduct(SHOP2.getId(), null, null)).thenReturn(
given(balanceService.getBalancesByShopPerProduct(SHOP2.getId(), null, null)).willReturn(
List.of(
Balance.builder()
.spent(PACK2A_SALES_EXPENSES)
Expand Down Expand Up @@ -149,8 +149,8 @@ void getBalancesByShopPerProduct() throws Exception {

@Test
void getBalanceByProductAndShop() throws Exception {
when(balanceService.getBalanceByProductAndShop(PRODUCT_B.getId(), SHOP2.getId(), null, null))
.thenReturn(
given(balanceService.getBalanceByProductAndShop(PRODUCT_B.getId(), SHOP2.getId(), null, null))
.willReturn(
Balance.builder()
.spent(PACK2B_SALES_EXPENSES)
.income(PACK2B_SALES_INCOME)
Expand All @@ -169,7 +169,7 @@ void getBalanceByProductAndShop() throws Exception {

@Test
void getBalancesPerProductPerShop() throws Exception {
when(balanceService.getBalancesPerProductPerShop(null, null)).thenReturn(
given(balanceService.getBalancesPerProductPerShop(null, null)).willReturn(
List.of(
Balance.builder()
.spent(PACK1A_SALES_EXPENSES)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.i8ai.training.store.api;

import com.i8ai.training.store.service.ExistenceService;
import com.i8ai.training.store.service.data.Existence;
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.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Arrays;
import java.util.List;

import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest(ExistenceController.class)
class ExistenceControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private ExistenceService existenceService;

@Test
void testGetAllProductsExistenceInMain() throws Exception {
List<Existence> existences = Arrays.asList(Existence.builder().build(), Existence.builder().build());
given(existenceService.getAllProductsExistenceInMain()).willReturn(existences);

mockMvc.perform(get("/existence"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$[0]").exists())
.andExpect(jsonPath("$[1]").exists());
}

@Test
void testGetProductExistenceInMain() throws Exception {
Existence existence = Existence.builder().build();
given(existenceService.getProductExistenceInMain(anyLong())).willReturn(existence);

mockMvc.perform(get("/existence/1"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").exists());
}

@Test
void testGetProductExistenceInAllShops() throws Exception {
List<Existence> existences = Arrays.asList(Existence.builder().build(), Existence.builder().build());
given(existenceService.getProductExistenceInAllShops(anyLong())).willReturn(existences);

mockMvc.perform(get("/existence/1/shop"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$[0]").exists())
.andExpect(jsonPath("$[1]").exists());
}

@Test
void testGetProductExistenceInShop() throws Exception {
Existence existence = Existence.builder().build();
given(existenceService.getProductExistenceInShop(anyLong(), anyLong())).willReturn(existence);

mockMvc.perform(get("/existence/1/shop/1"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").exists());
}
}
67 changes: 67 additions & 0 deletions src/test/java/com/i8ai/training/store/api/LotControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.i8ai.training.store.api;

import com.i8ai.training.store.model.Lot;
import com.i8ai.training.store.service.LotService;
import com.i8ai.training.store.util.TestUtils;
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.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Arrays;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest(LotController.class)
class LotControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private LotService lotService;

@Test
void testGetLots() throws Exception {
List<Lot> lots = Arrays.asList(Lot.builder().build(), Lot.builder().build());

given(lotService.getLots(null, null, null)).willReturn(lots);

mockMvc.perform(get("/lot"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$[0]").exists())
.andExpect(jsonPath("$[1]").exists());
}

@Test
void testRegisterLot() throws Exception {
Lot newLot = Lot.builder().build();
Lot savedLot = Lot.builder().build();
given(lotService.registerLot(any(Lot.class))).willReturn(savedLot);

mockMvc.perform(post("/lot")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtils.asJsonString(newLot)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").exists());
}

@Test
void testDeleteLot() throws Exception {
doNothing().when(lotService).deleteLot(anyLong());

mockMvc.perform(delete("/lot/1"))
.andExpect(status().isOk());
}
}
66 changes: 66 additions & 0 deletions src/test/java/com/i8ai/training/store/api/PackControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.i8ai.training.store.api;

import com.i8ai.training.store.model.Pack;
import com.i8ai.training.store.service.PackService;
import com.i8ai.training.store.util.TestUtils;
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.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Arrays;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest(PackController.class)
class PackControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private PackService packService;

@Test
void testGetPacks() throws Exception {
List<Pack> packs = Arrays.asList(Pack.builder().build(), Pack.builder().build());
given(packService.getPacks(null, null, null, null)).willReturn(packs);

mockMvc.perform(get("/pack"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$[0]").exists())
.andExpect(jsonPath("$[1]").exists());
}

@Test
void testRegisterPack() throws Exception {
Pack newPack = Pack.builder().build();
Pack savedPack = Pack.builder().build();
given(packService.registerPack(any(Pack.class))).willReturn(savedPack);

mockMvc.perform(post("/pack")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtils.asJsonString(newPack)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").exists());
}

@Test
void testDeletePack() throws Exception {
doNothing().when(packService).deletePack(anyLong());

mockMvc.perform(delete("/pack/1"))
.andExpect(status().isOk());
}
}
Loading

0 comments on commit 9bc808d

Please sign in to comment.