|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.support.converter; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.Mockito.doReturn; |
| 21 | + |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 27 | +import org.junit.Rule; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.rules.ExpectedException; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.mockito.Mock; |
| 32 | +import org.mockito.junit.MockitoJUnitRunner; |
| 33 | + |
| 34 | +import org.springframework.data.projection.SpelAwareProxyProjectionFactory; |
| 35 | +import org.springframework.data.web.JsonPath; |
| 36 | +import org.springframework.kafka.support.KafkaNull; |
| 37 | +import org.springframework.messaging.support.MessageBuilder; |
| 38 | + |
| 39 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 40 | +import com.jayway.jsonpath.DocumentContext; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author Oliver Gierke |
| 44 | + * |
| 45 | + * @since 2.1.1 |
| 46 | + */ |
| 47 | +@RunWith(MockitoJUnitRunner.class) |
| 48 | +public class ProjectingMessageConverterTests { |
| 49 | + |
| 50 | + private static final String STRING_PAYLOAD = |
| 51 | + "{ \"username\" : \"SomeUsername\", \"user\" : { \"name\" : \"SomeName\"}}"; |
| 52 | + |
| 53 | + private static final byte[] BYTE_ARRAY_PAYLOAD = STRING_PAYLOAD.getBytes(StandardCharsets.UTF_8); |
| 54 | + |
| 55 | + private final ProjectingMessageConverter converter = new ProjectingMessageConverter(new ObjectMapper()); |
| 56 | + |
| 57 | + @Mock |
| 58 | + private ConsumerRecord<?, ?> record; |
| 59 | + |
| 60 | + @Rule |
| 61 | + public ExpectedException exception = ExpectedException.none(); |
| 62 | + |
| 63 | + @Test |
| 64 | + public void rejectsNullObjectMapper() { |
| 65 | + this.exception.expect(IllegalArgumentException.class); |
| 66 | + new ProjectingMessageConverter(null); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void returnsKafkaNullForNullPayload() { |
| 71 | + doReturn(null).when(this.record).value(); |
| 72 | + |
| 73 | + assertThat(this.converter.extractAndConvertValue(this.record, Object.class)).isEqualTo(KafkaNull.INSTANCE); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void createsProjectedPayloadForInterface() { |
| 78 | + assertProjectionProxy(STRING_PAYLOAD); |
| 79 | + assertProjectionProxy(BYTE_ARRAY_PAYLOAD); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void usesJacksonToCreatePayloadForClass() { |
| 84 | + assertSimpleObject(STRING_PAYLOAD); |
| 85 | + assertSimpleObject(BYTE_ARRAY_PAYLOAD); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void rejectsInvalidPayload() { |
| 90 | + this.exception.expect(ConversionException.class); |
| 91 | + this.exception.expectMessage(Object.class.getName()); |
| 92 | + |
| 93 | + assertProjectionProxy(new Object()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void writesProjectedPayloadUsingJackson() { |
| 98 | + Map<String, Object> values = new HashMap<>(); |
| 99 | + values.put("username", "SomeUsername"); |
| 100 | + values.put("name", "SomeName"); |
| 101 | + |
| 102 | + Sample sample = new SpelAwareProxyProjectionFactory().createProjection(Sample.class, values); |
| 103 | + |
| 104 | + Object payload = this.converter.convertPayload(MessageBuilder.withPayload(sample).build()); |
| 105 | + |
| 106 | + DocumentContext json = com.jayway.jsonpath.JsonPath.parse(payload.toString()); |
| 107 | + |
| 108 | + assertThat(json.read("$.username", String.class)).isEqualTo("SomeUsername"); |
| 109 | + assertThat(json.read("$.name", String.class)).isEqualTo("SomeName"); |
| 110 | + } |
| 111 | + |
| 112 | + private void assertProjectionProxy(Object payload) { |
| 113 | + doReturn(payload).when(this.record).value(); |
| 114 | + |
| 115 | + Object value = this.converter.extractAndConvertValue(this.record, Sample.class); |
| 116 | + |
| 117 | + assertThat(value).isInstanceOf(Sample.class); |
| 118 | + |
| 119 | + Sample sample = (Sample) value; |
| 120 | + |
| 121 | + assertThat(sample.getName()).isEqualTo("SomeName"); |
| 122 | + assertThat(sample.getUsername()).isEqualTo("SomeUsername"); |
| 123 | + } |
| 124 | + |
| 125 | + private void assertSimpleObject(Object payload) { |
| 126 | + doReturn(payload).when(this.record).value(); |
| 127 | + |
| 128 | + Object value = this.converter.extractAndConvertValue(this.record, AnotherSample.class); |
| 129 | + |
| 130 | + assertThat(value).isInstanceOf(AnotherSample.class); |
| 131 | + |
| 132 | + AnotherSample sample = (AnotherSample) value; |
| 133 | + |
| 134 | + assertThat(sample.user.name).isEqualTo("SomeName"); |
| 135 | + assertThat(sample.username).isEqualTo("SomeUsername"); |
| 136 | + } |
| 137 | + |
| 138 | + interface Sample { |
| 139 | + |
| 140 | + String getUsername(); |
| 141 | + |
| 142 | + @JsonPath("$.user.name") |
| 143 | + String getName(); |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + public static class AnotherSample { |
| 148 | + |
| 149 | + public String username; |
| 150 | + |
| 151 | + public User user; |
| 152 | + |
| 153 | + public static class User { |
| 154 | + |
| 155 | + public String name; |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments