[ 
https://issues.apache.org/jira/browse/KNOX-3452?focusedWorklogId=1041905&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1041905
 ]

ASF GitHub Bot logged work on KNOX-3452:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 16/Sep/26 07:47
            Start Date: 16/Sep/26 07:47
    Worklog Time Spent: 10m 
      Work Description: hanicz commented on code in PR #1393:
URL: https://github.com/apache/knox/pull/1393#discussion_r4023651079


##########
gateway-util-common/src/test/java/org/apache/knox/gateway/util/MimeTypesTest.java:
##########
@@ -0,0 +1,271 @@
+/*
+ * 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.knox.gateway.util;
+
+import jakarta.activation.MimeType;
+import org.junit.Test;
+
+import java.nio.charset.StandardCharsets;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class MimeTypesTest {
+
+  @Test
+  public void testCreateReturnsNullForNullBaseType() {
+    assertThat(MimeTypes.create(null, StandardCharsets.UTF_8.name()), 
nullValue());
+  }
+
+  @Test
+  public void testCreateAddsEncodingWhenCharsetIsMissing() {
+    MimeType type = MimeTypes.create("application/json", 
StandardCharsets.UTF_8.name());
+
+    assertThat(type.getBaseType(), is("application/json"));
+    assertThat(MimeTypes.getCharset(type, null), 
is(StandardCharsets.UTF_8.name()));
+  }
+
+  @Test
+  public void testCreateAddsCustomEncodingWhenCharsetIsMissing() {
+    MimeType type = MimeTypes.create("text/plain", "ISO-8859-1");
+
+    assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+  }
+
+  @Test
+  public void testCreateSupportsWildcardMimeType() {
+    MimeType type = MimeTypes.create("text/*", StandardCharsets.UTF_8.name());
+
+    assertThat(type.getBaseType(), is("text/*"));
+    assertThat(MimeTypes.getCharset(type, null), 
is(StandardCharsets.UTF_8.name()));
+  }
+
+  @Test
+  public void testCreatePreservesExistingCharset() {
+    MimeType type = MimeTypes.create("application/json; charset=ISO-8859-1", 
StandardCharsets.UTF_8.name());
+
+    assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+  }
+
+  @Test
+  public void testCreateReadsQuotedCharsetValue() {
+    MimeType type = MimeTypes.create("text/plain; charset=\"ISO-8859-1\"", 
null);
+
+    assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+  }
+
+  @Test
+  public void testCreatePreservesOtherMimeTypeParameters() {
+    MimeType type = MimeTypes.create("application/json; version=1", 
StandardCharsets.UTF_8.name());
+
+    assertThat(type.getParameter("version"), is("1"));
+    assertThat(MimeTypes.getCharset(type, null), 
is(StandardCharsets.UTF_8.name()));
+  }
+
+  @Test
+  public void testCreatePreservesOtherParametersWhenCharsetAlreadyExists() {
+    MimeType type = MimeTypes.create(
+        "application/json; version=1; charset=ISO-8859-1", 
StandardCharsets.UTF_8.name());
+
+    assertThat(type.getParameter("version"), is("1"));
+    assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+  }
+
+  @Test
+  public void 
testCreateRecognizesCharsetParameterRegardlessOfParameterNameCase() {
+    MimeType type = MimeTypes.create("text/plain; CHARSET=ISO-8859-1", 
StandardCharsets.UTF_8.name());
+
+    assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+  }
+
+  @Test
+  public void testCreateLeavesCharsetUnsetWhenEncodingIsNull() {
+    MimeType type = MimeTypes.create("application/json", null);
+
+    assertThat(MimeTypes.getCharset(type, null), nullValue());
+  }
+
+  @Test
+  public void testCreateLeavesCharsetUnsetForWildcardTypeWhenEncodingIsNull() {
+    MimeType type = MimeTypes.create("*/*", null);
+
+    assertThat(MimeTypes.getCharset(type, null), nullValue());
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testCreateRejectsMalformedMimeType() {
+    MimeTypes.create("not-a-mime-type", null);
+  }
+
+  @Test
+  public void testCreateIncludesOriginalMimeTypeInParseExceptionMessage() {

Review Comment:
   This is a duplicate of `testCreateRejectsMalformedMimeType`. Use 
`assertThrows` and you can validate the message.



##########
gateway-util-common/src/test/java/org/apache/knox/gateway/util/MimeTypesTest.java:
##########
@@ -0,0 +1,271 @@
+/*
+ * 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.knox.gateway.util;
+
+import jakarta.activation.MimeType;
+import org.junit.Test;
+
+import java.nio.charset.StandardCharsets;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class MimeTypesTest {
+
+  @Test
+  public void testCreateReturnsNullForNullBaseType() {
+    assertThat(MimeTypes.create(null, StandardCharsets.UTF_8.name()), 
nullValue());
+  }
+
+  @Test
+  public void testCreateAddsEncodingWhenCharsetIsMissing() {
+    MimeType type = MimeTypes.create("application/json", 
StandardCharsets.UTF_8.name());
+
+    assertThat(type.getBaseType(), is("application/json"));
+    assertThat(MimeTypes.getCharset(type, null), 
is(StandardCharsets.UTF_8.name()));
+  }
+
+  @Test
+  public void testCreateAddsCustomEncodingWhenCharsetIsMissing() {
+    MimeType type = MimeTypes.create("text/plain", "ISO-8859-1");
+
+    assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+  }
+
+  @Test
+  public void testCreateSupportsWildcardMimeType() {
+    MimeType type = MimeTypes.create("text/*", StandardCharsets.UTF_8.name());
+
+    assertThat(type.getBaseType(), is("text/*"));
+    assertThat(MimeTypes.getCharset(type, null), 
is(StandardCharsets.UTF_8.name()));
+  }
+
+  @Test
+  public void testCreatePreservesExistingCharset() {
+    MimeType type = MimeTypes.create("application/json; charset=ISO-8859-1", 
StandardCharsets.UTF_8.name());
+
+    assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+  }
+
+  @Test
+  public void testCreateReadsQuotedCharsetValue() {
+    MimeType type = MimeTypes.create("text/plain; charset=\"ISO-8859-1\"", 
null);
+
+    assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+  }
+
+  @Test
+  public void testCreatePreservesOtherMimeTypeParameters() {
+    MimeType type = MimeTypes.create("application/json; version=1", 
StandardCharsets.UTF_8.name());
+
+    assertThat(type.getParameter("version"), is("1"));
+    assertThat(MimeTypes.getCharset(type, null), 
is(StandardCharsets.UTF_8.name()));
+  }
+
+  @Test
+  public void testCreatePreservesOtherParametersWhenCharsetAlreadyExists() {
+    MimeType type = MimeTypes.create(
+        "application/json; version=1; charset=ISO-8859-1", 
StandardCharsets.UTF_8.name());
+
+    assertThat(type.getParameter("version"), is("1"));
+    assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+  }
+
+  @Test
+  public void 
testCreateRecognizesCharsetParameterRegardlessOfParameterNameCase() {
+    MimeType type = MimeTypes.create("text/plain; CHARSET=ISO-8859-1", 
StandardCharsets.UTF_8.name());
+
+    assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+  }
+
+  @Test
+  public void testCreateLeavesCharsetUnsetWhenEncodingIsNull() {
+    MimeType type = MimeTypes.create("application/json", null);
+
+    assertThat(MimeTypes.getCharset(type, null), nullValue());
+  }
+
+  @Test
+  public void testCreateLeavesCharsetUnsetForWildcardTypeWhenEncodingIsNull() {
+    MimeType type = MimeTypes.create("*/*", null);
+
+    assertThat(MimeTypes.getCharset(type, null), nullValue());
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testCreateRejectsMalformedMimeType() {
+    MimeTypes.create("not-a-mime-type", null);
+  }
+
+  @Test
+  public void testCreateIncludesOriginalMimeTypeInParseExceptionMessage() {
+    try {
+      MimeTypes.create("not-a-mime-type", null);
+    } catch (IllegalArgumentException e) {
+      assertThat(e.getMessage(), is("not-a-mime-type"));
+      return;
+    }
+    throw new AssertionError("Expected an IllegalArgumentException");
+  }
+
+  @Test
+  public void testCreateRejectsEmptyMimeType() {

Review Comment:
   Use `expected` or `assertThrows` please.



##########
gateway-util-common/src/test/java/org/apache/knox/gateway/util/MimeTypesTest.java:
##########
@@ -0,0 +1,271 @@
+/*
+ * 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.knox.gateway.util;
+
+import jakarta.activation.MimeType;
+import org.junit.Test;
+
+import java.nio.charset.StandardCharsets;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class MimeTypesTest {
+
+  @Test
+  public void testCreateReturnsNullForNullBaseType() {
+    assertThat(MimeTypes.create(null, StandardCharsets.UTF_8.name()), 
nullValue());
+  }
+
+  @Test
+  public void testCreateAddsEncodingWhenCharsetIsMissing() {
+    MimeType type = MimeTypes.create("application/json", 
StandardCharsets.UTF_8.name());
+
+    assertThat(type.getBaseType(), is("application/json"));
+    assertThat(MimeTypes.getCharset(type, null), 
is(StandardCharsets.UTF_8.name()));
+  }
+
+  @Test
+  public void testCreateAddsCustomEncodingWhenCharsetIsMissing() {
+    MimeType type = MimeTypes.create("text/plain", "ISO-8859-1");
+
+    assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+  }
+
+  @Test
+  public void testCreateSupportsWildcardMimeType() {
+    MimeType type = MimeTypes.create("text/*", StandardCharsets.UTF_8.name());
+
+    assertThat(type.getBaseType(), is("text/*"));
+    assertThat(MimeTypes.getCharset(type, null), 
is(StandardCharsets.UTF_8.name()));
+  }
+
+  @Test
+  public void testCreatePreservesExistingCharset() {
+    MimeType type = MimeTypes.create("application/json; charset=ISO-8859-1", 
StandardCharsets.UTF_8.name());
+
+    assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+  }
+
+  @Test
+  public void testCreateReadsQuotedCharsetValue() {
+    MimeType type = MimeTypes.create("text/plain; charset=\"ISO-8859-1\"", 
null);
+
+    assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+  }
+
+  @Test
+  public void testCreatePreservesOtherMimeTypeParameters() {
+    MimeType type = MimeTypes.create("application/json; version=1", 
StandardCharsets.UTF_8.name());
+
+    assertThat(type.getParameter("version"), is("1"));
+    assertThat(MimeTypes.getCharset(type, null), 
is(StandardCharsets.UTF_8.name()));
+  }
+
+  @Test
+  public void testCreatePreservesOtherParametersWhenCharsetAlreadyExists() {
+    MimeType type = MimeTypes.create(
+        "application/json; version=1; charset=ISO-8859-1", 
StandardCharsets.UTF_8.name());
+
+    assertThat(type.getParameter("version"), is("1"));
+    assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+  }
+
+  @Test
+  public void 
testCreateRecognizesCharsetParameterRegardlessOfParameterNameCase() {

Review Comment:
   Where exactly is the case logic here?





Issue Time Tracking
-------------------

    Worklog Id:     (was: 1041905)
    Time Spent: 50m  (was: 40m)

> Add comprehensive unit tests for MimeTypes
> ------------------------------------------
>
>                 Key: KNOX-3452
>                 URL: https://issues.apache.org/jira/browse/KNOX-3452
>             Project: Apache Knox
>          Issue Type: Task
>            Reporter: Raghav Maheshwari
>            Priority: Major
>          Time Spent: 50m
>  Remaining Estimate: 0h
>
> Add comprehensive unit test coverage for 
> org.apache.knox.gateway.util.MimeTypes.
> The tests should cover MIME type creation, charset handling, invalid input, 
> charset fallback behavior, MIME parameter preservation, wildcard MIME types, 
> normalization, and default charset lookup.
> Test coverage:
> •Null MIME type creation
> •Valid MIME type creation
> •Custom charset insertion
> •UTF-8 charset insertion
> •Existing charset preservation
> •Quoted charset values
> •Case-insensitive charset parameter names
> •Preservation of additional MIME parameters
> •Wildcard MIME types
> •Malformed, empty, and whitespace-only MIME types
> •Charset lookup with explicit and fallback values
> •Charset lookup with null MIME types
> •Adding and replacing charset parameters
> •Preservation of parameters when setting charset
> •Default charset lookup for supported MIME types
> •Case and whitespace normalization
> •Unknown, empty, parameterized, and structured MIME types



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to