]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8332 make RequestIdMDCStorage inner class of RequestIdFilter 1364/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 15 Nov 2016 11:13:22 +0000 (12:13 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 15 Nov 2016 12:31:50 +0000 (13:31 +0100)
server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestIdFilter.java
server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestIdMDCStorage.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestIdFilterTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestIdMDCStorageTest.java [deleted file]

index 9807cd91190f22442e1dfc6982f545a94256aeb5..d157116eb4b6dfd8b3321c561e40fab1aa7705ef 100644 (file)
@@ -27,8 +27,11 @@ import javax.servlet.FilterConfig;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
+import org.slf4j.MDC;
 import org.sonar.server.platform.Platform;
 
+import static java.util.Objects.requireNonNull;
+
 /**
  * A {@link Filter} that puts and removes the HTTP request ID from the {@link org.slf4j.MDC}.
  */
@@ -62,4 +65,20 @@ public class RequestIdFilter implements Filter {
   public void destroy() {
     // nothing to do
   }
+
+  /**
+   * Wraps MDC calls to store the HTTP request ID in the {@link MDC} into an {@link AutoCloseable}.
+   */
+  static class RequestIdMDCStorage implements AutoCloseable {
+    private static final String HTTP_REQUEST_ID_MDC_KEY = "HTTP_REQUEST_ID";
+
+    public RequestIdMDCStorage(String requestId) {
+      MDC.put(HTTP_REQUEST_ID_MDC_KEY, requireNonNull(requestId, "Request ID can't be null"));
+    }
+
+    @Override
+    public void close() {
+      MDC.remove(HTTP_REQUEST_ID_MDC_KEY);
+    }
+  }
 }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestIdMDCStorage.java b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestIdMDCStorage.java
deleted file mode 100644 (file)
index 3130a32..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.platform.web.requestid;
-
-import org.slf4j.MDC;
-
-import static java.util.Objects.requireNonNull;
-
-/**
- * Wraps MDC calls to store the HTTP request ID in the {@link MDC} into an {@link AutoCloseable}.
- */
-class RequestIdMDCStorage implements AutoCloseable {
-  private static final String HTTP_REQUEST_ID_MDC_KEY = "HTTP_REQUEST_ID";
-
-  public RequestIdMDCStorage(String requestId) {
-    MDC.put(HTTP_REQUEST_ID_MDC_KEY, requireNonNull(requestId, "Request ID can't be null"));
-  }
-
-  @Override
-  public void close() {
-    MDC.remove(HTTP_REQUEST_ID_MDC_KEY);
-  }
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestIdFilterTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestIdFilterTest.java
new file mode 100644 (file)
index 0000000..41a53c3
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.web.requestid;
+
+import java.io.IOException;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.MDC;
+import org.sonar.core.platform.ComponentContainer;
+import org.sonar.server.platform.Platform;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class RequestIdFilterTest {
+  private Platform platform = mock(Platform.class);
+  private RequestIdGenerator requestIdGenerator = mock(RequestIdGenerator.class);
+  private ServletRequest servletRequest = mock(ServletRequest.class);
+  private ServletResponse servletResponse = mock(ServletResponse.class);
+  private FilterChain filterChain = mock(FilterChain.class);
+  private RequestIdFilter underTest = new RequestIdFilter(platform);
+
+  @Before
+  public void setUp() throws Exception {
+    ComponentContainer container = new ComponentContainer();
+    container.add(requestIdGenerator);
+    when(platform.getContainer()).thenReturn(container);
+  }
+
+  @Test
+  public void filter_put_id_in_MDC_and_remove_it_after_chain_has_executed() throws IOException, ServletException {
+    String requestId = "request id";
+    when(requestIdGenerator.generate()).thenReturn(requestId);
+    doAnswer(invocation -> assertThat(MDC.get("HTTP_REQUEST_ID")).isEqualTo(requestId))
+      .when(filterChain)
+      .doFilter(servletRequest, servletResponse);
+
+    underTest.doFilter(servletRequest, servletResponse, filterChain);
+
+    assertThat(MDC.get("HTTP_REQUEST_ID")).isNull();
+  }
+
+  @Test
+  public void filter_put_id_in_MDC_and_remove_it_after_chain_throws_exception() throws IOException, ServletException {
+    RuntimeException exception = new RuntimeException("Simulating chain failing");
+    String requestId = "request id";
+    when(requestIdGenerator.generate()).thenReturn(requestId);
+    doAnswer(invocation -> {
+      assertThat(MDC.get("HTTP_REQUEST_ID")).isEqualTo(requestId);
+      throw exception;
+    })
+      .when(filterChain)
+      .doFilter(servletRequest, servletResponse);
+
+    try {
+      underTest.doFilter(servletRequest, servletResponse, filterChain);
+      fail("A runtime exception should have been raised");
+    } catch (RuntimeException e) {
+      assertThat(e).isEqualTo(exception);
+    } finally {
+      assertThat(MDC.get("HTTP_REQUEST_ID")).isNull();
+    }
+
+  }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestIdMDCStorageTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestIdMDCStorageTest.java
deleted file mode 100644 (file)
index 01a638c..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.platform.web.requestid;
-
-import org.apache.log4j.MDC;
-import org.junit.After;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class RequestIdMDCStorageTest {
-  @Rule
-  public ExpectedException expectedException = ExpectedException.none();
-
-  @After
-  public void tearDown() throws Exception {
-    MDC.clear();
-  }
-
-  @Test
-  public void constructor_fails_with_NPE_when_argument_is_null() {
-    expectedException.expect(NullPointerException.class);
-    expectedException.expectMessage("Request ID can't be null");
-
-    new RequestIdMDCStorage(null);
-  }
-
-  @Test
-  public void constructor_adds_specified_value_in_MDC_under_HTTP_REQUEST_ID_key() {
-    new RequestIdMDCStorage("toto");
-
-    assertThat(MDC.get("HTTP_REQUEST_ID")).isEqualTo("toto");
-  }
-
-  @Test
-  public void close_removes_value_from_MDC() {
-    RequestIdMDCStorage underTest = new RequestIdMDCStorage("boum");
-    assertThat(MDC.get("HTTP_REQUEST_ID")).isEqualTo("boum");
-
-    underTest.close();
-
-    assertThat(MDC.get("HTTP_REQUEST_ID")).isNull();
-  }
-}