|
| 1 | +package com.reflectoring; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.MapperFeature; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 7 | +import com.reflectoring.userdetails.model.UserData; |
| 8 | +import com.reflectoring.userdetails.persistence.Views; |
| 9 | +import com.reflectoring.util.MockedUsersUtility; |
| 10 | +import org.hamcrest.beans.PropertyUtil; |
| 11 | +import org.junit.Test; |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; |
| 13 | + |
| 14 | +import java.beans.PropertyDescriptor; |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Objects; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +@SpringBootTest |
| 23 | +public class JsonViewTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + public void serializeUserSummaryViewTest() throws JsonProcessingException { |
| 27 | + final UserData mockedUser = MockedUsersUtility.getMockedUserData(); |
| 28 | + |
| 29 | + // DEFAULT_VIEW_INCLUSION is disabled - Fields not annotated with @JsonView are not present |
| 30 | + final ObjectMapper objectMapper = new ObjectMapper(); |
| 31 | + objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); |
| 32 | + final String serializedValue = objectMapper |
| 33 | + .writerWithView(Views.UserSummary.class) |
| 34 | + .writeValueAsString(mockedUser); |
| 35 | + |
| 36 | + final List<String> expectedFields = Arrays.asList("createdBy", "createdDate", "updatedBy", "updatedDate", |
| 37 | + "additionalData", "loginId", "loginPassword", "ssnNumber"); |
| 38 | + expectedFields.stream().forEach(field -> { |
| 39 | + assertFalse(serializedValue.contains(field)); |
| 40 | + }); |
| 41 | + |
| 42 | + // DEFAULT_VIEW_INCLUSION is enabled - Fields not annotated with @JsonView are present |
| 43 | + final ObjectMapper objectMapper1 = new ObjectMapper(); |
| 44 | + objectMapper1.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true); |
| 45 | + final String serializedValue1 = objectMapper1 |
| 46 | + .writerWithView(Views.UserSummary.class) |
| 47 | + .writeValueAsString(mockedUser); |
| 48 | + System.out.println(serializedValue1); |
| 49 | + |
| 50 | + assertTrue(serializedValue1.contains("additionalData")); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void serializeUserDetailedSummaryViewTest() throws JsonProcessingException { |
| 55 | + final UserData mockedUser = MockedUsersUtility.getMockedUserData(); |
| 56 | + |
| 57 | + // DEFAULT_VIEW_INCLUSION is disabled - Fields not annotated with @JsonView are not present |
| 58 | + final ObjectMapper objectMapper = new ObjectMapper(); |
| 59 | + objectMapper.registerModule(new JavaTimeModule()); |
| 60 | + objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); |
| 61 | + final String serializedValue = objectMapper |
| 62 | + .writerWithView(Views.UserDetailedSummary.class) |
| 63 | + .writeValueAsString(mockedUser); |
| 64 | + |
| 65 | + final List<String> expectedFields = Arrays.asList("createdBy", "createdDate", "updatedBy", "updatedDate"); |
| 66 | + expectedFields.stream().forEach(field -> { |
| 67 | + assertTrue(serializedValue.contains(field)); |
| 68 | + }); |
| 69 | + |
| 70 | + final List<String> moreFields = Arrays.asList("loginId", "loginPassword", "ssnNumber"); |
| 71 | + moreFields.stream().forEach(field -> { |
| 72 | + assertFalse(serializedValue.contains(field)); |
| 73 | + }); |
| 74 | + |
| 75 | + // DEFAULT_VIEW_INCLUSION is enabled - Fields not annotated with @JsonView are present |
| 76 | + final ObjectMapper objectMapper1 = new ObjectMapper(); |
| 77 | + objectMapper1.registerModule(new JavaTimeModule()); |
| 78 | + objectMapper1.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true); |
| 79 | + final String serializedValue1 = objectMapper1 |
| 80 | + .writerWithView(Views.UserDetailedSummary.class) |
| 81 | + .writeValueAsString(mockedUser); |
| 82 | + System.out.println(serializedValue1); |
| 83 | + |
| 84 | + assertTrue(serializedValue1.contains("additionalData")); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void deserializeUserSummaryViewTest() throws JsonProcessingException { |
| 89 | + // DEFAULT_VIEW_INCLUSION is disabled - Fields not annotated with @JsonView are not present |
| 90 | + // Deserializes only the fields present in the view |
| 91 | + final ObjectMapper objectMapper = new ObjectMapper(); |
| 92 | + objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); |
| 93 | + final UserData deserializedValue = objectMapper |
| 94 | + .readerWithView(Views.UserSummary.class) |
| 95 | + .forType(UserData.class) |
| 96 | + .readValue(MockedUsersUtility.userDataObjectAsString()); |
| 97 | + |
| 98 | + System.out.println("Deserialize with DEFAULT_VIEW_INCLUSION as false :" + deserializedValue); |
| 99 | + |
| 100 | + assertTrue(Objects.isNull(deserializedValue.getCreatedBy())); |
| 101 | + assertTrue(Objects.isNull(deserializedValue.getCreatedDate())); |
| 102 | + assertTrue(Objects.isNull(deserializedValue.getUpdatedBy())); |
| 103 | + assertTrue(Objects.isNull(deserializedValue.getUpdatedDate())); |
| 104 | + assertTrue(Objects.isNull(deserializedValue.getAdditionalData())); |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void deserializeUserDetailedSummaryViewTest() throws JsonProcessingException { |
| 110 | + // DEFAULT_VIEW_INCLUSION is disabled - Fields not annotated with @JsonView are not present |
| 111 | + // Deserializes only the fields present in the view |
| 112 | + final ObjectMapper objectMapper = new ObjectMapper(); |
| 113 | + objectMapper.registerModule(new JavaTimeModule()); |
| 114 | + objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); |
| 115 | + final UserData deserializedValue = objectMapper |
| 116 | + .readerWithView(Views.UserDetailedSummary.class) |
| 117 | + .forType(UserData.class) |
| 118 | + .readValue(MockedUsersUtility.userDataObjectAsString()); |
| 119 | + |
| 120 | + assertFalse(Objects.isNull(deserializedValue.getCreatedBy())); |
| 121 | + assertFalse(Objects.isNull(deserializedValue.getCreatedDate())); |
| 122 | + assertFalse(Objects.isNull(deserializedValue.getUpdatedBy())); |
| 123 | + assertFalse(Objects.isNull(deserializedValue.getUpdatedDate())); |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments