Skip to content

Commit

Permalink
Add test for webhook to reach 80% coverage (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
hnd14 authored Sep 19, 2024
1 parent df461f4 commit cd8dd4e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ public void test_deleteWebhook_shouldSuccess() {
.log().ifValidationFails();
}

@Test
public void test_deleteWebhook_shouldFailedWhenNotFindId() {
given(getRequestSpecification())
.auth().oauth2(getAccessToken("admin", "admin"))
.contentType(ContentType.JSON)
.pathParam("id", 10)
.when()
.delete(WEBHOOK_URL + "/{id}")
.then()
.statusCode(HttpStatus.NOT_FOUND.value())
.log().ifValidationFails();
}

@Test
public void test_getWebhook_shouldReturnSuccessfully() {
given(getRequestSpecification())
Expand All @@ -120,6 +133,19 @@ public void test_getWebhook_shouldReturnSuccessfully() {
.log().ifValidationFails();
}

@Test
public void test_getWebhook_shouldReturn404_whenWebhookNotFound() {
given(getRequestSpecification())
.auth().oauth2(getAccessToken("admin", "admin"))
.contentType(ContentType.JSON)
.pathParam("id", 10)
.when()
.get(WEBHOOK_URL + "/{id}")
.then()
.statusCode(HttpStatus.NOT_FOUND.value())
.log().ifValidationFails();
}

@Test
public void test_listWebhooks_shouldReturnSuccessfully() {
given(getRequestSpecification())
Expand All @@ -143,7 +169,7 @@ public void test_getPageableWebhooks_shouldReturnSuccessfully() {
.statusCode(HttpStatus.OK.value())
.body("pageNo", equalTo(0))
.body("pageSize", equalTo(10))
.body("totalElements", equalTo(4))
.body("totalElements", equalTo(5))
.body("totalPages", equalTo(1))
.log().ifValidationFails();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yas.webhook.service;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -62,4 +63,66 @@ void test_onOrderEvent_shouldNotException() {

verify(webhookEventNotificationRepository).save(any(WebhookEventNotification.class));
}

@Test
void test_onOrderEvent_shouldNotExceptionForUpdateOrderStatus() {
ObjectMapper objectMapper = new ObjectMapper();

ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put("op", "u");
ObjectNode before = objectMapper.createObjectNode();
before.put("order_status","NEW");
objectNode.set("before", before);
ObjectNode after = objectMapper.createObjectNode();
after.put("order_status","PAID");
objectNode.set("after", after);

Event event = new Event();
WebhookEvent webhookEvent = new WebhookEvent();
Webhook webhook = new Webhook();
List<WebhookEvent> webhookEvents = List.of(webhookEvent);

event.setWebhookEvents(webhookEvents);
webhookEvent.setWebhook(webhook);

WebhookEventNotification notification = new WebhookEventNotification();
notification.setWebhookEventId(1L);

when(eventRepository.findByName(EventName.ON_ORDER_STATUS_UPDATED)).thenReturn(Optional.of(event));
when(webhookEventNotificationRepository.save(any(WebhookEventNotification.class))).thenReturn(notification);

orderEventService.onOrderEvent(objectNode);

verify(webhookEventNotificationRepository).save(any(WebhookEventNotification.class));
}

@Test
void test_onOrderEvent_shouldNotNotifyWhenOpIsUnknown() {
ObjectMapper objectMapper = new ObjectMapper();

ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put("op", "k");

orderEventService.onOrderEvent(objectNode);

verify(webhookEventNotificationRepository, times(0)).save(any(WebhookEventNotification.class));
}

@Test
void test_onOrderEvent_shouldNotNotifyWhenOpIsUpdateButOrderStatusNotChange() {
ObjectMapper objectMapper = new ObjectMapper();

ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put("op", "u");
ObjectNode before = objectMapper.createObjectNode();
before.put("order_status","NEW");
objectNode.set("before", before);
ObjectNode after = objectMapper.createObjectNode();
after.put("order_status","NEW");
objectNode.set("after", after);

orderEventService.onOrderEvent(objectNode);

verify(webhookEventNotificationRepository, times(0)).save(any(WebhookEventNotification.class));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yas.webhook.service;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -61,4 +62,17 @@ void test_onProductEvent_shouldNotException() {
verify(webhookEventNotificationRepository).save(any(WebhookEventNotification.class));
verify(webhookService).notifyToWebhook(any(WebhookEventNotificationDto.class));
}

@Test
void test_onProductEvent_shouldNotDoAnythingWhenOpUnknown() {
ObjectMapper objectMapper = new ObjectMapper();

ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put("op", "k");

productEventService.onProductEvent(objectNode);

verify(webhookEventNotificationRepository, times(0)).save(any(WebhookEventNotification.class));
verify(webhookService, times(0)).notifyToWebhook(any(WebhookEventNotificationDto.class));
}
}

0 comments on commit cd8dd4e

Please sign in to comment.