Skip to content

Commit 27c25c4

Browse files
authored
Merge pull request #789 from marklogic/feature/23425-bug
MLE-23425 Better error when ManageConfig is null
2 parents 9b55ac8 + b0f14a3 commit 27c25c4

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

ml-app-deployer/src/main/java/com/marklogic/mgmt/ManageClient.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.springframework.web.client.RestTemplate;
1919

2020
import java.io.IOException;
21-
import java.io.StringReader;
2221
import java.io.StringWriter;
2322
import java.net.URI;
2423
import java.util.ArrayList;
@@ -37,41 +36,39 @@ public class ManageClient extends LoggingObject {
3736
private PayloadParser payloadParser;
3837

3938
public ManageClient(ManageConfig config) {
40-
setManageConfig(config);
39+
this.manageConfig = config;
40+
if (logger.isInfoEnabled()) {
41+
logger.info("Initializing ManageClient with manage config of: {}", config);
42+
}
4143
}
4244

4345
/**
44-
* Uses the given ManageConfig instance to construct a Spring RestTemplate for communicating with the Manage API.
45-
* In addition, if adminUsername on the ManageConfig instance differs from username, then a separate RestTemplate is
46-
* constructed for making calls to the Manage API that need user with the manage-admin and security roles, which is
47-
* often an admin user.
46+
* Deprecated in 6.0.1 with the intention of removing in 7.0.0 so that the underlying ManageConfig can be declared
47+
* as final.
4848
*
49-
* @param config
49+
* @deprecated
5050
*/
51+
@Deprecated(since = "6.0.1", forRemoval = true)
5152
public void setManageConfig(ManageConfig config) {
5253
this.manageConfig = config;
53-
if (logger.isInfoEnabled()) {
54-
logger.info("Initializing ManageClient with manage config of: " + config);
55-
}
5654
}
5755

5856
/**
59-
* Use this when you want to provide your own RestTemplate as opposed to using the one that's constructed via a
60-
* ManageConfig instance.
61-
*
62-
* @param restTemplate
57+
* Deprecated in 6.0.1 as it will not work without a ManageConfig instance being set, which is then unlikely to
58+
* be consistent with the given RestTemplate.
59+
* @deprecated
6360
*/
61+
@Deprecated(since = "6.0.1", forRemoval = true)
6462
public ManageClient(RestTemplate restTemplate) {
6563
this(restTemplate, restTemplate);
6664
}
6765

6866
/**
69-
* Use this when you want to provide your own RestTemplate as opposed to using the one that's constructed via a
70-
* ManageConfig instance.
71-
*
72-
* @param restTemplate
73-
* @param adminRestTemplate
67+
* Deprecated in 6.0.1 as it will not work without a ManageConfig instance being set, which is then unlikely to
68+
* be consistent with the given RestTemplate.
69+
* @deprecated
7470
*/
71+
@Deprecated(since = "6.0.1", forRemoval = true)
7572
public ManageClient(RestTemplate restTemplate, RestTemplate adminRestTemplate) {
7673
this.restTemplate = restTemplate;
7774
this.securityUserRestTemplate = adminRestTemplate;
@@ -257,8 +254,10 @@ public HttpEntity<String> buildXmlEntity(String xml) {
257254

258255
protected void logRequest(String path, String contentType, String method) {
259256
if (logger.isInfoEnabled()) {
260-
String username = String.format("as user '%s' ", manageConfig.getUsername());
261-
logger.info("Sending {} {} request {}to path: {}", contentType, method, username, buildUri(path));
257+
String username = manageConfig != null && StringUtils.hasText(manageConfig.getUsername()) ?
258+
String.format("as user '%s' ", manageConfig.getUsername()) : "";
259+
URI uri = buildUri(path);
260+
logger.info("Sending {} {} request {}to path: {}", contentType, method, username, uri);
262261
}
263262
}
264263

@@ -268,7 +267,8 @@ protected void logSecurityUserRequest(String path, String contentType, String me
268267
if (!"".equals(username)) {
269268
username = String.format("as user '%s' (who should have the 'manage-admin' and 'security' roles) ", username);
270269
}
271-
logger.info("Sending {} {} request {}to path: {}", contentType, method, username, buildUri(path));
270+
URI uri = buildUri(path);
271+
logger.info("Sending {} {} request {}to path: {}", contentType, method, username, uri);
272272
}
273273
}
274274

@@ -307,6 +307,7 @@ private void initializeSecurityUserRestTemplate() {
307307
}
308308

309309
public URI buildUri(String path) {
310+
Objects.requireNonNull(manageConfig, "A ManageConfig instance must be provided");
310311
return manageConfig.buildUri(path);
311312
}
312313

@@ -321,6 +322,7 @@ public ManageConfig getManageConfig() {
321322
return manageConfig;
322323
}
323324

325+
@Deprecated(since = "6.0.1", forRemoval = true)
324326
public void setRestTemplate(RestTemplate restTemplate) {
325327
this.restTemplate = restTemplate;
326328
}
@@ -332,6 +334,7 @@ public RestTemplate getSecurityUserRestTemplate() {
332334
return securityUserRestTemplate;
333335
}
334336

337+
@Deprecated(since = "6.0.1", forRemoval = true)
335338
public void setSecurityUserRestTemplate(RestTemplate restTemplate) {
336339
this.securityUserRestTemplate = restTemplate;
337340
}

ml-app-deployer/src/test/java/com/marklogic/mgmt/ManageClientTest.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
*/
44
package com.marklogic.mgmt;
55

6-
import static org.junit.jupiter.api.Assertions.*;
6+
import com.marklogic.mgmt.resource.databases.DatabaseManager;
77
import org.junit.jupiter.api.Test;
88

9-
public class ManageClientTest {
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertThrows;
11+
12+
class ManageClientTest {
1013

1114
@Test
12-
public void determineUsernameForSecurityUserRequest() {
15+
void determineUsernameForSecurityUserRequest() {
1316
ManageConfig config = new ManageConfig("localhost", 8002, "someone", "someword");
1417
config.setSecurityUsername("admin");
1518
config.setSecurityPassword("admin");
@@ -20,4 +23,15 @@ public void determineUsernameForSecurityUserRequest() {
2023
config.setSecurityUsername(null);
2124
assertEquals("someone", client.determineUsernameForSecurityUserRequest());
2225
}
26+
27+
@Test
28+
void nullManageConfig() {
29+
ManageClient client = new ManageClient(null);
30+
NullPointerException npe = assertThrows(NullPointerException.class, () -> new DatabaseManager(client).getAsXml());
31+
assertEquals("A ManageConfig instance must be provided", npe.getMessage(),
32+
"It's possible to pass in null as the ManageConfig since there's still a setManageConfig method, but that's been " +
33+
"deprecated so that it can be removed in 7.0.0. The goal is to have ManageConfig be final once " +
34+
"it's set, and ideally hidden as well so that the ManageClient is effectively immutable. " +
35+
"In the meantime, we expect a nice error message if the ManageConfig is null.");
36+
}
2337
}

0 commit comments

Comments
 (0)