|
| 1 | +package com.marklogic.quickstart.service; |
| 2 | + |
| 3 | +import com.marklogic.hub.HubConfig; |
| 4 | +import com.marklogic.hub.HubTestBase; |
| 5 | +import com.marklogic.hub.flow.CodeFormat; |
| 6 | +import com.marklogic.hub.flow.DataFormat; |
| 7 | +import com.marklogic.hub.flow.FlowType; |
| 8 | +import com.marklogic.hub.scaffold.Scaffolding; |
| 9 | +import com.marklogic.hub.util.FileUtil; |
| 10 | +import com.marklogic.quickstart.auth.ConnectionAuthenticationToken; |
| 11 | +import com.marklogic.quickstart.model.EnvironmentConfig; |
| 12 | +import com.marklogic.quickstart.model.entity_services.EntityModel; |
| 13 | +import org.apache.commons.io.FileUtils; |
| 14 | +import org.junit.*; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.boot.test.context.SpringBootTest; |
| 18 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 19 | +import org.springframework.mock.web.MockHttpSession; |
| 20 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 21 | +import org.springframework.test.context.junit4.SpringRunner; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.nio.file.Paths; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +@RunWith(SpringRunner.class) |
| 29 | +@SpringBootTest() |
| 30 | +public class EntityManagerServiceTest extends HubTestBase { |
| 31 | + |
| 32 | + private static String ENTITY = "test-entity"; |
| 33 | + private static Path projectDir = Paths.get(".", PROJECT_PATH); |
| 34 | + |
| 35 | + @Autowired |
| 36 | + MockHttpServletRequest request; |
| 37 | + |
| 38 | + @Autowired |
| 39 | + MockHttpSession session; |
| 40 | + |
| 41 | + @Autowired |
| 42 | + EntityManagerService entityMgrService; |
| 43 | + |
| 44 | + @BeforeClass |
| 45 | + public static void setupSuite() throws IOException { |
| 46 | + FileUtils.deleteDirectory(projectDir.toFile()); |
| 47 | + |
| 48 | + EnvironmentConfig envConfig = new EnvironmentConfig(projectDir.toString(), "local", "admin", "admin"); |
| 49 | + envConfig.setMlSettings(HubConfig.hubFromEnvironment(projectDir.toString(), null)); |
| 50 | + envConfig.checkIfInstalled(); |
| 51 | + setEnvConfig(envConfig); |
| 52 | + |
| 53 | + installHub(); |
| 54 | + } |
| 55 | + |
| 56 | + private static void setEnvConfig(EnvironmentConfig envConfig) { |
| 57 | + ConnectionAuthenticationToken authenticationToken = new ConnectionAuthenticationToken("admin", "admin", "localhost", 1, "local"); |
| 58 | + authenticationToken.setEnvironmentConfig(envConfig); |
| 59 | + SecurityContextHolder.getContext().setAuthentication(authenticationToken); |
| 60 | + } |
| 61 | + |
| 62 | + @Before |
| 63 | + public void setUp() throws IOException { |
| 64 | + FileUtils.deleteDirectory(projectDir.toFile()); |
| 65 | + |
| 66 | + Scaffolding scaffolding = new Scaffolding(projectDir.toString(), stagingClient); |
| 67 | + scaffolding.createEntity(ENTITY); |
| 68 | + scaffolding.createFlow(ENTITY, "sjs-json-input-flow", FlowType.INPUT, |
| 69 | + CodeFormat.JAVASCRIPT, DataFormat.JSON); |
| 70 | + |
| 71 | + scaffolding.createFlow(ENTITY, "sjs-xml-input-flow", FlowType.INPUT, |
| 72 | + CodeFormat.JAVASCRIPT, DataFormat.XML); |
| 73 | + |
| 74 | + scaffolding.createFlow(ENTITY, "xqy-json-input-flow", FlowType.INPUT, |
| 75 | + CodeFormat.XQUERY, DataFormat.JSON); |
| 76 | + |
| 77 | + scaffolding.createFlow(ENTITY, "xqy-xml-input-flow", FlowType.INPUT, |
| 78 | + CodeFormat.XQUERY, DataFormat.XML); |
| 79 | + |
| 80 | + Path entityDir = projectDir.resolve("plugins/entities/" + ENTITY); |
| 81 | + Path inputDir = entityDir.resolve("input"); |
| 82 | + |
| 83 | + String entityFilename = ENTITY + EntityManagerService.ENTITY_FILE_EXTENSION; |
| 84 | + FileUtil.copy(getResourceStream(entityFilename), entityDir.resolve(entityFilename).toFile()); |
| 85 | + |
| 86 | + FileUtil.copy(getResourceStream("flow-manager/sjs-flow/headers.sjs"), inputDir.resolve("sjs-json-input-flow/headers.sjs").toFile()); |
| 87 | + FileUtil.copy(getResourceStream("flow-manager/sjs-flow/content-input.sjs"), inputDir.resolve("sjs-json-input-flow/content.sjs").toFile()); |
| 88 | + FileUtil.copy(getResourceStream("flow-manager/sjs-flow/triples.sjs"), inputDir.resolve("sjs-json-input-flow/triples.sjs").toFile()); |
| 89 | + |
| 90 | + FileUtil.copy(getResourceStream("flow-manager/sjs-flow/headers.sjs"), inputDir.resolve("sjs-xml-input-flow/headers.sjs").toFile()); |
| 91 | + FileUtil.copy(getResourceStream("flow-manager/sjs-flow/content-input.sjs"), inputDir.resolve("sjs-xml-input-flow/content.sjs").toFile()); |
| 92 | + FileUtil.copy(getResourceStream("flow-manager/sjs-flow/triples.sjs"), inputDir.resolve("sjs-xml-input-flow/triples.sjs").toFile()); |
| 93 | + |
| 94 | + FileUtil.copy(getResourceStream("flow-manager/xqy-flow/headers-json.xqy"), inputDir.resolve("xqy-json-input-flow/headers.xqy").toFile()); |
| 95 | + FileUtil.copy(getResourceStream("flow-manager/xqy-flow/content-input.xqy"), inputDir.resolve("xqy-json-input-flow/content.xqy").toFile()); |
| 96 | + FileUtil.copy(getResourceStream("flow-manager/xqy-flow/triples.xqy"), inputDir.resolve("xqy-json-input-flow/triples.xqy").toFile()); |
| 97 | + |
| 98 | + FileUtil.copy(getResourceStream("flow-manager/xqy-flow/headers-xml.xqy"), inputDir.resolve("xqy-xml-input-flow/headers.xqy").toFile()); |
| 99 | + FileUtil.copy(getResourceStream("flow-manager/xqy-flow/content-input.xqy"), inputDir.resolve("xqy-xml-input-flow/content.xqy").toFile()); |
| 100 | + FileUtil.copy(getResourceStream("flow-manager/xqy-flow/triples.xqy"), inputDir.resolve("xqy-xml-input-flow/triples.xqy").toFile()); |
| 101 | + |
| 102 | + getDataHub().installUserModules(true); |
| 103 | + } |
| 104 | + |
| 105 | + @AfterClass |
| 106 | + public static void tearDown() throws IOException { |
| 107 | + FileUtils.deleteDirectory(projectDir.toFile()); |
| 108 | + uninstallHub(); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void getEntities() throws IOException { |
| 113 | + List<EntityModel> entities = entityMgrService.getEntities(); |
| 114 | + |
| 115 | + Assert.assertEquals(1, entities.size()); |
| 116 | + Assert.assertEquals(ENTITY, entities.get(0).getName()); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void saveEntity() { |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void getEntity() { |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void getFlow() { |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments