update api - enums, errors

This commit is contained in:
ggmaleva 2021-09-13 13:43:26 +03:00
parent 8efa118e1c
commit 8f084ce6cf
No known key found for this signature in database
GPG Key ID: 0E412B78565B108F
8 changed files with 197 additions and 41 deletions

View File

@ -38,7 +38,7 @@
<fraudo.version>0.0.17</fraudo.version>
<geck.version>0.6.11</geck.version>
<db.common.lib.version>0.0.3</db.common.lib.version>
<swag-fraudbusters-management.version>1.85-0ac0610-server</swag-fraudbusters-management.version>
<swag-fraudbusters-management.version>1.87-3171c60-server</swag-fraudbusters-management.version>
<fraudbusters.notificator.proto.version>1.9-742bc9d</fraudbusters.notificator.proto.version>
</properties>

View File

@ -4,6 +4,7 @@ import com.rbkmoney.dao.DaoException;
import com.rbkmoney.fraudbusters.management.domain.response.ErrorResponse;
import com.rbkmoney.fraudbusters.management.exception.KafkaProduceException;
import com.rbkmoney.fraudbusters.management.exception.KafkaSerializationException;
import com.rbkmoney.fraudbusters.management.exception.NotificatorCallException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
@ -20,6 +21,7 @@ public class ErrorController {
public static final String INVALID_PARAMETERS = "invalidParameters";
public static final String DATA_BASE_INVOCATION_EXCEPTION = "dataBaseInvocationException";
public static final String KAFKA_PRODUCE_ERROR = "kafkaProduceError";
public static final String NOTIFICATOR_CALL_EXCEPTION = "notificatorCallException";
@ExceptionHandler(HttpClientErrorException.Unauthorized.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ -72,4 +74,15 @@ public class ErrorController {
.build();
}
@ExceptionHandler(NotificatorCallException.class)
@ResponseStatus(HttpStatus.BAD_GATEWAY)
@ResponseBody
public ErrorResponse handleBadRequest(NotificatorCallException e) {
log.error("NotificatorCallException exception e: ", e);
return ErrorResponse.builder()
.code(NOTIFICATOR_CALL_EXCEPTION)
.message(e.getMessage())
.build();
}
}

View File

@ -0,0 +1,7 @@
package com.rbkmoney.fraudbusters.management.exception;
public class NotificatorCallException extends RuntimeException {
public NotificatorCallException(String s) {
super(s);
}
}

View File

@ -120,9 +120,9 @@ public class NotificationResource implements NotificationsApi {
@Override
@PreAuthorize("hasAnyRole('fraud-officer')")
public ResponseEntity<Void> updateNotificationStatus(Long id,
@Valid NotificationStatus notificationStatus) {
@Valid String notificationStatus) {
var status = com.rbkmoney.damsel.fraudbusters_notificator.NotificationStatus
.valueOf(notificationStatus.getStatus().getValue());
.valueOf(notificationStatus);
notificationService.updateStatus(id, status);
log.info("NotificationResource update notification status: {}", notificationStatus);
return ResponseEntity.noContent().build();
@ -133,7 +133,7 @@ public class NotificationResource implements NotificationsApi {
public ResponseEntity<ChannelTypeListResponse> getChannelTypes() {
var channelTypeListResponse = channelService.getAllTypes();
List<ChannelType> channelTypes = channelTypeListResponse.getChannelTypes().stream()
.map(value -> new ChannelType().type(ChannelType.TypeEnum.fromValue(value)))
.map(ChannelType::fromValue)
.collect(Collectors.toList());
ChannelTypeListResponse response = new ChannelTypeListResponse();
response.setResult(channelTypes);

View File

@ -1,14 +1,13 @@
package com.rbkmoney.fraudbusters.management.service;
import com.rbkmoney.damsel.fraudbusters_notificator.*;
import com.rbkmoney.fraudbusters.management.exception.NotificatorCallException;
import com.rbkmoney.fraudbusters.management.service.iface.ChannelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import org.springframework.stereotype.Service;
import java.util.Collections;
@Slf4j
@Service
@RequiredArgsConstructor
@ -21,8 +20,8 @@ public class ChannelServiceImpl implements ChannelService {
try {
return channelClient.create(channel);
} catch (TException e) {
log.error("Error call ChannelService create ", e);
return null;
log.error("Error call notificator create channel ", e);
throw new NotificatorCallException("Error call notificator create channel");
}
}
@ -31,7 +30,8 @@ public class ChannelServiceImpl implements ChannelService {
try {
channelClient.remove(name);
} catch (TException e) {
log.error("Error call ChannelService remove ", e);
log.error("Error call notificator remove channel ", e);
throw new NotificatorCallException("Error call notificator remove channel");
}
}
@ -41,9 +41,8 @@ public class ChannelServiceImpl implements ChannelService {
try {
return channelClient.getAll(page, filter);
} catch (TException e) {
log.error("Error call ChannelService getAll ", e);
return new ChannelListResponse()
.setChannels(Collections.emptyList());
log.error("Error call notificator getAll channels ", e);
throw new NotificatorCallException("Error call notificator getAll channels");
}
}
@ -52,9 +51,8 @@ public class ChannelServiceImpl implements ChannelService {
try {
return channelClient.getAllTypes();
} catch (TException e) {
log.error("Error call ChannelService getAllTypes ", e);
return new ChannelTypeListResponse()
.setChannelTypes(Collections.emptyList());
log.error("Error call notificator getAll channel types ", e);
throw new NotificatorCallException("Error call notificator getAll channel types");
}
}
}

View File

@ -1,14 +1,13 @@
package com.rbkmoney.fraudbusters.management.service;
import com.rbkmoney.damsel.fraudbusters_notificator.*;
import com.rbkmoney.fraudbusters.management.exception.NotificatorCallException;
import com.rbkmoney.fraudbusters.management.service.iface.NotificationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import org.springframework.stereotype.Service;
import java.util.Collections;
@Slf4j
@Service
@RequiredArgsConstructor
@ -21,8 +20,8 @@ public class NotificationServiceImpl implements NotificationService {
try {
return notificationClient.create(notification);
} catch (TException e) {
log.error("Error call NotificationService create ", e);
return null;
log.error("Error call notificator create notification ", e);
throw new NotificatorCallException("Error call notificator create notification");
}
}
@ -31,7 +30,8 @@ public class NotificationServiceImpl implements NotificationService {
try {
notificationClient.remove(id);
} catch (TException e) {
log.error("Error call NotificationService remove ", e);
log.error("Error call notificator remove notification ", e);
throw new NotificatorCallException("Error call notificator remove notification");
}
}
@ -41,7 +41,8 @@ public class NotificationServiceImpl implements NotificationService {
try {
notificationClient.updateStatus(id, status);
} catch (TException e) {
log.error("Error call NotificationService updateStatus ", e);
log.error("Error call notificator update notification status ", e);
throw new NotificatorCallException("Error call notificator update notification status");
}
}
@ -50,8 +51,8 @@ public class NotificationServiceImpl implements NotificationService {
try {
return notificationClient.validate(notification);
} catch (TException e) {
log.error("Error call NotificationService validate ", e);
return null;
log.error("Error call notificator validate notification ", e);
throw new NotificatorCallException("Error call notificator validate notification");
}
}
@ -60,9 +61,8 @@ public class NotificationServiceImpl implements NotificationService {
try {
return notificationClient.getAll(page, filter);
} catch (TException e) {
log.error("Error call NotificationService getAll ", e);
return new NotificationListResponse()
.setNotifications(Collections.emptyList());
log.error("Error call notificator getAll notifications ", e);
throw new NotificatorCallException("Error call notificator getAll notifications");
}
}
}

View File

@ -2,14 +2,13 @@ package com.rbkmoney.fraudbusters.management.service;
import com.rbkmoney.damsel.fraudbusters_notificator.NotificationTemplateListResponse;
import com.rbkmoney.damsel.fraudbusters_notificator.NotificationTemplateServiceSrv;
import com.rbkmoney.fraudbusters.management.exception.NotificatorCallException;
import com.rbkmoney.fraudbusters.management.service.iface.NotificationTemplateService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import org.springframework.stereotype.Service;
import java.util.Collections;
@Slf4j
@Service
@RequiredArgsConstructor
@ -22,9 +21,8 @@ public class NotificationTemplateServiceImpl implements NotificationTemplateServ
try {
return notificationTemplateClient.getAll();
} catch (TException e) {
log.error("Error call NotificationTemplateService getAll ", e);
return new NotificationTemplateListResponse()
.setNotificationTemplates(Collections.emptyList());
log.error("Error call notificator getAll templates ", e);
throw new NotificatorCallException("Error call notificator getAll templates");
}
}
}

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.rbkmoney.damsel.fraudbusters_notificator.*;
import com.rbkmoney.fraudbusters.management.TestObjectFactory;
import com.rbkmoney.fraudbusters.management.controller.ErrorController;
import com.rbkmoney.fraudbusters.management.resource.notificator.converter.ChannelConverter;
import com.rbkmoney.fraudbusters.management.resource.notificator.converter.NotificationConverter;
import com.rbkmoney.fraudbusters.management.resource.notificator.converter.NotificationTemplateConverter;
@ -15,6 +16,7 @@ import com.rbkmoney.swag.fraudbusters.management.model.Channel;
import com.rbkmoney.swag.fraudbusters.management.model.Notification;
import com.rbkmoney.swag.fraudbusters.management.model.ValidationError;
import com.rbkmoney.swag.fraudbusters.management.model.ValidationResponse;
import org.apache.thrift.TException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -29,7 +31,9 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.List;
import java.util.stream.Collectors;
import static com.rbkmoney.fraudbusters.management.controller.ErrorController.NOTIFICATOR_CALL_EXCEPTION;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ -63,7 +67,21 @@ class NotificationResourceTest {
var notificationResource =
new NotificationResource(notificationService, notificationConverter, notificationTemplateService,
notificationTemplateConverter, channelService, channelConverter, validationConverter);
this.mockMvc = MockMvcBuilders.standaloneSetup(notificationResource).build();
this.mockMvc = MockMvcBuilders.standaloneSetup(notificationResource, new ErrorController()).build();
}
@Test
void createChannelWithErrorCall() throws Exception {
Channel channel = TestObjectFactory.testChannel();
when(channelClient.create(any(com.rbkmoney.damsel.fraudbusters_notificator.Channel.class)))
.thenThrow(new TException());
mockMvc.perform(post("/notifications/channels")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(channel)))
.andExpect(status().isBadGateway())
.andExpect(jsonPath("$.code", is(NOTIFICATOR_CALL_EXCEPTION)))
.andExpect(jsonPath("$.message", is("Error call notificator create channel")));
}
@Test
@ -87,6 +105,20 @@ class NotificationResourceTest {
verify(channelClient, times(1)).create(any(com.rbkmoney.damsel.fraudbusters_notificator.Channel.class));
}
@Test
void createOrUpdateNotificationWithErrorCall() throws Exception {
Notification notification = TestObjectFactory.testNotification();
when(notificationClient.create(any(com.rbkmoney.damsel.fraudbusters_notificator.Notification.class)))
.thenThrow(new TException());
mockMvc.perform(post("/notifications")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(notification)))
.andExpect(status().isBadGateway())
.andExpect(jsonPath("$.code", is(NOTIFICATOR_CALL_EXCEPTION)))
.andExpect(jsonPath("$.message", is("Error call notificator create notification")));
}
@Test
void createOrUpdateNotification() throws Exception {
Notification notification = TestObjectFactory.testNotification();
@ -112,6 +144,17 @@ class NotificationResourceTest {
.create(any(com.rbkmoney.damsel.fraudbusters_notificator.Notification.class));
}
@Test
void removeChannelWithErrorCall() throws Exception {
String channelName = TestObjectFactory.randomString();
doThrow(new TException()).when(channelClient).remove(channelName);
mockMvc.perform(delete("/notifications/channels/{name}", channelName))
.andExpect(status().isBadGateway())
.andExpect(jsonPath("$.code", is(NOTIFICATOR_CALL_EXCEPTION)))
.andExpect(jsonPath("$.message", is("Error call notificator remove channel")));
}
@Test
void removeChannel() throws Exception {
String channelName = TestObjectFactory.randomString();
@ -123,6 +166,17 @@ class NotificationResourceTest {
verify(channelClient, times(1)).remove(channelName);
}
@Test
void removeNotificationWithErrorCall() throws Exception {
Long notificationId = TestObjectFactory.randomLong();
doThrow(new TException()).when(notificationClient).remove(notificationId);
mockMvc.perform(delete("/notifications/{id}", notificationId))
.andExpect(status().isBadGateway())
.andExpect(jsonPath("$.code", is(NOTIFICATOR_CALL_EXCEPTION)))
.andExpect(jsonPath("$.message", is("Error call notificator remove notification")));
}
@Test
void removeNotification() throws Exception {
Long notificationId = TestObjectFactory.randomLong();
@ -134,6 +188,20 @@ class NotificationResourceTest {
verify(notificationClient, times(1)).remove(notificationId);
}
@Test
void validateNotificationWithErrorCall() throws Exception {
Notification notification = TestObjectFactory.testNotification();
when(notificationClient.validate(any(com.rbkmoney.damsel.fraudbusters_notificator.Notification.class)))
.thenThrow(new TException());
mockMvc.perform(post("/notifications/validation")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(notification)))
.andExpect(status().isBadGateway())
.andExpect(jsonPath("$.code", is(NOTIFICATOR_CALL_EXCEPTION)))
.andExpect(jsonPath("$.message", is("Error call notificator validate notification")));
}
@Test
void validateNotificationWithResult() throws Exception {
Notification notification = TestObjectFactory.testNotification();
@ -180,6 +248,25 @@ class NotificationResourceTest {
ValidationError::getErrorReason).collect(Collectors.toList()));
}
@Test
void getNotificationsWithErrorCall() throws Exception {
long lastId = 1L;
int size = 10;
String searchValue = TestObjectFactory.randomString();
when(notificationClient
.getAll(new Page(size).setContinuationId(lastId), new Filter().setSearchField(searchValue)))
.thenThrow(new TException());
mockMvc.perform(get("/notifications")
.contentType(MediaType.APPLICATION_JSON)
.param("lastId", String.valueOf(lastId))
.param("size", String.valueOf(size))
.param("searchValue", searchValue))
.andExpect(status().isBadGateway())
.andExpect(jsonPath("$.code", is(NOTIFICATOR_CALL_EXCEPTION)))
.andExpect(jsonPath("$.message", is("Error call notificator getAll notifications")));
}
@Test
void getNotifications() throws Exception {
long lastId = 1L;
@ -187,8 +274,7 @@ class NotificationResourceTest {
String searchValue = TestObjectFactory.randomString();
var notifications = TestObjectFactory.testInternalNotifications(3);
when(notificationClient
.getAll(new Page(size).setContinuationId(lastId), new Filter().setSearchField(searchValue))
)
.getAll(new Page(size).setContinuationId(lastId), new Filter().setSearchField(searchValue)))
.thenReturn(new NotificationListResponse().setNotifications(notifications));
mockMvc.perform(get("/notifications")
@ -198,7 +284,26 @@ class NotificationResourceTest {
.param("searchValue", searchValue))
.andExpect(status().isOk())
.andExpect(jsonPath("$.result.[*]", hasSize(notifications.size())));
}
@Test
void getChannelsWithErrorCall() throws Exception {
long lastId = 1L;
int size = 10;
String searchValue = TestObjectFactory.randomString();
var channels = TestObjectFactory.testInternalChannels(3);
when(channelClient
.getAll(new Page(size).setContinuationId(lastId), new Filter().setSearchField(searchValue)))
.thenThrow(new TException());
mockMvc.perform(get("/notifications/channels")
.contentType(MediaType.APPLICATION_JSON)
.param("lastId", String.valueOf(lastId))
.param("size", String.valueOf(size))
.param("searchValue", searchValue))
.andExpect(status().isBadGateway())
.andExpect(jsonPath("$.code", is(NOTIFICATOR_CALL_EXCEPTION)))
.andExpect(jsonPath("$.message", is("Error call notificator getAll channels")));
}
@Test
@ -208,8 +313,7 @@ class NotificationResourceTest {
String searchValue = TestObjectFactory.randomString();
var channels = TestObjectFactory.testInternalChannels(3);
when(channelClient
.getAll(new Page(size).setContinuationId(lastId), new Filter().setSearchField(searchValue))
)
.getAll(new Page(size).setContinuationId(lastId), new Filter().setSearchField(searchValue)))
.thenReturn(new ChannelListResponse().setChannels(channels));
mockMvc.perform(get("/notifications/channels")
@ -221,27 +325,53 @@ class NotificationResourceTest {
.andExpect(jsonPath("$.result.[*]", hasSize(channels.size())));
}
@Test
void updateNotificationStatusWithErrorCall() throws Exception {
var status = com.rbkmoney.swag.fraudbusters.management.model.NotificationStatus.ACTIVE.getValue();
long id = 1L;
doThrow(new TException()).when(notificationClient).updateStatus(id, NotificationStatus.valueOf(status));
mockMvc.perform(put("/notifications/{id}/status", id)
.contentType(MediaType.APPLICATION_JSON)
.content(com.rbkmoney.swag.fraudbusters.management.model.NotificationStatus.ACTIVE.getValue()))
.andExpect(status().isBadGateway())
.andExpect(jsonPath("$.code", is(NOTIFICATOR_CALL_EXCEPTION)))
.andExpect(jsonPath("$.message", is("Error call notificator update notification status")));
}
@Test
void updateNotificationStatus() throws Exception {
var status = new com.rbkmoney.swag.fraudbusters.management.model.NotificationStatus();
status.setStatus(com.rbkmoney.swag.fraudbusters.management.model.NotificationStatus.StatusEnum.ACTIVE);
var status = com.rbkmoney.swag.fraudbusters.management.model.NotificationStatus.ACTIVE.getValue();
long id = 1L;
mockMvc.perform(put("/notifications/{id}/status", id)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(status)))
.content(com.rbkmoney.swag.fraudbusters.management.model.NotificationStatus.ACTIVE.getValue()))
.andExpect(status().isNoContent())
.andReturn();
verify(notificationClient, times(1))
.updateStatus(id, NotificationStatus.valueOf(status.getStatus().getValue()));
.updateStatus(id, NotificationStatus.valueOf(status));
}
@Test
void getChannelTypesWithErrorCall() throws Exception {
when(channelClient.getAllTypes()).thenThrow(new TException());
mockMvc.perform(get("/notifications/channels/types"))
.andExpect(status().isBadGateway())
.andExpect(jsonPath("$.code", is(NOTIFICATOR_CALL_EXCEPTION)))
.andExpect(jsonPath("$.message", is("Error call notificator getAll channel types")));
}
@Test
void getChannelTypes() throws Exception {
var channelTypes =
List.of(com.rbkmoney.swag.fraudbusters.management.model.ChannelType.TypeEnum.MAIL.getValue());
List.of(com.rbkmoney.swag.fraudbusters.management.model.ChannelType.MAIL.getValue());
when(channelClient.getAllTypes()).thenReturn(new ChannelTypeListResponse().setChannelTypes(channelTypes));
mockMvc.perform(get("/notifications/channels/types"))
@ -250,6 +380,16 @@ class NotificationResourceTest {
}
@Test
void getTemplatesWithErrorCall() throws Exception {
when(notificationTemplateClient.getAll()).thenThrow(new TException());
mockMvc.perform(get("/notifications/templates"))
.andExpect(status().isBadGateway())
.andExpect(jsonPath("$.code", is(NOTIFICATOR_CALL_EXCEPTION)))
.andExpect(jsonPath("$.message", is("Error call notificator getAll templates")));
}
@Test
void getTemplates() throws Exception {
var templates = TestObjectFactory.testNotificationTemplates(3);