Copilot commented on code in PR #135:
URL: 
https://github.com/apache/fineract-consumer-facing/pull/135#discussion_r3952939500


##########
consumer/src/test/java/org/apache/fineract/consumer/infrastructure/web/filter/CorrelationIdFilterTest.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.fineract.consumer.infrastructure.web.filter;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import jakarta.servlet.FilterChain;
+import org.apache.fineract.consumer.infrastructure.web.data.ConsumerHeaders;
+import 
org.apache.fineract.consumer.infrastructure.web.data.CorrelationConstants;
+import 
org.apache.fineract.consumer.infrastructure.web.service.CorrelationIdHolder;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.slf4j.MDC;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+class CorrelationIdFilterTest {
+
+    private final CorrelationIdHolder holder = new CorrelationIdHolder();
+    private final CorrelationIdFilter filter = new CorrelationIdFilter(holder);
+    private final FilterChain chain = mock(FilterChain.class);
+    private final MockHttpServletRequest request = new 
MockHttpServletRequest();
+    private final MockHttpServletResponse response = new 
MockHttpServletResponse();
+
+    @AfterEach
+    void clearMdc() {
+        MDC.clear();
+        holder.clear();
+    }
+
+    @Test
+    void generatesIdWhenHeaderMissing() throws Exception {
+        filter.doFilter(request, response, chain);
+
+        verify(chain).doFilter(request, response);
+        
assertThat(response.getHeader(ConsumerHeaders.CORRELATION_ID)).isNotBlank();
+        assertThat(holder.get()).isNull();
+        assertThat(MDC.get(CorrelationConstants.MDC_KEY)).isNull();
+    }
+
+    @Test
+    void propagatesValidInboundHeader() throws Exception {
+        String inbound = "client-corr-abc_123.OK";
+        request.addHeader(ConsumerHeaders.CORRELATION_ID, inbound);
+
+        filter.doFilter(request, response, chain);
+
+        verify(chain).doFilter(request, response);
+        
assertThat(response.getHeader(ConsumerHeaders.CORRELATION_ID)).isEqualTo(inbound);
+    }
+
+    @Test
+    void replacesBlankInboundHeader() throws Exception {
+        request.addHeader(ConsumerHeaders.CORRELATION_ID, "   ");
+
+        filter.doFilter(request, response, chain);
+
+        
assertThat(response.getHeader(ConsumerHeaders.CORRELATION_ID)).isNotBlank();
+        
assertThat(response.getHeader(ConsumerHeaders.CORRELATION_ID).trim()).isNotEmpty();
+    }
+
+    @Test
+    void replacesOversizedInboundHeader() throws Exception {
+        request.addHeader(ConsumerHeaders.CORRELATION_ID, 
"a".repeat(CorrelationConstants.MAX_LENGTH + 1));
+
+        filter.doFilter(request, response, chain);
+
+        String outbound = response.getHeader(ConsumerHeaders.CORRELATION_ID);
+        assertThat(outbound).isNotBlank();
+        
assertThat(outbound.length()).isLessThanOrEqualTo(CorrelationConstants.MAX_LENGTH);
+        assertThat(outbound).doesNotStartWith("aaa");

Review Comment:
   `replacesOversizedInboundHeader` is flaky: `resolveCorrelationId` generates 
a random UUID, so the assertion `doesNotStartWith("aaa")` can fail 
nondeterministically (rarely, but possible). Prefer deterministic assertions 
that prove the oversized inbound value was not reused or truncated.



##########
consumer/src/main/java/org/apache/fineract/consumer/infrastructure/web/data/CorrelationConstants.java:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.fineract.consumer.infrastructure.web.data;
+
+import java.util.regex.Pattern;
+
+public final class CorrelationConstants {
+
+    private CorrelationConstants() {
+    }
+
+    /** SLF4J MDC key used for request correlation. */
+    public static final String MDC_KEY = "correlationId";
+
+    /**
+     * Maximum accepted length for an inbound correlation identifier. Oversized
+     * values are discarded and replaced with a generated id.
+     */
+    public static final int MAX_LENGTH = 128;
+

Review Comment:
   `MAX_LENGTH` is set to 128, but the existing audit DB column for 
`correlation_id` is length 64 (`consumer/.../AuditEvent.java` @Column(length = 
64)). If correlation IDs are later propagated into audit persistence (as 
suggested by the docs), accepting up to 128 will either require a schema change 
or will risk persistence errors/truncation. It would be better to align the 
allowed inbound length with the tightest downstream constraint (or 
document/plan a migration).



-- 
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