Skip to content

Commit 4b08938

Browse files
adityamparikhclaude
andcommitted
test(config): use @jsontest for URL normalization tests to get Spring's ObjectMapper
Extract URL normalization tests from SolrConfigTest into a dedicated SolrConfigUrlNormalizationTest annotated with @jsontest, so Spring's auto-configured ObjectMapper is injected rather than using new ObjectMapper(). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: adityamparikh <aditya.m.parikh@gmail.com>
1 parent ca80599 commit 4b08938

2 files changed

Lines changed: 126 additions & 106 deletions

File tree

src/test/java/org/apache/solr/mcp/server/config/SolrConfigTest.java

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@
1818

1919
import static org.junit.jupiter.api.Assertions.*;
2020

21-
import com.fasterxml.jackson.databind.ObjectMapper;
2221
import org.apache.solr.client.solrj.SolrClient;
2322
import org.apache.solr.client.solrj.impl.Http2SolrClient;
2423
import org.apache.solr.mcp.server.TestcontainersConfiguration;
2524
import org.junit.jupiter.api.Test;
26-
import org.junit.jupiter.params.ParameterizedTest;
27-
import org.junit.jupiter.params.provider.CsvSource;
2825
import org.springframework.beans.factory.annotation.Autowired;
2926
import org.springframework.boot.test.context.SpringBootTest;
3027
import org.springframework.context.annotation.Import;
@@ -68,107 +65,4 @@ void testSolrConfigurationProperties() {
6865
properties.url());
6966
}
7067

71-
@ParameterizedTest
72-
@CsvSource({"http://localhost:8983, http://localhost:8983/solr",
73-
"http://localhost:8983/, http://localhost:8983/solr",
74-
"http://localhost:8983/solr, http://localhost:8983/solr",
75-
"http://localhost:8983/solr/, http://localhost:8983/solr",
76-
"http://localhost:8983/custom/solr/, http://localhost:8983/custom/solr"})
77-
void testUrlNormalization(String inputUrl, String expectedUrl) {
78-
// Create a test properties object
79-
SolrConfigurationProperties testProperties = new SolrConfigurationProperties(inputUrl);
80-
81-
// Create SolrConfig instance
82-
SolrConfig solrConfig = new SolrConfig();
83-
84-
// Test URL normalization
85-
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(new ObjectMapper()));
86-
assertNotNull(client);
87-
88-
var httpClient = assertInstanceOf(Http2SolrClient.class, client);
89-
assertEquals(expectedUrl, httpClient.getBaseURL());
90-
91-
// Clean up
92-
try {
93-
client.close();
94-
} catch (Exception e) {
95-
// Ignore close errors in test
96-
}
97-
}
98-
99-
@Test
100-
void testUrlWithoutTrailingSlash() {
101-
// Test URL without trailing slash branch
102-
SolrConfigurationProperties testProperties = new SolrConfigurationProperties("http://localhost:8983");
103-
SolrConfig solrConfig = new SolrConfig();
104-
105-
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(new ObjectMapper()));
106-
Http2SolrClient httpClient = (Http2SolrClient) client;
107-
108-
// Should add trailing slash and solr path
109-
assertEquals("http://localhost:8983/solr", httpClient.getBaseURL());
110-
111-
try {
112-
client.close();
113-
} catch (Exception e) {
114-
// Ignore close errors in test
115-
}
116-
}
117-
118-
@Test
119-
void testUrlWithTrailingSlashButNoSolrPath() {
120-
// Test URL with trailing slash but no solr path branch
121-
SolrConfigurationProperties testProperties = new SolrConfigurationProperties("http://localhost:8983/");
122-
SolrConfig solrConfig = new SolrConfig();
123-
124-
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(new ObjectMapper()));
125-
Http2SolrClient httpClient = (Http2SolrClient) client;
126-
127-
// Should add solr path to existing trailing slash
128-
assertEquals("http://localhost:8983/solr", httpClient.getBaseURL());
129-
130-
try {
131-
client.close();
132-
} catch (Exception e) {
133-
// Ignore close errors in test
134-
}
135-
}
136-
137-
@Test
138-
void testUrlWithSolrPathButNoTrailingSlash() {
139-
// Test URL with solr path but no trailing slash
140-
SolrConfigurationProperties testProperties = new SolrConfigurationProperties("http://localhost:8983/solr");
141-
SolrConfig solrConfig = new SolrConfig();
142-
143-
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(new ObjectMapper()));
144-
Http2SolrClient httpClient = (Http2SolrClient) client;
145-
146-
// Should add trailing slash
147-
assertEquals("http://localhost:8983/solr", httpClient.getBaseURL());
148-
149-
try {
150-
client.close();
151-
} catch (Exception e) {
152-
// Ignore close errors in test
153-
}
154-
}
155-
156-
@Test
157-
void testUrlAlreadyProperlyFormatted() {
158-
// Test URL that's already properly formatted
159-
SolrConfigurationProperties testProperties = new SolrConfigurationProperties("http://localhost:8983/solr/");
160-
SolrConfig solrConfig = new SolrConfig();
161-
162-
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(new ObjectMapper()));
163-
Http2SolrClient httpClient = (Http2SolrClient) client;
164-
165-
// Should remain unchanged
166-
assertEquals("http://localhost:8983/solr", httpClient.getBaseURL());
167-
168-
try {
169-
client.close();
170-
} catch (Exception e) {
171-
// Ignore close errors in test
172-
}
173-
}
17468
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.solr.mcp.server.config;
18+
19+
import static org.junit.jupiter.api.Assertions.*;
20+
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import org.apache.solr.client.solrj.SolrClient;
23+
import org.apache.solr.client.solrj.impl.Http2SolrClient;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.CsvSource;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.autoconfigure.json.JsonTest;
29+
30+
@JsonTest
31+
class SolrConfigUrlNormalizationTest {
32+
33+
@Autowired
34+
private ObjectMapper objectMapper;
35+
36+
@ParameterizedTest
37+
@CsvSource({"http://localhost:8983, http://localhost:8983/solr",
38+
"http://localhost:8983/, http://localhost:8983/solr",
39+
"http://localhost:8983/solr, http://localhost:8983/solr",
40+
"http://localhost:8983/solr/, http://localhost:8983/solr",
41+
"http://localhost:8983/custom/solr/, http://localhost:8983/custom/solr"})
42+
void testUrlNormalization(String inputUrl, String expectedUrl) {
43+
SolrConfigurationProperties testProperties = new SolrConfigurationProperties(inputUrl);
44+
SolrConfig solrConfig = new SolrConfig();
45+
46+
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(objectMapper));
47+
assertNotNull(client);
48+
49+
var httpClient = assertInstanceOf(Http2SolrClient.class, client);
50+
assertEquals(expectedUrl, httpClient.getBaseURL());
51+
52+
try {
53+
client.close();
54+
} catch (Exception e) {
55+
// Ignore close errors in test
56+
}
57+
}
58+
59+
@Test
60+
void testUrlWithoutTrailingSlash() {
61+
SolrConfigurationProperties testProperties = new SolrConfigurationProperties("http://localhost:8983");
62+
SolrConfig solrConfig = new SolrConfig();
63+
64+
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(objectMapper));
65+
Http2SolrClient httpClient = (Http2SolrClient) client;
66+
67+
assertEquals("http://localhost:8983/solr", httpClient.getBaseURL());
68+
69+
try {
70+
client.close();
71+
} catch (Exception e) {
72+
// Ignore close errors in test
73+
}
74+
}
75+
76+
@Test
77+
void testUrlWithTrailingSlashButNoSolrPath() {
78+
SolrConfigurationProperties testProperties = new SolrConfigurationProperties("http://localhost:8983/");
79+
SolrConfig solrConfig = new SolrConfig();
80+
81+
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(objectMapper));
82+
Http2SolrClient httpClient = (Http2SolrClient) client;
83+
84+
assertEquals("http://localhost:8983/solr", httpClient.getBaseURL());
85+
86+
try {
87+
client.close();
88+
} catch (Exception e) {
89+
// Ignore close errors in test
90+
}
91+
}
92+
93+
@Test
94+
void testUrlWithSolrPathButNoTrailingSlash() {
95+
SolrConfigurationProperties testProperties = new SolrConfigurationProperties("http://localhost:8983/solr");
96+
SolrConfig solrConfig = new SolrConfig();
97+
98+
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(objectMapper));
99+
Http2SolrClient httpClient = (Http2SolrClient) client;
100+
101+
assertEquals("http://localhost:8983/solr", httpClient.getBaseURL());
102+
103+
try {
104+
client.close();
105+
} catch (Exception e) {
106+
// Ignore close errors in test
107+
}
108+
}
109+
110+
@Test
111+
void testUrlAlreadyProperlyFormatted() {
112+
SolrConfigurationProperties testProperties = new SolrConfigurationProperties("http://localhost:8983/solr/");
113+
SolrConfig solrConfig = new SolrConfig();
114+
115+
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(objectMapper));
116+
Http2SolrClient httpClient = (Http2SolrClient) client;
117+
118+
assertEquals("http://localhost:8983/solr", httpClient.getBaseURL());
119+
120+
try {
121+
client.close();
122+
} catch (Exception e) {
123+
// Ignore close errors in test
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)