use of org.rembx.jeeshop.rest.WebApplicationException in project jeeshop by remibantos.
the class SKUs method delete.
@DELETE
@Transactional
@Path("/{skuId}")
@RolesAllowed({ ADMIN, STORE_ADMIN })
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public void delete(@Context SecurityContext securityContext, @PathParam("skuId") Long skuId) {
SKU sku = entityManager.find(SKU.class, skuId);
checkNotNull(sku);
if (isAdminUser(securityContext) || isOwner(securityContext, sku.getOwner())) {
List<Product> productHolders = catalogItemFinder.findForeignHolder(QProduct.product, QProduct.product.childSKUs, sku);
for (Product product : productHolders) {
product.getChildSKUs().remove(sku);
}
List<Discount> discountHolders = catalogItemFinder.findForeignHolder(QDiscount.discount, QDiscount.discount.skus, sku);
for (Discount discount : discountHolders) {
sku.getDiscounts().remove(discount);
discount.getSkus().remove(sku);
}
entityManager.remove(sku);
} else
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
use of org.rembx.jeeshop.rest.WebApplicationException in project jeeshop by remibantos.
the class OrdersCT method find_whenClientHasUserRoleAndOrderBelongsToAnotherUser_ShouldThrowException.
@Test
public void find_whenClientHasUserRoleAndOrderBelongsToAnotherUser_ShouldThrowException() throws Exception {
entityManager.getTransaction().begin();
User user = new User("777@test.com", "test", "M.", "John", "Doe", "+33616161616", null, null, "fr_FR", null);
entityManager.persist(user);
entityManager.getTransaction().commit();
when(sessionContextMock.isUserInRole(JeeshopRoles.USER)).thenReturn(true);
when(sessionContextMock.isUserInRole(JeeshopRoles.ADMIN)).thenReturn(false);
when(sessionContextMock.getUserPrincipal()).thenReturn(new BasicUserPrincipal("777@test.com"));
try {
service.find(sessionContextMock, 1L, null);
fail("should have thrown ex");
} catch (WebApplicationException e) {
assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.UNAUTHORIZED);
} finally {
entityManager.getTransaction().begin();
entityManager.remove(user);
entityManager.persist(user);
}
}
use of org.rembx.jeeshop.rest.WebApplicationException in project jeeshop by remibantos.
the class OrdersCT method create_shouldThrowBadRequestWhenParametersHaveId.
@Test
public void create_shouldThrowBadRequestWhenParametersHaveId() throws Exception {
Address address = new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA");
address.setId(777L);
OrderItem orderItemWithId = new OrderItem();
orderItemWithId.setId(777L);
Set<OrderItem> orderItems = Collections.singleton(orderItemWithId);
try {
Order order = new Order(null, address, new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA"));
service.create(sessionContextMock, order, null);
fail("should have thrown ex");
} catch (WebApplicationException e) {
assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
}
try {
Order order = new Order(null, new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA"), address);
service.create(sessionContextMock, order, null);
fail("should have thrown ex");
} catch (WebApplicationException e) {
assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
}
try {
Order order = new Order(orderItems, new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA"), address);
service.create(sessionContextMock, order, null);
fail("should have thrown ex");
} catch (WebApplicationException e) {
assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
}
}
use of org.rembx.jeeshop.rest.WebApplicationException in project jeeshop by remibantos.
the class UsersCT method create_shouldThrowBadRequestExWhenUserHasAnId.
@Test
public void create_shouldThrowBadRequestExWhenUserHasAnId() throws Exception {
User user = new User();
user.setId(777L);
try {
service.create(sessionContextMock, user);
fail("should have thrown ex");
} catch (WebApplicationException e) {
assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
}
}
use of org.rembx.jeeshop.rest.WebApplicationException in project jeeshop by remibantos.
the class UsersCT method resetPassword_shouldReturnUnauthorizedResponse_whenAuthenticatedUserDoesNotMatchLogin.
@Test
public void resetPassword_shouldReturnUnauthorizedResponse_whenAuthenticatedUserDoesNotMatchLogin() throws Exception {
try {
when(sessionContextMock.isUserInRole(JeeshopRoles.USER)).thenReturn(true);
when(sessionContextMock.getUserPrincipal()).thenReturn(new BasicUserPrincipal(testUser.firstUser().getLogin()));
service.resetPassword(sessionContextMock, "not_matching_login", null, null);
fail("should have thrown ex");
} catch (WebApplicationException e) {
assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.UNAUTHORIZED);
}
}
Aggregations