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

Commit 59ac9c2

Browse files
committed
#177 New command for deleting test modules
Merged from 2.x
1 parent 3581bc4 commit 59ac9c2

File tree

8 files changed

+186
-43
lines changed

8 files changed

+186
-43
lines changed

src/main/java/com/marklogic/appdeployer/AppConfig.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public class AppConfig {
9595
private boolean staticCheckLibraryAssets = false;
9696
private boolean bulkLoadAssets = true;
9797
private String moduleTimestampsPath = PropertiesModuleManager.DEFAULT_FILE_PATH;
98+
private boolean deleteTestModules = false;
99+
private String deleteTestModulesPattern = "/test/**";
98100

99101
private String schemasPath;
100102
private ConfigDir configDir;
@@ -766,4 +768,20 @@ public boolean isSortRolesByDependencies() {
766768
public void setSortRolesByDependencies(boolean sortRolesByDependencies) {
767769
this.sortRolesByDependencies = sortRolesByDependencies;
768770
}
771+
772+
public boolean isDeleteTestModules() {
773+
return deleteTestModules;
774+
}
775+
776+
public void setDeleteTestModules(boolean deleteTestModules) {
777+
this.deleteTestModules = deleteTestModules;
778+
}
779+
780+
public String getDeleteTestModulesPattern() {
781+
return deleteTestModulesPattern;
782+
}
783+
784+
public void setDeleteTestModulesPattern(String deleteTestModulesPattern) {
785+
this.deleteTestModulesPattern = deleteTestModulesPattern;
786+
}
769787
}

src/main/java/com/marklogic/appdeployer/DefaultAppConfigFactory.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,18 @@ public AppConfig newAppConfig() {
365365
c.setStaticCheckLibraryAssets(Boolean.parseBoolean(prop));
366366
}
367367

368+
prop = getProperty("mlDeleteTestModules");
369+
if (prop != null) {
370+
logger.info("Delete test modules: " + prop);
371+
c.setDeleteTestModules(Boolean.parseBoolean(prop));
372+
}
373+
374+
prop = getProperty("mlDeleteTestModulesPattern");
375+
if (prop != null) {
376+
logger.info("Delete test modules pattern: " + prop);
377+
c.setDeleteTestModulesPattern(prop);
378+
}
379+
368380
/**
369381
* The following properties are all for generating Entity Services artifacts.
370382
*/

src/main/java/com/marklogic/appdeployer/command/CommandMapBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.marklogic.appdeployer.command.forests.DeployCustomForestsCommand;
2121
import com.marklogic.appdeployer.command.groups.DeployGroupsCommand;
2222
import com.marklogic.appdeployer.command.mimetypes.DeployMimetypesCommand;
23+
import com.marklogic.appdeployer.command.modules.DeleteTestModulesCommand;
2324
import com.marklogic.appdeployer.command.modules.LoadModulesCommand;
2425
import com.marklogic.appdeployer.command.restapis.DeployRestApiServersCommand;
2526
import com.marklogic.appdeployer.command.schemas.LoadSchemasCommand;
@@ -93,6 +94,7 @@ public Map<String, List<Command>> buildCommandMap() {
9394
// Modules
9495
List<Command> moduleCommands = new ArrayList<>();
9596
moduleCommands.add(new LoadModulesCommand());
97+
moduleCommands.add(new DeleteTestModulesCommand());
9698
map.put("mlModuleCommands", moduleCommands);
9799

98100
// Alerting

src/main/java/com/marklogic/appdeployer/command/SortOrderConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public abstract class SortOrderConstants {
3232
// Modules have to be loaded after the REST API server has been updated, for if the deployer is expecting to load
3333
// modules via SSL, then the REST API server must already be configured with a certificate template
3434
public static Integer LOAD_MODULES = 400;
35+
public static Integer DELETE_TEST_MODULES = 410;
3536

3637
// The modules database must exist before we deploy amps
3738
public static Integer DEPLOY_AMPS = 450;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.marklogic.appdeployer.command.modules;
2+
3+
import com.marklogic.appdeployer.AppConfig;
4+
import com.marklogic.appdeployer.command.AbstractCommand;
5+
import com.marklogic.appdeployer.command.CommandContext;
6+
import com.marklogic.client.DatabaseClient;
7+
import com.marklogic.client.DatabaseClientFactory;
8+
9+
public class DeleteModulesCommand extends AbstractCommand {
10+
11+
private String pattern;
12+
private String databaseName;
13+
14+
public DeleteModulesCommand() {
15+
super();
16+
}
17+
18+
public DeleteModulesCommand(String pattern) {
19+
this();
20+
this.pattern = pattern;
21+
}
22+
23+
@Override
24+
public void execute(CommandContext context) {
25+
if (pattern == null || pattern.trim().length() == 0) {
26+
logger.warn("No pattern was specified, so not deleting any modules");
27+
}
28+
29+
AppConfig appConfig = context.getAppConfig();
30+
31+
String dbName = databaseName != null ? databaseName : appConfig.getModulesDatabaseName();
32+
if (logger.isInfoEnabled()) {
33+
logger.info(format("Deleting modules in database '%s' with URIs matching pattern '%s'", dbName, pattern));
34+
}
35+
36+
DatabaseClient client = DatabaseClientFactory.newClient(appConfig.getHost(), appConfig.getAppServicesPort(), dbName,
37+
appConfig.getRestAdminUsername(), appConfig.getRestAdminPassword(), appConfig.getRestAuthentication(),
38+
appConfig.getRestSslContext(), appConfig.getRestSslHostnameVerifier());
39+
40+
String xquery = "for $uri in cts:uri-match('%s') where fn:doc-available($uri) return xdmp:document-delete($uri)";
41+
try {
42+
client.newServerEval().xquery(format(xquery, pattern)).evalAs(String.class);
43+
if (logger.isInfoEnabled()) {
44+
logger.info("Finished deleting modules");
45+
}
46+
} finally {
47+
client.release();
48+
}
49+
}
50+
51+
public void setPattern(String pattern) {
52+
this.pattern = pattern;
53+
}
54+
55+
public void setDatabaseName(String databaseName) {
56+
this.databaseName = databaseName;
57+
}
58+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.marklogic.appdeployer.command.modules;
2+
3+
import com.marklogic.appdeployer.command.CommandContext;
4+
import com.marklogic.appdeployer.command.SortOrderConstants;
5+
6+
/**
7+
* Subclass that is primarily intended for current Roxy users that are accustomed to having test-only modules deployed
8+
* under the "/test" path.
9+
*/
10+
public class DeleteTestModulesCommand extends DeleteModulesCommand {
11+
12+
public final static String DEFAULT_TEST_MODULES_PATTERN = "/test/**";
13+
14+
public DeleteTestModulesCommand() {
15+
this(DEFAULT_TEST_MODULES_PATTERN);
16+
}
17+
18+
public DeleteTestModulesCommand(String pattern) {
19+
super(pattern);
20+
setExecuteSortOrder(SortOrderConstants.DELETE_TEST_MODULES);
21+
}
22+
23+
@Override
24+
public void execute(CommandContext context) {
25+
if (context.getAppConfig().isDeleteTestModules()) {
26+
String pattern = context.getAppConfig().getDeleteTestModulesPattern();
27+
if (pattern != null) {
28+
setPattern(pattern);
29+
}
30+
super.execute(context);
31+
}
32+
}
33+
}

src/test/java/com/marklogic/appdeployer/DefaultAppConfigFactoryTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public void allProperties() {
103103
p.setProperty("mlUseRoxyTokenPrefix", "false");
104104
p.setProperty("mlModulePaths", "path1,path2,path3");
105105
p.setProperty("mlModuleTimestampsPath", "custom/timestamps/path.properties");
106+
p.setProperty("mlDeleteTestModules", "true");
107+
p.setProperty("mlDeleteTestModulesPattern", "/some/pattern");
106108

107109
p.setProperty("mlModelsPath", "ml/models");
108110
p.setProperty("mlInstanceConverterPath", "ext/my/path");
@@ -149,6 +151,8 @@ public void allProperties() {
149151
assertEquals((Integer) 8123, config.getAppServicesPort());
150152
assertFalse(config.isReplaceTokensInModules());
151153
assertFalse(config.isUseRoxyTokenPrefix());
154+
assertTrue(config.isDeleteTestModules());
155+
assertEquals("/some/pattern", config.getDeleteTestModulesPattern());
152156

153157
assertEquals("ml/models", config.getModelsPath());
154158
assertEquals("ext/my/path", config.getInstanceConverterPath());

src/test/java/com/marklogic/appdeployer/command/modules/LoadModulesTest.java

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414

1515
public class LoadModulesTest extends AbstractAppDeployerTest {
1616

17-
private XccTemplate xccTemplate;
17+
private XccTemplate xccTemplate;
1818

19-
@Before
20-
public void setup() {
21-
xccTemplate = new XccTemplate(format("xcc://%s:%s@%s:8000/%s", "admin", "admin", appConfig.getHost(),
22-
appConfig.getModulesDatabaseName()));
23-
}
19+
@Before
20+
public void setup() {
21+
xccTemplate = new XccTemplate(format("xcc://%s:%s@%s:8000/%s", appConfig.getRestAdminUsername(),
22+
appConfig.getRestAdminPassword(), appConfig.getHost(), appConfig.getModulesDatabaseName()));
23+
}
2424

25-
@After
26-
public void teardown() {
27-
undeploySampleApp();
28-
}
25+
@After
26+
public void teardown() {
27+
undeploySampleApp();
28+
}
2929

3030
@Test
3131
public void loadModulesWithStaticCheck() {
@@ -48,19 +48,21 @@ public void loadModulesWithStaticCheck() {
4848
assertTrue("Loading modules with 2.11.0 of ml-javaclient-util defaults to bulk loading, so static checking should as well; message: " + message,
4949
message.contains("Unexpected token syntax error"));
5050
assertTrue(message.contains("in /ext/bad.xqy, on line 2"));
51+
} finally {
52+
initializeAppDeployer(new DeployRestApiServersCommand(true));
5153
}
5254
}
5355

5456
@Test
5557
public void customModuleTimestampsPath() {
56-
String path = "build/custom-path.properties";
57-
File customFile = new File(path);
58-
customFile.mkdirs();
59-
if (customFile.exists()) {
60-
customFile.delete();
61-
}
62-
63-
appConfig.setModuleTimestampsPath("build/custom-path.properties");
58+
String path = "build/custom-path.properties";
59+
File customFile = new File(path);
60+
customFile.mkdirs();
61+
if (customFile.exists()) {
62+
customFile.delete();
63+
}
64+
65+
appConfig.setModuleTimestampsPath("build/custom-path.properties");
6466
initializeAppDeployer(new DeployRestApiServersCommand(true), new LoadModulesCommand());
6567
appDeployer.deploy(appConfig);
6668
assertTrue("The custom file should have been created when the modules were loaded", customFile.exists());
@@ -127,7 +129,6 @@ public void loadModulesWithAssetFileFilterAndTokenReplacement() {
127129
Fragment f = parse(xml);
128130
f.assertElementValue("/test/color", "red");
129131
f.assertElementValue("/test/description", "red description");
130-
131132
}
132133

133134
@Test
@@ -145,35 +146,49 @@ public void testServerExists() {
145146
}
146147
}
147148

148-
private void assertModuleExistsWithDefaultPermissions(String message, String uri) {
149-
assertEquals(message, "true", xccTemplate.executeAdhocQuery(format("fn:doc-available('%s')", uri)));
150-
assertDefaultPermissionsExists(uri);
151-
}
149+
@Test
150+
public void deleteTestModules() {
151+
appConfig.setDeleteTestModules(true);
152+
appConfig.setDeleteTestModulesPattern("/ext/lib/*.xqy");
152153

153-
/**
154-
* Apparently, the REST API won't let you remove these 3 default permissions, they're always present.
155-
*
156-
* And, now that we're loading modules via the REST API by default, rest-reader/read and rest-writer/update are
157-
* always present, at least on 8.0-6.3 and 9.0-1.1, which seems like a bug.
158-
*/
159-
private void assertDefaultPermissionsExists(String uri) {
160-
PermissionsFragment perms = getDocumentPermissions(uri, xccTemplate);
161-
perms.assertPermissionCount(5);
162-
perms.assertPermissionExists("rest-admin", "read");
163-
perms.assertPermissionExists("rest-admin", "update");
164-
perms.assertPermissionExists("rest-extension-user", "execute");
154+
initializeAppDeployer(new DeployRestApiServersCommand(true), buildLoadModulesCommand(),
155+
new DeleteTestModulesCommand());
156+
appDeployer.deploy(appConfig);
157+
158+
String xquery = "fn:count(cts:uri-match('/ext/**.xqy'))";
159+
assertEquals(1, Integer.parseInt(xccTemplate.executeAdhocQuery(xquery)));
160+
}
161+
162+
private void assertModuleExistsWithDefaultPermissions(String message, String uri) {
163+
assertEquals(message, "true", xccTemplate.executeAdhocQuery(format("fn:doc-available('%s')", uri)));
164+
assertDefaultPermissionsExists(uri);
165+
}
166+
167+
/**
168+
* Apparently, the REST API won't let you remove these 3 default permissions, they're always present.
169+
*
170+
* And, now that we're loading modules via the REST API by default, rest-reader/read and rest-writer/update are
171+
* always present, at least on 8.0-6.3 and 9.0-1.1, which seems like a bug.
172+
*/
173+
private void assertDefaultPermissionsExists(String uri) {
174+
PermissionsFragment perms = getDocumentPermissions(uri, xccTemplate);
175+
perms.assertPermissionCount(5);
176+
perms.assertPermissionExists("rest-admin", "read");
177+
perms.assertPermissionExists("rest-admin", "update");
178+
perms.assertPermissionExists("rest-extension-user", "execute");
179+
180+
// Not really expected!
181+
perms.assertPermissionExists("rest-reader", "read");
182+
perms.assertPermissionExists("rest-writer", "update");
183+
}
165184

166-
// Not really expected!
167-
perms.assertPermissionExists("rest-reader", "read");
168-
perms.assertPermissionExists("rest-writer", "update");
169-
}
170185
}
171186

172187
class TestFileFilter extends AssetFileFilter {
173188

174-
@Override
175-
public boolean accept(File f) {
176-
return !f.getName().equals("test2.xqy") && super.accept(f);
177-
}
189+
@Override
190+
public boolean accept(File f) {
191+
return !f.getName().equals("test2.xqy") && super.accept(f);
192+
}
178193

179194
}

0 commit comments

Comments
 (0)