|
| 1 | +/* |
| 2 | + * Copyright 2016 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.integration.zip.splitter; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | + |
| 22 | +import java.util.LinkedHashMap; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | + |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.integration.channel.QueueChannel; |
| 30 | +import org.springframework.integration.file.FileHeaders; |
| 31 | +import org.springframework.integration.support.MessageBuilder; |
| 32 | +import org.springframework.integration.zip.ZipHeaders; |
| 33 | +import org.springframework.messaging.Message; |
| 34 | +import org.springframework.messaging.MessageChannel; |
| 35 | +import org.springframework.test.context.ContextConfiguration; |
| 36 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 37 | + |
| 38 | +/** |
| 39 | + * @author Andriy Kryvtsun |
| 40 | + */ |
| 41 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 42 | +@ContextConfiguration |
| 43 | +public class UnZipResultSplitterTests { |
| 44 | + |
| 45 | + private static final String DIR_1 = "dir1/"; |
| 46 | + |
| 47 | + private static final String DIR_2 = "dir2/"; |
| 48 | + |
| 49 | + private static final String FILE_1 = "file1"; |
| 50 | + |
| 51 | + private static final String FILE_2 = "file2"; |
| 52 | + |
| 53 | + private static final String DATA_1 = "data1"; |
| 54 | + |
| 55 | + private static final String DATA_2 = "data2"; |
| 56 | + |
| 57 | + private static int TIMEOUT = 10000; |
| 58 | + |
| 59 | + @Autowired |
| 60 | + private MessageChannel input; |
| 61 | + |
| 62 | + @Autowired |
| 63 | + private QueueChannel output; |
| 64 | + |
| 65 | + @Test |
| 66 | + public void splitPreservingSourceMessageHeaderValues() { |
| 67 | + |
| 68 | + final String headerName = "headerName"; |
| 69 | + final String headerValue = "headerValue"; |
| 70 | + |
| 71 | + Message<?> inMessage = MessageBuilder.withPayload(createPayload()) |
| 72 | + .setHeader(headerName, headerValue) |
| 73 | + .build(); |
| 74 | + |
| 75 | + input.send(inMessage); |
| 76 | + |
| 77 | + Message<?> message1 = output.receive(TIMEOUT); |
| 78 | + checkMessageWithHeaderValue(message1, headerName, headerValue, DATA_1); |
| 79 | + |
| 80 | + Message<?> message2 = output.receive(TIMEOUT); |
| 81 | + checkMessageWithHeaderValue(message2, headerName, headerValue, DATA_2); |
| 82 | + } |
| 83 | + |
| 84 | + private static void checkMessageWithHeaderValue(Message<?> message, String headerName, String headerValue, |
| 85 | + String payload) { |
| 86 | + assertNotNull(message); |
| 87 | + checkHeaderValue(message, headerName, headerValue); |
| 88 | + checkPayload(message, payload); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void splitPreservingServiceHeaderValues() { |
| 93 | + |
| 94 | + Message<?> inMessage = MessageBuilder.withPayload(createPayload()) |
| 95 | + .setHeader(ZipHeaders.ZIP_ENTRY_PATH, "dir") |
| 96 | + .setHeader(FileHeaders.FILENAME, "filename") |
| 97 | + .build(); |
| 98 | + |
| 99 | + input.send(inMessage); |
| 100 | + |
| 101 | + Message<?> message1 = output.receive(TIMEOUT); |
| 102 | + checkMessageWithServiceHeaderValues(message1, DIR_1, FILE_1, DATA_1); |
| 103 | + |
| 104 | + Message<?> message2 = output.receive(TIMEOUT); |
| 105 | + checkMessageWithServiceHeaderValues(message2, DIR_2, FILE_2, DATA_2); |
| 106 | + } |
| 107 | + |
| 108 | + private static void checkMessageWithServiceHeaderValues(Message<?> message, String path, String filename, |
| 109 | + String payload) { |
| 110 | + assertNotNull(message); |
| 111 | + checkHeaderValue(message, ZipHeaders.ZIP_ENTRY_PATH, path); |
| 112 | + checkHeaderValue(message, FileHeaders.FILENAME, filename); |
| 113 | + checkPayload(message, payload); |
| 114 | + } |
| 115 | + |
| 116 | + private static Map<String, Object> createPayload() { |
| 117 | + Map<String, Object> payload = new LinkedHashMap<String, Object>(); |
| 118 | + payload.put(DIR_1 + FILE_1, DATA_1); |
| 119 | + payload.put(DIR_2 + FILE_2, DATA_2); |
| 120 | + return payload; |
| 121 | + } |
| 122 | + |
| 123 | + private static void checkPayload(Message<?> message, String payload) { |
| 124 | + assertEquals(payload, message.getPayload()); |
| 125 | + } |
| 126 | + |
| 127 | + private static void checkHeaderValue(Message<?> message, String headerName, String headerValue) { |
| 128 | + assertEquals(headerValue, message.getHeaders().get(headerName)); |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments