|
23 | 23 | * #L% |
24 | 24 | */ |
25 | 25 |
|
26 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
27 | | -import static org.junit.jupiter.api.Assertions.assertNull; |
| 26 | +import com.google.protobuf.ByteString; |
| 27 | +import com.google.protobuf.Timestamp; |
| 28 | +import com.google.protobuf.util.Durations; |
| 29 | +import com.google.type.TimeOfDay; |
| 30 | +import no.entur.abt.mapstruct.common.Timestamps; |
| 31 | +import org.junit.jupiter.api.Test; |
28 | 32 |
|
29 | 33 | import java.time.Duration; |
30 | 34 | import java.time.Instant; |
31 | 35 | import java.time.LocalDateTime; |
| 36 | +import java.time.OffsetDateTime; |
32 | 37 | import java.time.ZoneId; |
33 | 38 | import java.time.temporal.ChronoUnit; |
34 | 39 | import java.util.concurrent.TimeUnit; |
35 | 40 |
|
36 | | -import org.junit.jupiter.api.Test; |
37 | | - |
38 | | -import com.google.protobuf.Timestamp; |
39 | | -import com.google.protobuf.util.Durations; |
40 | | - |
41 | | -import no.entur.abt.mapstruct.common.Timestamps; |
| 41 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertNull; |
42 | 43 |
|
43 | 44 | public class ProtobufStandardMappingsTest { |
44 | 45 |
|
45 | | - no.entur.abt.mapstruct.ProtobufStandardMappings MAPPER = no.entur.abt.mapstruct.ProtobufStandardMappings.INSTANCE; |
46 | | - |
47 | | - @Test |
48 | | - public void testMapLocalDateToTimestampSummertime() { |
49 | | - LocalDateTime l = LocalDateTime.of(2000, 6, 1, 12, 0); |
50 | | - |
51 | | - Timestamp timestamp = MAPPER.map(l); |
52 | | - Instant instant = MAPPER.mapToInstant(timestamp); |
53 | | - |
54 | | - LocalDateTime back = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); |
55 | | - |
56 | | - assertEquals(l, back); |
57 | | - } |
58 | | - |
59 | | - @Test |
60 | | - public void testMapLocalDateToTimestampWintertime() { |
61 | | - LocalDateTime l = LocalDateTime.of(2000, 2, 1, 12, 0); |
62 | | - |
63 | | - Timestamp timestamp = MAPPER.map(l); |
64 | | - Instant instant = MAPPER.mapToInstant(timestamp); |
65 | | - |
66 | | - LocalDateTime back = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); |
67 | | - |
68 | | - assertEquals(l, back); |
69 | | - } |
70 | | - |
71 | | - @Test |
72 | | - public void mapToInstant_whenSecondsAndNanosIs0_thenMapToNull() { |
73 | | - assertNull(MAPPER.mapToInstant(Timestamp.newBuilder().build())); |
74 | | - } |
75 | | - |
76 | | - @Test |
77 | | - public void mapToInstant_whenSecondsAndNanosIsNull_thenMapToNull() { |
78 | | - assertNull(MAPPER.mapToInstant(null)); |
79 | | - } |
80 | | - |
81 | | - @Test |
82 | | - public void mapToInstant_whenNanosIsSet_thenMapToInstant() { |
83 | | - assertEquals(3000, MAPPER.mapToInstant(Timestamp.newBuilder().setNanos(3000).build()).getNano()); |
84 | | - } |
85 | | - |
86 | | - @Test |
87 | | - public void mapToInstant_whenValueIsTooLargeForRangeForTimestamp_thenMapFromMaxValidTimestamp() { |
88 | | - assertEquals(MAPPER.mapToInstant(Timestamps.MAX_VALUE), MAPPER.mapToInstant(Timestamp.newBuilder().setSeconds(Long.MAX_VALUE).build())); |
89 | | - } |
90 | | - |
91 | | - @Test |
92 | | - public void mapToInstant_whenValueIsTooSmallForRangeForTimestamp_thenMapFromMinValidTimestamp() { |
93 | | - assertEquals(MAPPER.mapToInstant(Timestamps.MIN_VALUE), MAPPER.mapToInstant(Timestamp.newBuilder().setSeconds(-Long.MAX_VALUE).build())); |
94 | | - } |
95 | | - |
96 | | - @Test |
97 | | - public void mapInstantToTimestamp_whenValueIsTooLargeForRangeForTimestamp_thenMapToMaxValidTimestamp() { |
98 | | - assertEquals(Timestamps.MAX_VALUE, MAPPER.mapToTimestamp(Instant.now().plus(Integer.MAX_VALUE, ChronoUnit.DAYS))); |
99 | | - } |
100 | | - |
101 | | - @Test |
102 | | - public void mapInstantToTimestamp_whenValueIsTooSmallForRangeForTimestamp_thenMapToMinValidTimestamp() { |
103 | | - assertEquals(Timestamps.MIN_VALUE, MAPPER.mapToTimestamp(Instant.now().minus(Integer.MAX_VALUE, ChronoUnit.DAYS))); |
104 | | - } |
105 | | - |
106 | | - @Test |
107 | | - public void mapPositiveDuration() { |
108 | | - Duration duration = Duration.of(3, ChronoUnit.NANOS); |
| 46 | + no.entur.abt.mapstruct.ProtobufStandardMappings MAPPER = no.entur.abt.mapstruct.ProtobufStandardMappings.INSTANCE; |
109 | 47 |
|
110 | | - com.google.protobuf.Duration pbDuration = MAPPER.mapDuration(duration); |
111 | | - Durations.checkValid(pbDuration); |
112 | | - assertEquals(duration, MAPPER.mapDuration(pbDuration)); |
113 | | - } |
| 48 | + @Test |
| 49 | + public void testMapLocalDateToTimestampSummertime() { |
| 50 | + LocalDateTime l = LocalDateTime.of(2000, 6, 1, 12, 0); |
114 | 51 |
|
115 | | - @Test |
116 | | - public void mapNegativeDurationToProto_whenSecondsAreNegativeAndNanoPositive() { |
117 | | - Duration duration = Duration.ofSeconds(-3, 2); |
| 52 | + Timestamp timestamp = MAPPER.map(l); |
| 53 | + Instant instant = MAPPER.mapToInstant(timestamp); |
118 | 54 |
|
119 | | - com.google.protobuf.Duration pbDuration = MAPPER.mapDuration(duration); |
120 | | - Durations.checkValid(pbDuration); |
121 | | - assertEquals(duration, MAPPER.mapDuration(pbDuration)); |
122 | | - } |
| 55 | + LocalDateTime back = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); |
| 56 | + |
| 57 | + assertEquals(l, back); |
| 58 | + } |
123 | 59 |
|
124 | | - @Test |
125 | | - public void mapNegativeDurationToProto_whenSecondsArePositiveAndNanoNegative() { |
126 | | - // Duration.ofSeconds accepts negative values. Will still be stored as positive values in Duration |
127 | | - Duration duration = Duration.ofSeconds(3, -(TimeUnit.SECONDS.toNanos(1) - 2)); |
| 60 | + @Test |
| 61 | + public void testMapLocalDateToTimestampWintertime() { |
| 62 | + LocalDateTime l = LocalDateTime.of(2000, 2, 1, 12, 0); |
128 | 63 |
|
129 | | - com.google.protobuf.Duration pbDuration = MAPPER.mapDuration(duration); |
130 | | - Durations.checkValid(pbDuration); |
131 | | - assertEquals(duration, MAPPER.mapDuration(pbDuration)); |
132 | | - } |
| 64 | + Timestamp timestamp = MAPPER.map(l); |
| 65 | + Instant instant = MAPPER.mapToInstant(timestamp); |
| 66 | + |
| 67 | + LocalDateTime back = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); |
| 68 | + |
| 69 | + assertEquals(l, back); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void mapToInstant_whenSecondsAndNanosIs0_thenMapToNull() { |
| 74 | + assertNull(MAPPER.mapToInstant(Timestamp.newBuilder().build())); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void mapToInstant_whenSecondsAndNanosIsNull_thenMapToNull() { |
| 79 | + assertNull(MAPPER.mapToInstant(null)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void mapToInstant_whenNanosIsSet_thenMapToInstant() { |
| 84 | + assertEquals(3000, MAPPER.mapToInstant(Timestamp.newBuilder().setNanos(3000).build()).getNano()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void mapToInstant_whenValueIsTooLargeForRangeForTimestamp_thenMapFromMaxValidTimestamp() { |
| 89 | + assertEquals(MAPPER.mapToInstant(Timestamps.MAX_VALUE), MAPPER.mapToInstant(Timestamp.newBuilder().setSeconds(Long.MAX_VALUE).build())); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void mapToInstant_whenValueIsTooSmallForRangeForTimestamp_thenMapFromMinValidTimestamp() { |
| 94 | + assertEquals(MAPPER.mapToInstant(Timestamps.MIN_VALUE), MAPPER.mapToInstant(Timestamp.newBuilder().setSeconds(-Long.MAX_VALUE).build())); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void mapInstantToTimestamp_whenValueIsTooLargeForRangeForTimestamp_thenMapToMaxValidTimestamp() { |
| 99 | + assertEquals(Timestamps.MAX_VALUE, MAPPER.mapToTimestamp(Instant.now().plus(Integer.MAX_VALUE, ChronoUnit.DAYS))); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void mapInstantToTimestamp_whenValueIsTooSmallForRangeForTimestamp_thenMapToMinValidTimestamp() { |
| 104 | + assertEquals(Timestamps.MIN_VALUE, MAPPER.mapToTimestamp(Instant.now().minus(Integer.MAX_VALUE, ChronoUnit.DAYS))); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void mapToInstant_whenEpoch_thenReturnDefaultTimestamp() { |
| 109 | + assertEquals(Timestamp.getDefaultInstance(), MAPPER.mapToTimestamp(Instant.ofEpochSecond(0))); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void mapPositiveDuration() { |
| 114 | + Duration duration = Duration.of(3, ChronoUnit.NANOS); |
| 115 | + |
| 116 | + com.google.protobuf.Duration pbDuration = MAPPER.mapDuration(duration); |
| 117 | + Durations.checkValid(pbDuration); |
| 118 | + assertEquals(duration, MAPPER.mapDuration(pbDuration)); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void mapNegativeDurationToProto_whenSecondsAreNegativeAndNanoPositive() { |
| 123 | + Duration duration = Duration.ofSeconds(-3, 2); |
| 124 | + |
| 125 | + com.google.protobuf.Duration pbDuration = MAPPER.mapDuration(duration); |
| 126 | + Durations.checkValid(pbDuration); |
| 127 | + assertEquals(duration, MAPPER.mapDuration(pbDuration)); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void mapNegativeDurationToProto_whenSecondsArePositiveAndNanoNegative() { |
| 132 | + // Duration.ofSeconds accepts negative values. Will still be stored as positive values in Duration |
| 133 | + Duration duration = Duration.ofSeconds(3, -(TimeUnit.SECONDS.toNanos(1) - 2)); |
| 134 | + |
| 135 | + com.google.protobuf.Duration pbDuration = MAPPER.mapDuration(duration); |
| 136 | + Durations.checkValid(pbDuration); |
| 137 | + assertEquals(duration, MAPPER.mapDuration(pbDuration)); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void mapNegativeDuration_fromProto() { |
| 142 | + com.google.protobuf.Duration pbDuration = com.google.protobuf.Duration.newBuilder().setSeconds(-10).setNanos(-5).build(); |
| 143 | + |
| 144 | + Duration duration = MAPPER.mapDuration(pbDuration); |
| 145 | + Durations.checkValid(pbDuration); |
| 146 | + assertEquals(pbDuration, MAPPER.mapDuration(duration)); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void mapDurationToProto_whenNull_thenReturnDefaultInstance() { |
| 151 | + assertEquals(com.google.protobuf.Duration.getDefaultInstance(), MAPPER.mapDuration((Duration) null)); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void mapLocalDateToProto_whenNull_thenReturnDefaultInstance() { |
| 156 | + assertEquals(com.google.type.Date.getDefaultInstance(), MAPPER.mapLocalDate(null)); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void mapLocalDateTimeToProto_whenNull_thenReturnDefaultInstance() { |
| 161 | + assertEquals(Timestamp.getDefaultInstance(), MAPPER.map((LocalDateTime) null)); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void mapOffsetDateTimeToProto_whenNull_thenReturnDefaultInstance() { |
| 166 | + assertEquals(Timestamp.getDefaultInstance(), MAPPER.map((OffsetDateTime) null)); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void mapInstantToProto_whenNull_thenReturnDefaultInstance() { |
| 171 | + assertEquals(Timestamp.getDefaultInstance(), MAPPER.mapToTimestamp(null)); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void mapLocalTimeToProto_whenNull_thenReturnDefaultInstance() { |
| 176 | + assertEquals(TimeOfDay.getDefaultInstance(), MAPPER.mapLocalTime(null)); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void mapByteArrayToProto_whenNull_thenReturnEmpty() { |
| 181 | + assertEquals(ByteString.empty(), MAPPER.mapByteString((byte[]) null)); |
| 182 | + } |
133 | 183 |
|
134 | | - @Test |
135 | | - public void mapNegativeDuration_fromProto() { |
136 | | - com.google.protobuf.Duration pbDuration = com.google.protobuf.Duration.newBuilder().setSeconds(-10).setNanos(-5).build(); |
137 | 184 |
|
138 | | - Duration duration = MAPPER.mapDuration(pbDuration); |
139 | | - Durations.checkValid(pbDuration); |
140 | | - assertEquals(pbDuration, MAPPER.mapDuration(duration)); |
141 | | - } |
142 | 185 | } |
0 commit comments