|
1 | 1 | package com.marklogic.appdeployer.command.security; |
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
3 | 5 | import com.marklogic.appdeployer.ConfigDir; |
4 | 6 | import com.marklogic.appdeployer.command.AbstractManageResourceTest; |
5 | 7 | import com.marklogic.appdeployer.command.Command; |
|
8 | 10 | import com.marklogic.client.DatabaseClient; |
9 | 11 | import com.marklogic.client.DatabaseClientFactory; |
10 | 12 | import com.marklogic.mgmt.ManageClient; |
| 13 | +import com.marklogic.mgmt.api.API; |
| 14 | +import com.marklogic.mgmt.api.security.Amp; |
11 | 15 | import com.marklogic.mgmt.resource.ResourceManager; |
12 | 16 | import com.marklogic.mgmt.resource.security.AmpManager; |
| 17 | +import com.marklogic.mgmt.util.ObjectMapperFactory; |
13 | 18 | import org.junit.jupiter.api.Test; |
14 | 19 |
|
15 | 20 | import java.io.File; |
| 21 | +import java.util.List; |
16 | 22 |
|
17 | 23 | import static org.junit.jupiter.api.Assertions.*; |
18 | 24 |
|
19 | 25 | public class ManageAmpsTest extends AbstractManageResourceTest { |
20 | 26 |
|
| 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 | + |
21 | 64 | @Test |
22 | 65 | public void ampLoadedBeforeModules() { |
23 | 66 | appConfig.setRestPort(8004); |
|
0 commit comments