|
1 | 1 | package com.marklogic.quickstart.service; |
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
3 | 5 | import com.marklogic.hub.HubConfig; |
4 | 6 | import com.marklogic.hub.HubTestBase; |
5 | 7 | import com.marklogic.hub.flow.CodeFormat; |
|
31 | 33 | public class EntityManagerServiceTest extends HubTestBase { |
32 | 34 |
|
33 | 35 | private static String ENTITY = "test-entity"; |
| 36 | + private static String ENTITY2 = "test-entity2"; |
34 | 37 | private static Path projectDir = Paths.get(".", PROJECT_PATH); |
35 | 38 |
|
36 | 39 | @Autowired |
@@ -112,7 +115,23 @@ public void getEntities() throws IOException { |
112 | 115 | } |
113 | 116 |
|
114 | 117 | @Test |
115 | | - public void saveEntity() { |
| 118 | + public void saveEntity() throws IOException { |
| 119 | + Path entityDir = projectDir.resolve("plugins/entities/" + ENTITY); |
| 120 | + String entityFilename = ENTITY2 + EntityManagerService.ENTITY_FILE_EXTENSION; |
| 121 | + |
| 122 | + JsonNode node = getJsonFromResource(entityFilename); |
| 123 | + |
| 124 | + EntityModel entity = EntityModel.fromJson(entityFilename, node); |
| 125 | + entity.setFilename(entityDir.resolve(entityFilename).toString()); |
| 126 | + |
| 127 | + entityMgrService.saveEntity(entity); |
| 128 | + |
| 129 | + List<EntityModel> entities = entityMgrService.getEntities(); |
| 130 | + |
| 131 | + Assert.assertEquals(2, entities.size()); |
| 132 | + String[] expected = {ENTITY, ENTITY2}; |
| 133 | + String[] actual = { entities.get(0).getName(), entities.get(1).getName() }; |
| 134 | + Assert.assertArrayEquals(expected, actual); |
116 | 135 | } |
117 | 136 |
|
118 | 137 | @Test |
@@ -157,4 +176,39 @@ public void getFlowByWrongType() throws IOException { |
157 | 176 | Assert.assertNull(flow); |
158 | 177 | } |
159 | 178 |
|
| 179 | + /** |
| 180 | + * Addresses https://github.com/marklogic-community/marklogic-data-hub/issues/558. |
| 181 | + */ |
| 182 | + @Test |
| 183 | + public void changeEntityName() throws IOException { |
| 184 | + final String RENAMED_ENTITY = "renamed-entity"; |
| 185 | + |
| 186 | + // Get the original entity |
| 187 | + EntityModel entity = entityMgrService.getEntity(ENTITY); |
| 188 | + |
| 189 | + // Convert to String and change the title (the UI just changes the title property) |
| 190 | + String strEntity = entity.toJson().toString(); |
| 191 | + strEntity = strEntity.replaceFirst("\"title\"\\s*:\\s*\"test-entity\"", "\"title\" : \"" + RENAMED_ENTITY + "\""); |
| 192 | + strEntity = strEntity.replaceFirst("\"test-entity\"\\s*:", "\"" + RENAMED_ENTITY + "\" :"); |
| 193 | + |
| 194 | + // Convert back to JsonNode |
| 195 | + ObjectMapper mapper = new ObjectMapper(); |
| 196 | + JsonNode renamed = mapper.readTree(strEntity); |
| 197 | + EntityModel renamedEntity = EntityModel.fromJson(entity.getFilename(), renamed); |
| 198 | + |
| 199 | + // Save the renamedEntity |
| 200 | + entityMgrService.saveEntity(renamedEntity); |
| 201 | + |
| 202 | + List<EntityModel> entities = entityMgrService.getEntities(); |
| 203 | + Assert.assertEquals(1, entities.size()); |
| 204 | + |
| 205 | + // TODO: try to load the flows, which will fail. |
| 206 | + final String FLOW_NAME = "sjs-json-input-flow"; |
| 207 | + List<FlowModel> inputFlows = entities.get(0).getInputFlows(); |
| 208 | + |
| 209 | + Assert.assertEquals(RENAMED_ENTITY, inputFlows.get(0).entityName); |
| 210 | + Assert.assertEquals(FLOW_NAME, inputFlows.get(0).flowName); |
| 211 | + Assert.assertEquals(FlowType.INPUT, inputFlows.get(0).flowType); |
| 212 | + |
| 213 | + } |
160 | 214 | } |
0 commit comments