Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 94f49c7

Browse files
authored
Merge pull request #508 from BillFarber/task/fixInitWIthoutUsername
Can now call mlInit and mlInstallAdmin without mlUsername or mlPassword
2 parents 40bb2dc + 0fd8a7d commit 94f49c7

File tree

3 files changed

+101
-8
lines changed

3 files changed

+101
-8
lines changed

src/main/java/com/marklogic/mgmt/admin/AdminManager.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.marklogic.rest.util.Fragment;
2020
import com.marklogic.rest.util.RestConfig;
2121
import com.marklogic.rest.util.RestTemplateUtil;
22+
import org.apache.commons.lang3.tuple.ImmutablePair;
2223
import org.springframework.core.io.ByteArrayResource;
2324
import org.springframework.core.io.Resource;
2425
import org.springframework.http.*;
@@ -80,8 +81,9 @@ public boolean execute() {
8081
HttpHeaders headers = new HttpHeaders();
8182
headers.setContentType(MediaType.APPLICATION_JSON);
8283
HttpEntity<String> entity = new HttpEntity<>(payload, headers);
84+
ImmutablePair<Boolean, Boolean> credentialsStatus = checkCredentialsAndReplaceNulls(adminConfig);
8385
try {
84-
ResponseEntity<String> response = getRestTemplate().exchange(uri, HttpMethod.POST, entity, String.class);
86+
ResponseEntity<String> response = getRestTemplate().exchange(uri, HttpMethod.POST, entity, String.class);
8587
logger.info("Initialization response: " + response);
8688
// According to http://docs.marklogic.com/REST/POST/admin/v1/init, a 202 is sent back in the event a
8789
// restart is needed. A 400 or 401 will be thrown as an error by RestTemplate.
@@ -98,12 +100,14 @@ public boolean execute() {
98100
logger.error("Caught error, response body: " + body);
99101
throw hcee;
100102
}
101-
}
103+
} finally {
104+
restoreCredentials(adminConfig, credentialsStatus);
105+
}
102106
}
103107
});
104108
}
105109

106-
public void installAdmin() {
110+
public void installAdmin() {
107111
installAdmin(null, null);
108112
}
109113

@@ -130,8 +134,9 @@ public boolean execute() {
130134
HttpHeaders headers = new HttpHeaders();
131135
headers.setContentType(MediaType.APPLICATION_JSON);
132136
HttpEntity<String> entity = new HttpEntity<>(payload, headers);
137+
ImmutablePair<Boolean, Boolean> credentialsStatus = checkCredentialsAndReplaceNulls(adminConfig);
133138
try {
134-
ResponseEntity<String> response = getRestTemplate().exchange(uri, HttpMethod.POST, entity, String.class);
139+
ResponseEntity<String> response = getRestTemplate().exchange(uri, HttpMethod.POST, entity, String.class);
135140
logger.info("Admin installation response: " + response);
136141
// According to http://docs.marklogic.com/REST/POST/admin/v1/init, a 202 is sent back in the event a
137142
// restart is needed. A 400 or 401 will be thrown as an error by RestTemplate.
@@ -143,6 +148,8 @@ public boolean execute() {
143148
return false;
144149
}
145150
throw hcee;
151+
} finally {
152+
restoreCredentials(adminConfig, credentialsStatus);
146153
}
147154
}
148155
});
@@ -323,4 +330,31 @@ public RestTemplate getRestTemplate() {
323330
}
324331
return this.restTemplate;
325332
}
333+
334+
private ImmutablePair<Boolean, Boolean> checkCredentialsAndReplaceNulls(AdminConfig adminConfig) {
335+
boolean setNullUsername = false;
336+
boolean setNullPassword = false;
337+
if (adminConfig.getUsername() == null) {
338+
adminConfig.setUsername("");
339+
setNullUsername = true;
340+
this.restTemplate = null;
341+
}
342+
if (adminConfig.getPassword() == null) {
343+
adminConfig.setPassword("");
344+
setNullPassword = true;
345+
this.restTemplate = null;
346+
}
347+
return new ImmutablePair<>(setNullUsername, setNullPassword);
348+
}
349+
350+
private void restoreCredentials(AdminConfig adminConfig, ImmutablePair<Boolean, Boolean> credentialsStatus) {
351+
if (credentialsStatus.getLeft()) {
352+
adminConfig.setUsername(null);
353+
this.restTemplate = null;
354+
}
355+
if (credentialsStatus.getRight()) {
356+
adminConfig.setPassword(null);
357+
this.restTemplate = null;
358+
}
359+
}
326360
}

src/test/java/com/marklogic/mgmt/admin/InitializeMarkLogicTest.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import org.junit.jupiter.api.Test;
1919

2020
import com.marklogic.mgmt.AbstractMgmtTest;
21+
import org.springframework.web.client.HttpClientErrorException;
22+
23+
import static org.junit.jupiter.api.Assertions.*;
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
2125

2226
public class InitializeMarkLogicTest extends AbstractMgmtTest {
2327

@@ -27,7 +31,33 @@ public class InitializeMarkLogicTest extends AbstractMgmtTest {
2731
* to ensure no errors are thrown from bad JSON.
2832
*/
2933
@Test
30-
public void initAgainstAnAlreadyInitializedMarkLogic() {
31-
adminManager.init();
34+
void initAgainstAnAlreadyInitializedMarkLogic() {
35+
assertDoesNotThrow(() -> adminManager.init());
3236
}
37+
38+
@Test
39+
void withNullUsername() {
40+
String originalUsername = adminConfig.getUsername();
41+
try {
42+
adminConfig.setUsername(null);
43+
HttpClientErrorException exception = assertThrows(HttpClientErrorException.class, () -> adminManager.init());
44+
assertTrue(exception.getMessage().contains("Unauthorized"));
45+
assertEquals(401, exception.getStatusCode().value());
46+
} finally {
47+
adminConfig.setUsername(originalUsername);
48+
}
49+
}
50+
51+
@Test
52+
void withNullPassword() {
53+
String originalPassword = adminConfig.getPassword();
54+
try {
55+
adminConfig.setPassword(null);
56+
HttpClientErrorException exception = assertThrows(HttpClientErrorException.class, () -> adminManager.init());
57+
assertTrue(exception.getMessage().contains("Unauthorized"));
58+
assertEquals(401, exception.getStatusCode().value());
59+
} finally {
60+
adminConfig.setPassword(originalPassword);
61+
}
62+
}
3363
}

src/test/java/com/marklogic/mgmt/admin/InstallAdminTest.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import org.junit.jupiter.api.Test;
1919

2020
import com.marklogic.mgmt.AbstractMgmtTest;
21+
import org.springframework.web.client.HttpClientErrorException;
22+
23+
import static org.junit.jupiter.api.Assertions.*;
2124

2225
public class InstallAdminTest extends AbstractMgmtTest {
2326

@@ -27,7 +30,33 @@ public class InstallAdminTest extends AbstractMgmtTest {
2730
* again. Instead, a message should be logged and ML should not be restarted.
2831
*/
2932
@Test
30-
public void adminAlreadyInstalled() {
31-
adminManager.installAdmin("admin", "admin");
33+
void adminAlreadyInstalled() {
34+
assertDoesNotThrow(() -> adminManager.installAdmin("admin", "admin"));
3235
}
36+
37+
@Test
38+
void withNullUsername() {
39+
String originalUsername = adminConfig.getUsername();
40+
try {
41+
adminConfig.setUsername(null);
42+
HttpClientErrorException exception = assertThrows(HttpClientErrorException.class, () -> adminManager.installAdmin("admin", "admin"));
43+
assertTrue(exception.getMessage().contains("Unauthorized"));
44+
assertEquals(401, exception.getStatusCode().value());
45+
} finally {
46+
adminConfig.setUsername(originalUsername);
47+
}
48+
}
49+
50+
@Test
51+
void withNullPassword() {
52+
String originalPassword = adminConfig.getPassword();
53+
try {
54+
adminConfig.setPassword(null);
55+
HttpClientErrorException exception = assertThrows(HttpClientErrorException.class, () -> adminManager.installAdmin("admin", "admin"));
56+
assertTrue(exception.getMessage().contains("Unauthorized"));
57+
assertEquals(401, exception.getStatusCode().value());
58+
} finally {
59+
adminConfig.setPassword(originalPassword);
60+
}
61+
}
3362
}

0 commit comments

Comments
 (0)