reta commented on code in PR #3541:
URL: https://github.com/apache/cxf/pull/3541#discussion_r4190165768


##########
rt/features/logging/src/test/java/org/apache/cxf/ext/logging/LoggingOutInterceptorTest.java:
##########
@@ -0,0 +1,324 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.ext.logging;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.ext.logging.event.LogEvent;
+import org.apache.cxf.io.CachedOutputStream;
+import org.apache.cxf.io.DelayedCachedOutputStreamCleaner;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.equalToIgnoringCase;
+import static org.hamcrest.Matchers.hasSize;
+import static org.junit.Assert.assertThrows;
+
+public class LoggingOutInterceptorTest {
+    private Bus bus;
+    private DelayedCachedOutputStreamCleaner cleaner;
+    private TestEventSender sender;
+    private LoggingOutInterceptor interceptor; 
+    private Message message;
+    
+    @Before
+    public void setUp() {
+        bus = BusFactory.getDefaultBus(true);
+        cleaner = bus.getExtension(DelayedCachedOutputStreamCleaner.class);
+        sender = new TestEventSender();
+        interceptor = new LoggingOutInterceptor(sender);
+        message = new MessageImpl();
+        message.setExchange(new ExchangeImpl());
+    }
+    
+    @After
+    public void tearDown() {
+        assertThat(cleaner.size(), equalTo(0));
+    }
+
+    @Test
+    public void shouldLogMultipartPayload() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/";);
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        cached.close();
+        os.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadOnClean() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/";);
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        os.close();
+
+        // We did not close the cached stream, should be subject of cleanup
+        assertThat(cleaner.size(), equalTo(1));
+
+        cleaner.forceClean();
+        assertThat(cleaner.size(), equalTo(0));
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+
+    @Test
+    public void shouldLogMultipartPayloadCachedOutputStream() throws 
IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/";);
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final CachedOutputStream os = new CachedOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        cached.close();
+        os.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+
+    @Test
+    public void shouldLogMultipartPayloadWithExceptionOnClose() throws 
IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/";);
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream() {
+            public void close() throws IOException {
+                throw new IOException("Simulated");
+            }
+        };
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        assertThrows(IOException.class, () -> cached.close());
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadWithExceptionOnFlush() throws 
IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/";);
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream() {
+            public void flush() throws IOException {
+                throw new IOException("Simulated");
+            }
+        };
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        assertThrows(IOException.class, () -> cached.flush());
+        cached.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadClosedTwice() throws IOException {

Review Comment:
   This covers `shouldNotLogTwice`
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to