Fixed message restting after page refresh (#234)
Co-authored-by: widlam <mikolaj.widla@gmail.com> Reviewed-on: #234 Reviewed-by: Adam Bem <bema@noreply.example.com> Co-authored-by: Mikolaj Widla <widlam@noreply.example.com> Co-committed-by: Mikolaj Widla <widlam@noreply.example.com>
This commit is contained in:
		| @@ -1,6 +1,5 @@ | ||||
| package com.r11.tools.controller; | ||||
|  | ||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||
| import com.r11.tools.model.MockedMessageDto; | ||||
| import com.r11.tools.service.KlausService; | ||||
| import lombok.AllArgsConstructor; | ||||
| @@ -9,12 +8,12 @@ import org.apache.logging.log4j.LogManager; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import org.springframework.http.HttpHeaders; | ||||
| import org.springframework.http.HttpStatus; | ||||
| import org.springframework.http.MediaType; | ||||
| import org.springframework.http.ResponseEntity; | ||||
| import org.springframework.web.bind.annotation.*; | ||||
|  | ||||
| import java.time.LocalDateTime; | ||||
| import java.util.*; | ||||
| import java.util.Arrays; | ||||
| import java.util.Objects; | ||||
| import java.util.UUID; | ||||
|  | ||||
| /** | ||||
|  * Returns the homepage and provides the api for javascript async requests. | ||||
| @@ -57,40 +56,10 @@ public class MockController { | ||||
|         MockedMessageDto message ; | ||||
|         if(uuidValue == null || uuidValue.equals("")) clientUUID = UUID.randomUUID(); | ||||
|         else clientUUID = UUID.fromString(uuidValue); | ||||
|         message = klausService | ||||
|                 .getMockedMessageByClientUUID(clientUUID) | ||||
|                 .orElse( buildDefaultMessage(clientUUID) ); | ||||
|  | ||||
|         message = klausService.getMockedResponse(clientUUID.toString()); | ||||
|         return message; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * Constructs message with default set of data | ||||
|      * @param uuid the key-uuid of given set of messages | ||||
|      * @return message with default dataset | ||||
|      */ | ||||
|     private MockedMessageDto buildDefaultMessage(UUID uuid){ | ||||
|         Map<String, String> headers = new HashMap<>(); | ||||
|         headers.put("Keep-Alive", "timeout=60"); | ||||
|         headers.put("Connection", "keep-alive"); | ||||
|         headers.put("Date", LocalDateTime.now().toString()); | ||||
|         MockedMessageDto mockedMessageDto = MockedMessageDto.builder() | ||||
|                 .clientUUID(uuid) | ||||
|                 .contentType(MediaType.APPLICATION_XML_VALUE) | ||||
|                 .messageBody("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||||
|                         "<note>\n" + | ||||
|                         "  <to>Tove</to>\n" + | ||||
|                         "  <from>Jani</from>\n" + | ||||
|                         "  <heading>Reminder</heading>\n" + | ||||
|                         "  <body>Don't forget me this weekend!</body>\n" + | ||||
|                         "</note>") | ||||
|                 .httpHeaders(headers) | ||||
|                 .httpStatus(200) | ||||
|                 .build(); | ||||
|         klausService.setMockedResponse(mockedMessageDto); | ||||
|         return mockedMessageDto; | ||||
|     } | ||||
|     /** | ||||
|      * It's one of the most important features - the bread and butter of the Mocked Service. It's link that allows | ||||
|      * to receive mocked response from the server and use it to mock! | ||||
| @@ -99,7 +68,7 @@ public class MockController { | ||||
|      */ | ||||
|     @RequestMapping(value = "/r/{clientUUID}") | ||||
|     public ResponseEntity getMockedResponse( | ||||
|                                             @PathVariable UUID clientUUID) { | ||||
|                                             @PathVariable String clientUUID) { | ||||
|         MockedMessageDto mockedMessageDto = klausService.getMockedResponse(clientUUID); | ||||
|         HttpHeaders httpHeaders = new HttpHeaders(); | ||||
|         if (mockedMessageDto.getHttpHeaders() != null) mockedMessageDto.getHttpHeaders().forEach(httpHeaders::set); | ||||
|   | ||||
| @@ -26,7 +26,7 @@ import java.util.UUID; | ||||
| public class MockedMessage implements Serializable { | ||||
|     @Indexed | ||||
|     @Id | ||||
|     private UUID clientUUID; | ||||
|     private String clientUUID; | ||||
|     private String contentType; | ||||
|     private String messageBody; | ||||
|     private Map<String, String> httpHeaders; | ||||
|   | ||||
| @@ -18,7 +18,7 @@ import java.util.UUID; | ||||
| @NoArgsConstructor | ||||
| @AllArgsConstructor | ||||
| public class MockedMessageDto implements Serializable{ | ||||
|     private UUID clientUUID; | ||||
|     private String clientUUID; | ||||
|     private String contentType; | ||||
|     private String messageBody; | ||||
|     private Map<String, String> httpHeaders; | ||||
|   | ||||
| @@ -1,24 +1,16 @@ | ||||
| package com.r11.tools.repository; | ||||
|  | ||||
| import com.r11.tools.model.MockedMessage; | ||||
| import java.util.List; | ||||
| import java.util.Optional; | ||||
| import java.util.UUID; | ||||
| import org.springframework.data.repository.CrudRepository; | ||||
| import org.springframework.stereotype.Repository; | ||||
| import org.springframework.transaction.annotation.Transactional; | ||||
|  | ||||
| import java.util.Optional; | ||||
|  | ||||
| /** | ||||
|  * Spring repository that allows to retrieve message list by key-uuid from redis database | ||||
|  * @author Rafał Żukowicz | ||||
|  */ | ||||
| @Repository | ||||
| @Transactional | ||||
| public interface MockedResponseRepository extends CrudRepository<MockedMessage, UUID> { | ||||
|     /** | ||||
|      * Finds all messages by their uuid | ||||
|      * @param clientUUID the key-uuid of given set of messages | ||||
|      * @return Optional of list of {@link com.r11.tools.model.MockedMessage} | ||||
|      */ | ||||
|     Optional<MockedMessage> findAllByClientUUID(UUID clientUUID); | ||||
| } | ||||
| public interface MockedResponseRepository extends CrudRepository<MockedMessage, String> {} | ||||
|   | ||||
| @@ -13,7 +13,6 @@ import org.springframework.stereotype.Service; | ||||
|  */ | ||||
| @Service | ||||
| public interface KlausService { | ||||
|     Optional<MockedMessageDto> getMockedMessageByClientUUID(UUID clientUUID); | ||||
|     MockedMessageDto getMockedResponse(UUID clientUUID); | ||||
|     MockedMessageDto getMockedResponse(String clientUUID); | ||||
|     ResponseEntity<String> setMockedResponse(MockedMessageDto mockedMessageDto); | ||||
| } | ||||
|   | ||||
| @@ -10,11 +10,14 @@ import org.apache.logging.log4j.LogManager; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import org.springframework.http.HttpHeaders; | ||||
| import org.springframework.http.HttpStatus; | ||||
| import org.springframework.http.MediaType; | ||||
| import org.springframework.http.ResponseEntity; | ||||
| import org.springframework.stereotype.Service; | ||||
|  | ||||
| import java.time.LocalDateTime; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import java.util.Optional; | ||||
| import java.util.UUID; | ||||
|  | ||||
| /** | ||||
|  * Service for {@link com.r11.tools.controller.MockController} and {@link com.r11.tools.controller.MockController} | ||||
| @@ -31,18 +34,6 @@ public class KlausServiceImpl implements KlausService { | ||||
|     private final MockedResponseRepository mockedResponseRepository; | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * Returns all messages of given key-uuid | ||||
|      * @param clientUUID the key-uuid of given set of messages | ||||
|      * @return List of {@link MockedMessageDto} | ||||
|      */ | ||||
|     @Override | ||||
|     public Optional<MockedMessageDto> getMockedMessageByClientUUID(UUID clientUUID){ | ||||
|         Optional<MockedMessage> mockedMessageOptional = mockedResponseRepository.findAllByClientUUID(clientUUID); | ||||
|         log.info("Message for UUID: "+clientUUID+" has been fetched from DB."); | ||||
|         return mockedMessageMapper.optionalMockedMessageToOptionalMockedMessageDTO(mockedMessageOptional); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Returns {@link MockedMessageDto} of given id and key-uuid. If message doesn't then empty message is returned | ||||
|      * @param clientUUID the key-uuid of given set of messages | ||||
| @@ -50,18 +41,44 @@ public class KlausServiceImpl implements KlausService { | ||||
|      */ | ||||
|     @SneakyThrows | ||||
|     @Override | ||||
|     public MockedMessageDto getMockedResponse(UUID clientUUID){ | ||||
|     public MockedMessageDto getMockedResponse(String clientUUID){ | ||||
|         Optional<MockedMessage> optionalMockedMessage = mockedResponseRepository.findById(clientUUID); | ||||
|         MockedMessageDto mockedMessageDto = MockedMessageDto.builder() | ||||
|                 .clientUUID(clientUUID) | ||||
|                 .build(); | ||||
|         MockedMessageDto mockedMessageDto; | ||||
|         if (optionalMockedMessage.isPresent()) { | ||||
|             mockedMessageDto = mockedMessageMapper.mockedMessageToMockedMessageDto(optionalMockedMessage.get()); | ||||
|             //log.info(mockedMessageDto.toString().replaceAll("\"", "\\\\\"")); | ||||
|             return mockedMessageDto; | ||||
|         } else { | ||||
|             MockedMessageDto defaultMessage = buildDefaultMessage(clientUUID); | ||||
|             setMockedResponse(defaultMessage); | ||||
|             return defaultMessage; | ||||
|         } | ||||
|         //log.info(mockedMessageDto.toString().replaceAll("\"", "\\\\\"")); | ||||
|         return mockedMessageDto; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Constructs message with default set of data | ||||
|      * @param uuid the key-uuid of given set of messages | ||||
|      * @return message with default dataset | ||||
|      */ | ||||
|  | ||||
|  | ||||
|     private MockedMessageDto buildDefaultMessage(String uuid){ | ||||
|         Map<String, String> headers = new HashMap<>(); | ||||
|         headers.put("Keep-Alive", "timeout=60"); | ||||
|         headers.put("Connection", "keep-alive"); | ||||
|         headers.put("Date", LocalDateTime.now().toString()); | ||||
|         return MockedMessageDto.builder() | ||||
|                 .clientUUID(uuid) | ||||
|                 .contentType(MediaType.APPLICATION_XML_VALUE) | ||||
|                 .messageBody("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||||
|                         "<note>\n" + | ||||
|                         "  <to>Tove</to>\n" + | ||||
|                         "  <from>Jani</from>\n" + | ||||
|                         "  <heading>Reminder</heading>\n" + | ||||
|                         "  <body>Don't forget me this weekend!</body>\n" + | ||||
|                         "</note>") | ||||
|                 .httpHeaders(headers) | ||||
|                 .httpStatus(200) | ||||
|                 .build(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -72,8 +89,10 @@ public class KlausServiceImpl implements KlausService { | ||||
|     @SneakyThrows | ||||
|     @Override | ||||
|     public ResponseEntity<String> setMockedResponse(MockedMessageDto mockedMessageDto) { | ||||
|         mockedResponseRepository.save(mockedMessageMapper.mockedMessageDtoToMockedMessage(mockedMessageDto)); | ||||
|         //log.info(mockedMessageDto.toString().replaceAll("\"", "\\\\\"")); | ||||
|         MockedMessage message = mockedMessageMapper.mockedMessageDtoToMockedMessage(mockedMessageDto); | ||||
|         message.setCreatedAt(LocalDateTime.now()); | ||||
|         log.info("SAVE:"+message.toString().replace("\n"," ")); | ||||
|         mockedResponseRepository.save(message); | ||||
|         return new ResponseEntity<>("MockedResponse has been setup successfully!", new HttpHeaders(), | ||||
|                 HttpStatus.ACCEPTED); | ||||
|     } | ||||
|   | ||||
| @@ -12,7 +12,7 @@ const props = defineProps( | ||||
|  | ||||
| const message = ref(''); | ||||
| const visible = ref('hidden'); | ||||
| const fetchLink = window.location.protocol + "//" + window.location.hostname + "/mock/api/mock" | ||||
| const fetchLink = window.location.protocol + "//" + window.location.hostname + "/mock/api/mock"; | ||||
|  | ||||
| function prepareAndSendData(){ | ||||
|     if (props.messageData != null|| props.messageData != undefined ){ | ||||
| @@ -27,7 +27,7 @@ function prepareAndSendData(){ | ||||
|  | ||||
| function showToast(){ | ||||
|     visible.value = "visible"; | ||||
|     setTimeout( () => { visible.value = "opacity-0" } , 1500 ) | ||||
|     setTimeout( () => { visible.value = "opacity-0" } , 1000 ) | ||||
| } | ||||
|  | ||||
| function hideToast(){ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user