Fakultas Ilmu Komputer UI

Commit e6e755db authored by Fauzan Nazranda Rizqan's avatar Fauzan Nazranda Rizqan
Browse files

Merge branch 'fauzan' into 'staging'

Add some Test

See merge request !22
parents 841dccb1 db24c084
Pipeline #134135 passed with stages
in 22 minutes and 19 seconds
package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.model.User;
import id.ac.ui.cs.advprog.landiandfriends.service.UserServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.Utility;
import id.ac.ui.cs.advprog.landiandfriends.util.Utility;
import net.bytebuddy.utility.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -17,7 +16,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
......
......@@ -43,7 +43,6 @@ public class WishlistController {
@GetMapping(path= "/remove-wishlist/{id}")
public String removeWishlist(@PathVariable("id") Long id) {
wishlistService.removeWishlist(id);
System.out.println(id);
return "redirect:/wishlist";
}
}
\ No newline at end of file
......@@ -28,45 +28,25 @@ public class AddressServiceImpl {
return address;
}
public void saveAddress(AddressModel address){
RyanUserDetails userDetails = (RyanUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
var user = userRepository.findByEmail(userDetails.getEmail());
addressRepository.save(address);
addressRepository.insertUserAddress(address.getAddressId(), user.getUserId());
addressRepository.insertUserAddress(address.getAddressId(), user.getUserId());}
}
// get address with address_id
public List<AddressModel> getAddress(){
RyanUserDetails userDetails = (RyanUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
var user = userRepository.findByEmail(userDetails.getEmail());
List<Long> addressIdList = addressRepository.getAddressId(user.getUserId());
List<AddressModel> address = new ArrayList<>();
// iterate through the List and get the address
addressIdList
.forEach(addressId -> address.add(addressRepository.getAddressFromId(addressId)));
return address;
}
// delete address
return address;}
public void deleteAddress(Long id){
addressRepository.deleteUserAddress(id);
AddressModel address = addressRepository.getAddressFromId(id);
addressRepository.delete(address);
}
// get size of all address
public int getAddressSize(){
RyanUserDetails userDetails = (RyanUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
User user = userRepository.findByEmail(userDetails.getEmail());
List<Long> List = addressRepository.getAddressId(user.getUserId());
return List.size();
}
}
package id.ac.ui.cs.advprog.landiandfriends.service;
package id.ac.ui.cs.advprog.landiandfriends.util;
import javax.servlet.http.HttpServletRequest;
......
package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.exception.InvalidStockException;
import id.ac.ui.cs.advprog.landiandfriends.forms.CreateAddressForm;
import id.ac.ui.cs.advprog.landiandfriends.forms.CreateBookForm;
import id.ac.ui.cs.advprog.landiandfriends.forms.EditProfileForm;
import id.ac.ui.cs.advprog.landiandfriends.model.AddressModel;
import id.ac.ui.cs.advprog.landiandfriends.model.User;
import id.ac.ui.cs.advprog.landiandfriends.repository.AddressRepository;
import id.ac.ui.cs.advprog.landiandfriends.repository.UserRepository;
import id.ac.ui.cs.advprog.landiandfriends.service.AddressServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.UserServiceImpl;
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.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
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.*;
@WebMvcTest(controllers = ProfileController.class)
class ProfileControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserServiceImpl userService;
@MockBean
private AddressServiceImpl addressService;
@MockBean
private AddressRepository addressRepository;
@MockBean
private UserRepository userRepository;
@Test
void addressList() throws Exception{
User user = new User();
user.setEmail("test@gmail.com");
user.setUsername("fauzan");
user.setPassword("12345");
AddressModel addressModel = new AddressModel();
addressModel.setCity("Jakarta");
addressModel.setDescription("Jalan Tebet Timur");
addressModel.setPostalCode("12345");
addressModel.setUserModel(user);
mockMvc.perform(get("/delivery-address"))
.andExpect(status().is(200));
}
@Test
void createAddress() throws Exception {
mockMvc.perform(get("/create-address"))
.andExpect(status().isOk())
.andExpect(handler().methodName("createAddress"))
.andExpect(view().name("address-create"));
}
@Test
void addAddressPostShouldReturnStatus200() throws Exception {
AddressModel addressModel = new AddressModel();
addressModel.setDescription("Jalan Tebet Timur");
addressModel.setCity("Jakarta");
addressModel.setPostalCode("12345");
CreateAddressForm form = new CreateAddressForm();
when(addressService.createAddress(form.getDescription(),
form.getCity(), form.getPostalCode())).thenReturn(addressModel);
String body = "{\"description\": \"Jalan Tebet Timur\", \"city\": \"Jakarta\", \"postalCode\": \"12345\"}";
MvcResult result = mockMvc.perform(post("/create-address")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(body))
.andExpect(handler().methodName("addAddress"))
.andReturn();
assertEquals(200, result.getResponse().getStatus());
}
@Test
void updateAddressShouldShowTheCorrectForm() throws Exception {
when(addressRepository.getAddressFromId(1L)).thenReturn(new AddressModel());
mockMvc.perform(get("/update-address/1"))
.andExpect(status().isOk())
.andExpect(handler().methodName("updateAddress"))
.andExpect(view().name("address-update"));
}
@Test
void editAddressShouldCallsUpdateAddressWithTheCorrectArgument() throws Exception{
String body = "{\"description\": \"Jalan Tebet Timur\", \"city\": \"Jakarta\", \"postalCode\": \"12345\"}";
mockMvc.perform(post("/update-address/1")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(body))
.andExpect(handler().methodName("editAddress"))
.andReturn();
verify(addressRepository).updateAddress("Jalan Tebet Timur", "Jakarta", "12345", 1L);
}
@Test
@WithMockUser(authorities = "ADMIN")
void GetProfileShouldDisplayTheCorrectView() throws Exception {
mockMvc.perform(get("/profile"))
.andExpect(status().isOk())
.andExpect(handler().methodName("profile"))
.andExpect(view().name("profile"));
}
@Test
@WithMockUser(authorities = "ADMIN")
void deleteAddressShouldCallTheDeleteAddressMethod() throws Exception {
mockMvc.perform(get("/delete-address/1"))
.andExpect(handler().methodName("deleteAddress")).andReturn();
verify(addressService).deleteAddress(1L);
}
@Test
void editProfile() throws Exception {
mockMvc.perform(get("/edit-profile"))
.andExpect(status().isOk())
.andExpect(handler().methodName("editProfile"))
.andExpect(view().name("profile-edit"));
}
@Test
void testEditProfileShouldCallUpdateInfoAndPostTheCorrectInput() throws Exception {
String body = "{\"username\": \"Fauzan\"}";
mockMvc.perform(post("/edit-profile")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(body))
.andExpect(handler().methodName("editProfile"))
.andReturn();
verify(userService).updateInfo("Fauzan");
}
}
\ No newline at end of file
package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.model.Book;
import id.ac.ui.cs.advprog.landiandfriends.model.Wishlist;
import id.ac.ui.cs.advprog.landiandfriends.repository.UserRepository;
import id.ac.ui.cs.advprog.landiandfriends.service.BookServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.WishlistServiceImpl;
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.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(controllers = WishlistController.class)
class WishlistControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private BookServiceImpl bookService;
@MockBean
private WishlistServiceImpl wishlistService;
@MockBean
private UserRepository userRepository;
@Test
void getWishlistShouldDisplayTheCorrectView() throws Exception{
Wishlist wishlist = new Wishlist();
when(wishlistService.getUserWishlist()).thenReturn(wishlist);
mockMvc.perform(get("/wishlist"))
.andExpect(status().isOk())
.andExpect(handler().methodName("getWishlist"))
.andExpect(view().name("wishlist"));
}
@Test
void addWishlistShouldFound() throws Exception {
Book book = new Book();
when(bookService.getBookById("1")).thenReturn(book);
mockMvc.perform(get("/add-wishlist/1"))
.andExpect(status().isFound())
.andExpect(handler().methodName("addWishlist"));
}
@Test
void removeWishlistShouldCallTheRemoveMethod() throws Exception {
mockMvc.perform(get("/remove-wishlist/1"))
.andExpect(status().isFound())
.andExpect(handler().methodName("removeWishlist"));
verify(wishlistService).removeWishlist(1L);
}
}
\ No newline at end of file
package id.ac.ui.cs.advprog.landiandfriends.forms;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class EditProfileFormTest {
private EditProfileForm form;
@BeforeEach
void setUp() {
form = new EditProfileForm();
form.setUsername("username");
}
@Test
void getUsername() {
assertEquals("username", form.getUsername());
}
@Test
void setUsername() {
form.setUsername("newUsername");
assertEquals("newUsername", form.getUsername());
}
}
\ No newline at end of file
package id.ac.ui.cs.advprog.landiandfriends.service;
import id.ac.ui.cs.advprog.landiandfriends.model.AddressModel;
import id.ac.ui.cs.advprog.landiandfriends.model.User;
import id.ac.ui.cs.advprog.landiandfriends.repository.AddressRepository;
import id.ac.ui.cs.advprog.landiandfriends.repository.UserRepository;
import id.ac.ui.cs.advprog.landiandfriends.security.RyanUserDetails;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;
import static org.springframework.security.config.http.MatcherType.mvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
@ExtendWith(MockitoExtension.class)
class AddressServiceImplTest {
@Autowired
private MockMvc mockMvc;
@Mock
SecurityContext mockSecurityContext;
@Mock
private AddressRepository addressRepository;
@Mock
private UserRepository userRepository;
@InjectMocks
private AddressServiceImpl addressService;
@Test
void createAddress() {
addressService.createAddress("description","city","postal");
assertEquals(addressService.createAddress("description","city","postal").getDescription(),("description"));
}
@Test
void saveAddressShouldCallTheSaveMethod() throws Exception {
User user = new User();
user.setUsername("ryan");
user.setUserId(1L);
user.setPassword("password");
user.setEmail("testmail.com");
RyanUserDetails userDetails = new RyanUserDetails(user);
Authentication authentication = Mockito.mock(Authentication.class);
SecurityContext securityContext = Mockito.mock(SecurityContext.class);
Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
SecurityContextHolder.setContext(securityContext);
when(authentication.getPrincipal()).thenReturn(userDetails);
when(userRepository.findByEmail(userDetails.getEmail())).thenReturn(user);
AddressModel addressModel = new AddressModel();
addressModel.setCity("Jakarta");
addressModel.setDescription("Jalan Tebet Timur");
addressModel.setPostalCode("12345");
addressService.saveAddress(addressModel);
verify(addressRepository, times(1)).save(addressModel);
}
@Test
void getAddressPlainUserShouldReturnAnEmptyList() {
User user = new User();
user.setUsername("ryan");
user.setUserId(1L);
user.setPassword("password");
user.setEmail("testmail.com");
RyanUserDetails userDetails = new RyanUserDetails(user);
Authentication authentication = Mockito.mock(Authentication.class);
SecurityContext securityContext = Mockito.mock(SecurityContext.class);
Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
SecurityContextHolder.setContext(securityContext);
when(authentication.getPrincipal()).thenReturn(userDetails);
when(userRepository.findByEmail(userDetails.getEmail())).thenReturn(user);
assertEquals(addressService.getAddress().size(),0);
}
@Test
void deleteMethodShouldBeCalled() {
AddressModel addressModel = new AddressModel();
addressModel.setAddressId(1L);
when(addressRepository.getAddressFromId(1L)).thenReturn(addressModel);
addressService.deleteAddress(addressModel.getAddressId());
verify(addressRepository, times(1)).delete(addressModel);
}
}
\ No newline at end of file
......@@ -4,13 +4,20 @@ import id.ac.ui.cs.advprog.landiandfriends.forms.AddPaymentForm;
import id.ac.ui.cs.advprog.landiandfriends.forms.CreateBookForm;
import id.ac.ui.cs.advprog.landiandfriends.model.Book;
import id.ac.ui.cs.advprog.landiandfriends.model.PaymentModel;
import id.ac.ui.cs.advprog.landiandfriends.model.User;
import id.ac.ui.cs.advprog.landiandfriends.repository.ArticleRepository;
import id.ac.ui.cs.advprog.landiandfriends.repository.PaymentRepository;
import id.ac.ui.cs.advprog.landiandfriends.repository.UserRepository;
import id.ac.ui.cs.advprog.landiandfriends.security.RyanUserDetails;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import java.util.ArrayList;
import java.util.List;
......@@ -26,6 +33,8 @@ class PaymentServiceImplTest {
private PaymentServiceImpl paymentService;
@Mock
private PaymentRepository paymentRepository;
@Mock
private UserRepository userRepository;
@Test
......@@ -48,15 +57,48 @@ class PaymentServiceImplTest {
}
@Test
void getUserPayments() {
void getUserPaymentsFromPlainUserShouldReturnEmptyList() {
User user = new User();
user.setUsername("ryan");
user.setUserId(1L);
user.setPassword("password");
user.setEmail("testmail.com");
RyanUserDetails userDetails = new RyanUserDetails(user);
Authentication authentication = Mockito.mock(Authentication.class);
SecurityContext securityContext = Mockito.mock(SecurityContext.class);
Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
SecurityContextHolder.setContext(securityContext);
when(authentication.getPrincipal()).thenReturn(userDetails);
when(userRepository.findByUsername(userDetails.getUsername())).thenReturn(user);
assertEquals(paymentService.getUserPayments().size(),0);
}
@Test
void savePayment() {
void savePaymentShouldCallTheCorrectMethods() {
User user = new User();
user.setEmail("testmail.com");
PaymentModel payment = new PaymentModel();
RyanUserDetails userDetails = new RyanUserDetails(user);
Authentication authentication = Mockito.mock(Authentication.class);
SecurityContext securityContext = Mockito.mock(SecurityContext.class);
Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
SecurityContextHolder.setContext(securityContext);
when(authentication.getPrincipal()).thenReturn(userDetails);
when(userRepository.findByEmail(userDetails.getEmail())).thenReturn(user);
paymentService.savePayment(payment);
verify(paymentRepository).save(payment);
verify(paymentRepository).insertUserPayment(payment.getPaymentId(), user.getUserId());
}
@Test
void deletePayment() {
void deletePaymentShouldCallTheCorrectMethods() {
paymentService.deletePayment(1);
verify(paymentRepository).deleteUserPayment(1);
verify(paymentRepository).deleteById(1);
}
}
\ No newline at end of file
package id.ac.ui.cs.advprog.landiandfriends.service;
import id.ac.ui.cs.advprog.landiandfriends.model.Book;
import id.ac.ui.cs.advprog.landiandfriends.model.User;
import id.ac.ui.cs.advprog.landiandfriends.model.Wishlist;
import id.ac.ui.cs.advprog.landiandfriends.repository.UserRepository;
import id.ac.ui.cs.advprog.landiandfriends.repository.WishlistRepository;
import id.ac.ui.cs.advprog.landiandfriends.security.RyanUserDetails;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;