Fakultas Ilmu Komputer UI

Commit 16ed8750 authored by MUHAMMAD AAQIL ABDULLAH's avatar MUHAMMAD AAQIL ABDULLAH
Browse files

Fix conflicts

parents af749c32 df25ec64
Pipeline #134158 passed with stages
in 22 minutes and 43 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
package id.ac.ui.cs.advprog.landiandfriends.forms;
import lombok.Data;
import lombok.Generated;
@Generated
@Data
public class AddPaymentForm {
private String name;
......
package id.ac.ui.cs.advprog.landiandfriends.forms;
import lombok.Data;
import lombok.Generated;
@Generated
@Data
public class CreateAddressForm {
......
package id.ac.ui.cs.advprog.landiandfriends.forms;
import lombok.Data;
import lombok.Generated;
@Generated
@Data
public class CreateArticleForm {
private String title;
......
......@@ -6,7 +6,9 @@ import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import java.util.List;
import lombok.Generated;
@Generated
@Data
@NoArgsConstructor
@AllArgsConstructor
......
package id.ac.ui.cs.advprog.landiandfriends.forms;
import lombok.Data;
import lombok.Generated;
@Generated
@Data
public class EditProfileForm {
......
package id.ac.ui.cs.advprog.landiandfriends.forms;
import lombok.Data;
import lombok.Generated;
@Generated
@Data
public class RegisterAdmin{
private String email;
......
package id.ac.ui.cs.advprog.landiandfriends.forms;
import lombok.Data;
import lombok.Generated;
@Generated
@Data
public class RegisterForm {
private String email;
......
......@@ -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();
}
}
......@@ -62,10 +62,12 @@ public class ArticleServiceImpl implements ArticleService {
}
}
public void deleteArticle(Integer id){
public void deleteArticle(Integer id) {
Optional<Articles> art = repo.findById(id);
Articles objArt = art.get();
repo.delete(objArt);
if (art.isPresent()) {
Articles objArt = art.get();
repo.delete(objArt);
}
}
@Override
......
......@@ -16,6 +16,8 @@ import org.springframework.web.multipart.MultipartFile;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
......@@ -115,7 +117,7 @@ public class BookServiceImpl implements BookService {
}
public List<String> getSearchedBookTitles(String title){
return bookRepository.findAll().stream().filter(e -> e.getTitle().equals(title)).map(Book::getTitle).toList();
return bookRepository.findAll().stream().filter(e -> e.getTitle().equals(title)).map(Book::getTitle).collect(Collectors.toList());
}
......
package id.ac.ui.cs.advprog.landiandfriends.service;
package id.ac.ui.cs.advprog.landiandfriends.util;
import javax.servlet.http.HttpServletRequest;
......
......@@ -92,7 +92,7 @@
</thead>
<tbody th:each="user : ${all_users}" th:remove="tag">
<div th:if="${#strings.toString(user.roles.getName())} == 'USER'">
<td th:text="${user.user_id}">...</td>
<td th:text="${user.userId}">...</td>
<td th:text="${user.email}">...</td>
<td>
<div th:switch="${user.active}">
......
......@@ -8,6 +8,6 @@
<h1>You Have Successfully Banned
<span th:text="${username}" th:remove="tag">Success</span>
</h1>
<button type="button" th:onclick="|window.location.href='/admin/list-user'|">Go Back To User <List></List></button>
<button type="button" th:onclick="|window.location.href='/admin/list-user'|">Go Back To User List <List></List></button>
</body>
</html>
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.User;
import id.ac.ui.cs.advprog.landiandfriends.repository.UserRepository;
import id.ac.ui.cs.advprog.landiandfriends.service.PaymentServiceImpl;
......@@ -11,18 +10,21 @@ 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 org.springframework.test.web.servlet.MvcResult;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
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.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
@WebMvcTest(controllers = AdminController.class)
public class AdminControllerTest {
class AdminControllerTest {
@Autowired
private MockMvc mockMvc;
......@@ -45,18 +47,44 @@ public class AdminControllerTest {
@Test
@WithMockUser(authorities={"ADMIN"})
void whenOpenUserListShouldCallUserService() throws Exception {
List<User> users = new ArrayList<>();
when(userService.allUsers()).thenReturn(users);
void allUser() throws Exception {
List<User> userList = new ArrayList<>();
when(userService.allUsers()).thenReturn(userList);
mockMvc.perform(get("/admin/list-user"))
.andExpect(status().isOk())
.andExpect(handler().methodName("allUser"))
.andExpect(model().attributeExists( "username", "all_users"))
.andExpect(model().attribute("all_users", users))
.andExpect(model().attribute("all_users", userList))
.andExpect(view().name("listuser"));
verify(userService, times(1)).allUsers();
}
}
@Test
@WithMockUser(authorities={"ADMIN"})
void whenPostBanUserValid() throws Exception {
User user = new User();
UserServiceImpl userService = mock(UserServiceImpl.class);
user.setUsername("Fauzan");
lenient().doNothing().when(userService).banUser("Fauzan");
MvcResult result = mockMvc.perform(
post("/admin/ban/Fauzan")
.with(csrf()))
.andExpect(handler().methodName("updateUser"))
.andReturn();
assertEquals(302, result.getResponse().getStatus());
}
@Test
@WithMockUser(authorities={"ADMIN"})
void whenSuccessValid() throws Exception {
mockMvc.perform(get("/admin/success"))
.andExpect(status().isOk())
.andExpect(handler().methodName("success"))
.andExpect(view().name("successBan")).andExpect(status().is(200));
}
}
\ No newline at end of file
......@@ -256,5 +256,16 @@ class CuratorControllerTest {
.andExpect(view().name("article-create"));
}
@Test
@WithMockUser(authorities={"CURATOR"})
void whenPostRemoveArticleValid() throws Exception {
MvcResult result = mockMvc.perform(post("/curator/delete/5")
.with(csrf()))
.andExpect(handler().methodName("deleteArticle"))
.andReturn();
assertEquals(302, result.getResponse().getStatus());
}
}
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() {