Skip to content

Commit d36a6c8

Browse files
authored
Merge pull request #751 from marklogic/feature/22604-refactor-fragment
MLE-22604 Refactored ml-app-deployer tests to use marklogic-junit5
2 parents 2683d03 + b87bb47 commit d36a6c8

16 files changed

+207
-688
lines changed

ml-app-deployer/src/test/java/com/marklogic/appdeployer/AbstractAppDeployerTest.java

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
import com.marklogic.appdeployer.command.modules.LoadModulesCommand;
2424
import com.marklogic.appdeployer.command.security.GenerateTemporaryCertificateCommand;
2525
import com.marklogic.appdeployer.impl.SimpleAppDeployer;
26+
import com.marklogic.client.DatabaseClient;
27+
import com.marklogic.client.DatabaseClientFactory;
2628
import com.marklogic.client.ext.modulesloader.impl.DefaultModulesLoader;
2729
import com.marklogic.mgmt.AbstractMgmtTest;
2830
import com.marklogic.mgmt.resource.appservers.ServerManager;
29-
import com.marklogic.xcc.template.XccTemplate;
3031
import org.junit.jupiter.api.BeforeEach;
3132

3233
import java.io.File;
@@ -38,81 +39,82 @@
3839
*/
3940
public abstract class AbstractAppDeployerTest extends AbstractMgmtTest {
4041

41-
public final static String SAMPLE_APP_NAME = "sample-app";
42+
public final static String SAMPLE_APP_NAME = "sample-app";
4243

43-
protected final static Integer SAMPLE_APP_REST_PORT = 8004;
44-
protected final static Integer SAMPLE_APP_TEST_REST_PORT = 8005;
44+
protected final static Integer SAMPLE_APP_REST_PORT = 8004;
45+
protected final static Integer SAMPLE_APP_TEST_REST_PORT = 8005;
4546

46-
// Intended to be used by subclasses
47-
protected AppDeployer appDeployer;
48-
protected AppConfig appConfig;
47+
// Intended to be used by subclasses
48+
protected AppDeployer appDeployer;
49+
protected AppConfig appConfig;
4950

50-
@BeforeEach
51-
public void initialize() {
52-
initializeAppConfig();
53-
}
51+
@BeforeEach
52+
public void initialize() {
53+
initializeAppConfig();
54+
}
5455

55-
protected void initializeAppConfig() {
56-
initializeAppConfig(new File("src/test/resources/sample-app"));
57-
}
56+
protected void initializeAppConfig() {
57+
initializeAppConfig(new File("src/test/resources/sample-app"));
58+
}
5859

59-
protected void initializeAppConfig(File projectDir) {
60-
appConfig = new AppConfig(projectDir);
60+
protected void initializeAppConfig(File projectDir) {
61+
appConfig = new AppConfig(projectDir);
6162
appConfig.setHost(this.manageConfig.getHost());
62-
appConfig.setName(SAMPLE_APP_NAME);
63-
appConfig.setRestPort(SAMPLE_APP_REST_PORT);
63+
appConfig.setName(SAMPLE_APP_NAME);
64+
appConfig.setRestPort(SAMPLE_APP_REST_PORT);
6465

65-
// Assume that the manager user can also be used as the REST admin user
66-
appConfig.setRestAdminUsername(manageConfig.getUsername());
67-
appConfig.setRestAdminPassword(manageConfig.getPassword());
66+
// Assume that the manager user can also be used as the REST admin user
67+
appConfig.setRestAdminUsername(manageConfig.getUsername());
68+
appConfig.setRestAdminPassword(manageConfig.getPassword());
6869
appConfig.setAppServicesUsername(manageConfig.getUsername());
6970
appConfig.setAppServicesPassword(manageConfig.getPassword());
70-
}
71-
72-
/**
73-
* Initialize an AppDeployer with the given set of commands. Avoids having to create a Spring configuration.
74-
*
75-
* @param commands
76-
*/
77-
protected void initializeAppDeployer(Command... commands) {
78-
appDeployer = new SimpleAppDeployer(manageClient, adminManager, commands);
79-
}
80-
81-
protected void deploySampleApp() {
82-
appDeployer.deploy(appConfig);
83-
}
84-
85-
protected void undeploySampleApp() {
86-
if (appDeployer != null) {
87-
try {
88-
appDeployer.undeploy(appConfig);
89-
} catch (Exception e) {
90-
throw new RuntimeException("Unexpected error while undeploying sample app: " + e.getMessage(), e);
91-
}
92-
}
93-
}
94-
95-
protected XccTemplate newModulesXccTemplate() {
96-
return new XccTemplate(appConfig.getHost(), appConfig.getAppServicesPort(), appConfig.getRestAdminUsername(),
97-
appConfig.getRestAdminPassword(), appConfig.getModulesDatabaseName());
98-
}
99-
100-
/**
101-
* This command is configured to always load modules, ignoring the cache file in the build directory.
102-
* @return
103-
*/
104-
protected LoadModulesCommand buildLoadModulesCommand() {
105-
LoadModulesCommand command = new LoadModulesCommand();
106-
appConfig.setModuleTimestampsPath(null);
107-
DefaultModulesLoader loader = (DefaultModulesLoader)(new DefaultModulesLoaderFactory().newModulesLoader(appConfig));
108-
loader.setModulesManager(null);
109-
command.setModulesLoader(loader);
110-
return command;
111-
}
112-
113-
protected void setConfigBaseDir(String path) {
114-
appConfig.getFirstConfigDir().setBaseDir(new File("src/test/resources/" + path));
115-
}
71+
}
72+
73+
/**
74+
* Initialize an AppDeployer with the given set of commands. Avoids having to create a Spring configuration.
75+
*
76+
* @param commands
77+
*/
78+
protected void initializeAppDeployer(Command... commands) {
79+
appDeployer = new SimpleAppDeployer(manageClient, adminManager, commands);
80+
}
81+
82+
protected void deploySampleApp() {
83+
appDeployer.deploy(appConfig);
84+
}
85+
86+
protected void undeploySampleApp() {
87+
if (appDeployer != null) {
88+
try {
89+
appDeployer.undeploy(appConfig);
90+
} catch (Exception e) {
91+
throw new RuntimeException("Unexpected error while undeploying sample app: " + e.getMessage(), e);
92+
}
93+
}
94+
}
95+
96+
protected final DatabaseClient newDatabaseClient(String databaseName) {
97+
return DatabaseClientFactory.newClient(appConfig.getHost(), appConfig.getRestPort(),
98+
databaseName, new DatabaseClientFactory.DigestAuthContext(appConfig.getRestAdminUsername(), appConfig.getRestAdminPassword()));
99+
}
100+
101+
/**
102+
* This command is configured to always load modules, ignoring the cache file in the build directory.
103+
*
104+
* @return
105+
*/
106+
protected LoadModulesCommand buildLoadModulesCommand() {
107+
LoadModulesCommand command = new LoadModulesCommand();
108+
appConfig.setModuleTimestampsPath(null);
109+
DefaultModulesLoader loader = (DefaultModulesLoader) (new DefaultModulesLoaderFactory().newModulesLoader(appConfig));
110+
loader.setModulesManager(null);
111+
command.setModulesLoader(loader);
112+
return command;
113+
}
114+
115+
protected void setConfigBaseDir(String path) {
116+
appConfig.getFirstConfigDir().setBaseDir(new File("src/test/resources/" + path));
117+
}
116118

117119
/**
118120
* Intended to simplify testing app servers that require SSL.

ml-app-deployer/src/test/java/com/marklogic/appdeployer/command/databases/ClearDatabaseTest.java

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,42 @@
1515
*/
1616
package com.marklogic.appdeployer.command.databases;
1717

18-
import org.junit.jupiter.api.AfterEach;
19-
import org.junit.jupiter.api.Test;
20-
2118
import com.marklogic.appdeployer.AbstractAppDeployerTest;
2219
import com.marklogic.appdeployer.command.restapis.DeployRestApiServersCommand;
20+
import com.marklogic.client.DatabaseClient;
2321
import com.marklogic.mgmt.resource.databases.DatabaseManager;
22+
import org.junit.jupiter.api.AfterEach;
23+
import org.junit.jupiter.api.Test;
2424

25-
import static org.junit.jupiter.api.Assertions.assertEquals;
26-
import static org.junit.jupiter.api.Assertions.assertTrue;
27-
28-
public class ClearDatabaseTest extends AbstractAppDeployerTest {
29-
30-
@AfterEach
31-
public void teardown() {
32-
undeploySampleApp();
33-
}
34-
35-
/**
36-
* Testing against the modules database, but the operation is the same regardless of database.
37-
*
38-
* Also, for the modules database, ml-gradle 1.+ had the ability to exclude certain modules from being deleted in
39-
* case those modules were needed by the REST API rewriter. But in that case, it's usually preferable to just use
40-
* the XCC approach for loading asset modules.
41-
*/
42-
@Test
43-
public void modulesDatabase() {
44-
initializeAppDeployer(new DeployRestApiServersCommand(true), buildLoadModulesCommand());
45-
46-
appConfig.setRestPort(8004);
47-
appDeployer.deploy(appConfig);
48-
49-
DatabaseManager mgr = new DatabaseManager(manageClient);
50-
mgr.clearDatabase(appConfig.getModulesDatabaseName());
51-
52-
String uris = newModulesXccTemplate().executeAdhocQuery("cts:uris((), (), cts:and-query(()))");
53-
assertEquals(0, uris.length(), "The modules database should have been cleared");
54-
}
25+
import static org.junit.jupiter.api.Assertions.assertNull;
26+
27+
class ClearDatabaseTest extends AbstractAppDeployerTest {
28+
29+
@AfterEach
30+
public void teardown() {
31+
undeploySampleApp();
32+
}
33+
34+
/**
35+
* Testing against the modules database, but the operation is the same regardless of database.
36+
* <p>
37+
* Also, for the modules database, ml-gradle 1.+ had the ability to exclude certain modules from being deleted in
38+
* case those modules were needed by the REST API rewriter. But in that case, it's usually preferable to just use
39+
* the XCC approach for loading asset modules.
40+
*/
41+
@Test
42+
void modulesDatabase() {
43+
initializeAppDeployer(new DeployRestApiServersCommand(true), buildLoadModulesCommand());
44+
45+
appConfig.setRestPort(8004);
46+
appDeployer.deploy(appConfig);
47+
48+
DatabaseManager mgr = new DatabaseManager(manageClient);
49+
mgr.clearDatabase(appConfig.getModulesDatabaseName());
50+
51+
try (DatabaseClient client = newDatabaseClient("sample-app-modules")) {
52+
String uris = client.newServerEval().xquery("cts:uris((), (), cts:and-query(()))").evalAs(String.class);
53+
assertNull(uris, "The modules database should have been cleared.");
54+
}
55+
}
5556
}

ml-app-deployer/src/test/java/com/marklogic/appdeployer/command/mimetypes/DontRestartWhenMimetypePropertiesArentUpdatedTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
package com.marklogic.appdeployer.command.mimetypes;
1717

1818
import com.marklogic.appdeployer.AbstractAppDeployerTest;
19-
import com.marklogic.junit.XmlHelper;
2019
import com.marklogic.mgmt.SaveReceipt;
2120
import com.marklogic.mgmt.resource.mimetypes.MimetypeManager;
2221
import org.junit.jupiter.api.AfterEach;
2322
import org.junit.jupiter.api.Test;
23+
import org.springframework.core.io.ClassPathResource;
24+
import org.springframework.util.FileCopyUtils;
25+
26+
import java.io.IOException;
2427

2528
import static org.junit.jupiter.api.Assertions.*;
2629

@@ -35,7 +38,7 @@ public void teardown() {
3538
}
3639

3740
@Test
38-
public void test() {
41+
public void test() throws IOException {
3942
mimetypeManager = new MimetypeManager(manageClient);
4043

4144
initializeAppDeployer(new DeployMimetypesCommand());
@@ -47,7 +50,7 @@ public void test() {
4750
deploySampleApp();
4851

4952
// But we can verify that MimetypeManager doesn't cause an update
50-
String payload = new XmlHelper().readTestResource("sample-app/src/main/ml-config/mimetypes/ditamap.json");
53+
String payload = new String(FileCopyUtils.copyToByteArray(new ClassPathResource("sample-app/src/main/ml-config/mimetypes/ditamap.json").getInputStream()));
5154

5255
SaveReceipt receipt = mimetypeManager.save(payload);
5356
assertNull(receipt.getResponse(),

0 commit comments

Comments
 (0)