DecisionTableGuidedToDecisionTableXLSConverter { public XLSConversionResult convert(final Path originPath) throws IOException { final PackageDataModelOracle dmo = dataModelService.getDataModel(originPath); final GuidedDecisionTable52 dtable = guidedDecisionTableEditorService.load(originPath); final XLSBuilder.BuildResult buildResult = new XLSBuilder(dtable, dmo).build(); if (buildResult.getConversionResult().isConverted()) { final Workbook workbook = buildResult.getWorkbook(); final ByteArrayOutputStream fileOut = new ByteArrayOutputStream(); workbook.write(fileOut); final ByteArrayInputStream inStream = new ByteArrayInputStream(fileOut.toByteArray()); decisionTableXLSService.create(getDestinationFilePath(originPath), inStream, sessionInfo.getId(), "Converted from " + originPath.getFileName()); fileOut.close(); workbook.close(); inStream.close(); } return buildResult.getConversionResult(); }  XLSConversionResult convert(final Path originPath);  }
@Test public void convertEmptyTable() throws IOException { final Path path = PathFactory.newPath("file.gdst", "file: final GuidedDecisionTable52 table52 = new GuidedDecisionTable52(); doReturn(mock(PackageDataModelOracle.class)).when(dataModelService).getDataModel(any()); doReturn(table52).when(guidedDecisionTableEditorService).load(path); doReturn("id").when(sessionInfo).getId(); final XLSConversionResult convert = converter.convert(path); assertNotNull(convert); verify(decisionTableXLSService).create(any(), any(ByteArrayInputStream.class), eq("id"), eq("Converted from file.gdst")); }
@Test public void failureOnConversion() throws IOException { final Path path = PathFactory.newPath("file.gdst", "file: final GuidedDecisionTable52 table52 = new GuidedDecisionTable52(); final AttributeCol52 attributeCol52 = new AttributeCol52(); attributeCol52.setAttribute(Attribute.NEGATE_RULE.getAttributeName()); table52.getAttributeCols().add(attributeCol52); doReturn(mock(PackageDataModelOracle.class)).when(dataModelService).getDataModel(any()); doReturn(table52).when(guidedDecisionTableEditorService).load(path); doReturn("id").when(sessionInfo).getId(); final XLSConversionResult convert = converter.convert(path); assertNotNull(convert); verify(decisionTableXLSService, never()).create(any(), any(ByteArrayInputStream.class), any(), any()); assertFalse(convert.isConverted()); }
@Test public void fileExists() throws IOException { final Path path = PathFactory.newPath("file.gdst", "file: final GuidedDecisionTable52 table52 = new GuidedDecisionTable52(); doReturn(mock(PackageDataModelOracle.class)).when(dataModelService).getDataModel(any()); doReturn(table52).when(guidedDecisionTableEditorService).load(path); doReturn("id").when(sessionInfo).getId(); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { final org.uberfire.java.nio.file.Path proposedPath = (org.uberfire.java.nio.file.Path) invocation.getArguments()[0]; return Objects.equals("file.gdst export.xls", Paths.convert(proposedPath).getFileName()); } }).when(ioService).exists(any()); final XLSConversionResult convert = converter.convert(path); assertNotNull(convert); verify(decisionTableXLSService).create(pathArgumentCaptor.capture(), any(ByteArrayInputStream.class), any(), any()); final Path value = pathArgumentCaptor.getValue(); assertEquals("file.gdst export (1).xls", value.getFileName()); }