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

Commit c44af25

Browse files
committed
#449 Deleting amps on JS functions now works
1 parent 62d4543 commit c44af25

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/main/java/com/marklogic/mgmt/resource/security/AmpManager.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ protected String[] getUpdateResourceParams(String payload) {
100100
AmpParams ampParams = getAmpParams(payload);
101101
params.add("document-uri");
102102
params.add(ampParams.documentUri);
103+
// Building params for a DELETE requires namespace= in case namespace does not exist, but testing shows that
104+
// this is not required for an update, even though the docs for a PUT on an amp indicate that namespace is
105+
// required. The ManageAmpsTest suggests otherwise.
103106
if (ampParams.namespace != null) {
104107
params.add("namespace");
105108
params.add(ampParams.namespace);
@@ -125,9 +128,13 @@ protected String[] getDeleteResourceParams(String payload) {
125128
AmpParams ampParams = getAmpParams(payload);
126129
params.add("document-uri");
127130
params.add(ampParams.documentUri);
131+
// The DELETE endpoint requires 'namespace=' to be passed in case no namespace is set for an amp, which will
132+
// always be the case for an amp on a JS function
133+
params.add("namespace");
128134
if (ampParams.namespace != null) {
129-
params.add("namespace");
130135
params.add(ampParams.namespace);
136+
} else {
137+
params.add("");
131138
}
132139
if (ampParams.modulesDatabase != null) {
133140
params.add("modules-database");

src/test/java/com/marklogic/appdeployer/command/security/ManageAmpsTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.marklogic.appdeployer.command.security;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
35
import com.marklogic.appdeployer.ConfigDir;
46
import com.marklogic.appdeployer.command.AbstractManageResourceTest;
57
import com.marklogic.appdeployer.command.Command;
@@ -8,16 +10,57 @@
810
import com.marklogic.client.DatabaseClient;
911
import com.marklogic.client.DatabaseClientFactory;
1012
import com.marklogic.mgmt.ManageClient;
13+
import com.marklogic.mgmt.api.API;
14+
import com.marklogic.mgmt.api.security.Amp;
1115
import com.marklogic.mgmt.resource.ResourceManager;
1216
import com.marklogic.mgmt.resource.security.AmpManager;
17+
import com.marklogic.mgmt.util.ObjectMapperFactory;
1318
import org.junit.jupiter.api.Test;
1419

1520
import java.io.File;
21+
import java.util.List;
1622

1723
import static org.junit.jupiter.api.Assertions.*;
1824

1925
public class ManageAmpsTest extends AbstractManageResourceTest {
2026

27+
@Test
28+
void updateAndDeleteJavascriptAmp() throws Exception {
29+
Amp amp = new Amp(new API(manageClient), "aaa-function");
30+
amp.setDocumentUri("/some/module.sjs");
31+
amp.setModulesDatabase("Modules");
32+
amp.addRole("rest-reader");
33+
34+
AmpManager mgr = new AmpManager(manageClient);
35+
assertFalse(mgr.ampExists(amp.getJson()));
36+
37+
mgr.save(amp.getJson());
38+
assertTrue(mgr.ampExists(amp.getJson()));
39+
40+
ObjectMapper mapper = new ObjectMapper();
41+
try {
42+
JsonNode ampJson = mapper.readTree(mgr.getAsJson("aaa-function", "document-uri", "/some/module.sjs", "modules-database", "Modules"));
43+
assertEquals(1, ampJson.get("role").size());
44+
assertEquals("rest-reader", ampJson.get("role").get(0).asText());
45+
46+
amp.getRole().add("rest-writer");
47+
mgr.save(amp.getJson());
48+
49+
ampJson = mapper.readTree(mgr.getAsJson("aaa-function", "document-uri", "/some/module.sjs", "modules-database", "Modules"));
50+
assertEquals(2, ampJson.get("role").size());
51+
52+
ampJson.get("role").iterator().forEachRemaining(node -> {
53+
String role = node.asText();
54+
assertTrue("rest-reader".equals(role) || "rest-writer".equals(role));
55+
});
56+
} finally {
57+
mgr.delete(amp.getJson());
58+
assertFalse(mgr.ampExists(amp.getJson()), "The amp should have been deleted, even though it does not have a " +
59+
"namespace value. Per the Manage API docs, namespace is a required parameter and thus it must still be " +
60+
"defined as 'namespace='.");
61+
}
62+
}
63+
2164
@Test
2265
public void ampLoadedBeforeModules() {
2366
appConfig.setRestPort(8004);

0 commit comments

Comments
 (0)