Skip to content

Commit 97106f1

Browse files
committed
Update REST port when configuration replaced
1 parent 7092d76 commit 97106f1

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/main/java/io/prometheus/wls/rest/ConfigurationServlet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private void configure(PostAction postAction, List<FileItem> fileItems) throws I
9999
}
100100
}
101101

102-
private class PostAction {
102+
private static class PostAction {
103103
// The action to take. May be either "replace" or "append"
104104
private String effect = DEFAULT_ACTION;
105105
private ExporterConfig uploadedConfig;

src/main/java/io/prometheus/wls/rest/domain/ExporterConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ public void append(ExporterConfig config2) {
285285
public void replace(ExporterConfig config2) {
286286
this.metricsNameSnakeCase = config2.metricsNameSnakeCase;
287287
this.useDomainQualifier = config2.useDomainQualifier;
288+
this.restPort = config2.restPort;
288289
MBeanSelector[] newQueries = config2.getQueries();
289290
this.queries = Arrays.copyOf(newQueries, newQueries.length);
290291
this.domainName = null;

src/test/java/io/prometheus/wls/rest/ConfigurationServletTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
*/
3939
public class ConfigurationServletTest {
4040

41+
private final static int REST_PORT = 7651;
42+
4143
private final WebClientFactoryStub factory = createStrictStub(WebClientFactoryStub.class);
4244
private final ConfigurationServlet servlet = new ConfigurationServlet(factory);
4345
private final HttpServletResponseStub response = createServletResponse();
@@ -77,6 +79,16 @@ public void servletAnnotationIndicatesConfigurationPage() {
7779
" key: name\n" +
7880
" values: [sample1, sample2]\n";
7981

82+
private static final String CONFIGURATION_WITH_REST_PORT =
83+
"host: " + HttpServletRequestStub.HOST + "\n" +
84+
"port: " + HttpServletRequestStub.PORT + "\n" +
85+
"restPort: " + REST_PORT + "\n" +
86+
"queries:\n" + "" +
87+
"- groups:\n" +
88+
" prefix: new_\n" +
89+
" key: name\n" +
90+
" values: [sample1, sample2]\n";
91+
8092
private static final String ADDED_CONFIGURATION =
8193
"host: localhost\n" +
8294
"port: 7001\n" +
@@ -156,6 +168,20 @@ private String asString(HttpEntity entity) throws IOException {
156168
return new String(result);
157169
}
158170

171+
@Test
172+
public void afterUploadWithNewRestPort_useItInQueryUrl() throws Exception {
173+
servlet.doPost(createUploadRequest(createEncodedForm("replace", CONFIGURATION_WITH_REST_PORT)), response);
174+
175+
assertThat(LiveConfiguration.getAuthenticationUrl(), containsString(Integer.toString(REST_PORT)));
176+
}
177+
178+
@Test
179+
public void afterUsingRestPortAndReplaceWithoutIt_queryUrlUsesDefaultPort() throws Exception {
180+
servlet.doPost(createUploadRequest(createEncodedForm("replace", CONFIGURATION_WITH_REST_PORT)), response);
181+
servlet.doPost(createUploadRequest(createEncodedForm("replace", CONFIGURATION)), response);
182+
183+
assertThat(LiveConfiguration.getAuthenticationUrl(), containsString(Integer.toString(HttpServletRequestStub.PORT)));
184+
}
159185

160186
@Test
161187
public void afterUploadWithAppend_useBothConfiguration() throws Exception {

src/test/java/io/prometheus/wls/rest/domain/ExporterConfigTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,23 @@ public void includeSnakeCaseTrueSettingInToString() {
246246
" key: applicationName\n" +
247247
" values: [pendingRequests, completedRequests, stuckThreadCount]\n";
248248

249+
@Test
250+
public void includeRestPortSettingInToString() {
251+
ExporterConfig config = loadFromString(REST_PORT_CONFIG);
252+
253+
assertThat(config.toString(), equalToIgnoringWhiteSpace(REST_PORT_CONFIG));
254+
}
255+
256+
private static final String REST_PORT_CONFIG =
257+
"restPort: 1234\n" +
258+
"queries:\n" +
259+
"- applicationRuntimes:\n" +
260+
" key: name\n" +
261+
" workManagerRuntimes:\n" +
262+
" prefix: workmanager_\n" +
263+
" key: applicationName\n" +
264+
" values: [pendingRequests, completedRequests, stuckThreadCount]\n";
265+
249266
@Test
250267
public void afterAppend_configHasOriginalDestination() {
251268
ExporterConfig config = getAppendedConfiguration(SERVLET_CONFIG, WORK_MANAGER_CONFIG);
@@ -279,6 +296,12 @@ public void afterAppend_configHasOriginalSnakeCase() {
279296
assertThat(getAppendedConfiguration(WORK_MANAGER_CONFIG, SERVLET_CONFIG).getMetricsNameSnakeCase(), is(true));
280297
}
281298

299+
@Test
300+
public void afterAppend_configHasOriginalRestPort() {
301+
assertThat(getAppendedConfiguration(SERVLET_CONFIG, REST_PORT_CONFIG).getRestPort(), nullValue());
302+
assertThat(getAppendedConfiguration(REST_PORT_CONFIG, SERVLET_CONFIG).getRestPort(), equalTo(1234));
303+
}
304+
282305
@Test
283306
public void afterAppend_configHasOriginalQuery() {
284307
ExporterConfig config = getAppendedConfiguration(SERVLET_CONFIG, WORK_MANAGER_CONFIG);
@@ -328,6 +351,12 @@ public void afterReplace_configHasChangedSnakeCase() {
328351
assertThat(getReplacedConfiguration(WORK_MANAGER_CONFIG, SERVLET_CONFIG).getMetricsNameSnakeCase(), is(false));
329352
}
330353

354+
@Test
355+
public void afterReplace_configHasChangedRestPort() {
356+
assertThat(getReplacedConfiguration(SERVLET_CONFIG, REST_PORT_CONFIG).getRestPort(), equalTo(1234));
357+
assertThat(getReplacedConfiguration(REST_PORT_CONFIG, SERVLET_CONFIG).getRestPort(), nullValue());
358+
}
359+
331360
@Test
332361
public void afterReplace_configHasChangedDomainQualifier() {
333362
assertThat(getReplacedConfiguration(SERVLET_CONFIG, DOMAIN_QUALIFIER_CONFIG).useDomainQualifier(), is(true));
@@ -568,7 +597,7 @@ private JsonObject getJsonResponse(String jsonString) {
568597

569598
@SuppressWarnings("unused")
570599
static class QueryHierarchyMatcher extends TypeSafeDiagnosingMatcher<ExporterConfig> {
571-
private String[] selectorKeys;
600+
private final String[] selectorKeys;
572601

573602
private QueryHierarchyMatcher(String[] selectorKeys) {
574603
this.selectorKeys = selectorKeys;

0 commit comments

Comments
 (0)