@Test void add() { Message message = Message.builder().type(MessageType.REFERRAL_SHARE).name("myNewItem").build(); MessageDto messageDto = MessageDto.builder().type(MessageType.REFERRAL_SHARE).name("myNewItem").build(); when(objectMapper.convertValue(messageDto, Message.class)).thenReturn(message); when(messageRepository.save(message)).thenReturn(message); Message result = messageService.add(messageDto); assertThat(result).isEqualTo(message); }
@Transactional @Override public Message add(MessageDto messageDto) { int minlength = 3; if (messageDto.getName().length() < 3) { throw new RuntimeException(String.format("Message name should be at least %s characters long", minlength)); } if (repository.findByTypeAndName(messageDto.getType(), messageDto.getName()).isPresent()) { throw new RuntimeException(String.format("Message with type %s and name %s already exists", messageDto.getType().toString(), messageDto.getName())); } Message newM = objectMapper.convertValue(messageDto, Message.class); return repository.save(newM); }
MessageServiceImpl implements MessageService { @Transactional @Override public Message add(MessageDto messageDto) { int minlength = 3; if (messageDto.getName().length() < 3) { throw new RuntimeException(String.format("Message name should be at least %s characters long", minlength)); } if (repository.findByTypeAndName(messageDto.getType(), messageDto.getName()).isPresent()) { throw new RuntimeException(String.format("Message with type %s and name %s already exists", messageDto.getType().toString(), messageDto.getName())); } Message newM = objectMapper.convertValue(messageDto, Message.class); return repository.save(newM); } }
MessageServiceImpl implements MessageService { @Transactional @Override public Message add(MessageDto messageDto) { int minlength = 3; if (messageDto.getName().length() < 3) { throw new RuntimeException(String.format("Message name should be at least %s characters long", minlength)); } if (repository.findByTypeAndName(messageDto.getType(), messageDto.getName()).isPresent()) { throw new RuntimeException(String.format("Message with type %s and name %s already exists", messageDto.getType().toString(), messageDto.getName())); } Message newM = objectMapper.convertValue(messageDto, Message.class); return repository.save(newM); } MessageServiceImpl(MessageRepository repository, ObjectMapper objectMapper); }
MessageServiceImpl implements MessageService { @Transactional @Override public Message add(MessageDto messageDto) { int minlength = 3; if (messageDto.getName().length() < 3) { throw new RuntimeException(String.format("Message name should be at least %s characters long", minlength)); } if (repository.findByTypeAndName(messageDto.getType(), messageDto.getName()).isPresent()) { throw new RuntimeException(String.format("Message with type %s and name %s already exists", messageDto.getType().toString(), messageDto.getName())); } Message newM = objectMapper.convertValue(messageDto, Message.class); return repository.save(newM); } MessageServiceImpl(MessageRepository repository, ObjectMapper objectMapper); @Transactional(readOnly = true) @Override List<MessageDto> getMessagesByType(MessageType type); @Override MessageDto getMessageByKey(String key); @Override MessageDto getMessageByTypeAndName(MessageType type, String name); @Transactional @Override Message update(MessageDto messageDto); @Transactional @Override Message add(MessageDto messageDto); @Transactional @Override void delete(MessageType type, String name); }
MessageServiceImpl implements MessageService { @Transactional @Override public Message add(MessageDto messageDto) { int minlength = 3; if (messageDto.getName().length() < 3) { throw new RuntimeException(String.format("Message name should be at least %s characters long", minlength)); } if (repository.findByTypeAndName(messageDto.getType(), messageDto.getName()).isPresent()) { throw new RuntimeException(String.format("Message with type %s and name %s already exists", messageDto.getType().toString(), messageDto.getName())); } Message newM = objectMapper.convertValue(messageDto, Message.class); return repository.save(newM); } MessageServiceImpl(MessageRepository repository, ObjectMapper objectMapper); @Transactional(readOnly = true) @Override List<MessageDto> getMessagesByType(MessageType type); @Override MessageDto getMessageByKey(String key); @Override MessageDto getMessageByTypeAndName(MessageType type, String name); @Transactional @Override Message update(MessageDto messageDto); @Transactional @Override Message add(MessageDto messageDto); @Transactional @Override void delete(MessageType type, String name);  }