CaseProjectServiceImpl implements CaseProjectService { @Override public void configureNewCaseProject(WorkspaceProject project) { logger.debug("configuring case project {}", project); final KieModule kieModule = (KieModule) project.getMainModule(); org.uberfire.backend.vfs.Path rootPath1 = project.getRootPath(); final String separator = Paths.convert(rootPath1).getFileSystem().getSeparator(); try { String caseProjectDotFilePathStr = project.getMainModule().getRootPath().toURI().toString() + CASE_PROJECT_DOT_FILE; Path caseProjectDotFilePath = ioService.get(new URI(caseProjectDotFilePathStr)); if (!ioService.exists(caseProjectDotFilePath)) { ioService.write(caseProjectDotFilePath, ""); logger.debug("Added caseproject marker (dot) file at {}", caseProjectDotFilePath); } else { logger.debug("Caseproject marker (dot) file already exists. Not adding it."); } } catch (Exception e) { logger.error("Unable to write caseproject marker (dot) file: {}", e.getMessage()); } String metaInfPath = Paths.convert(kieModule.getKModuleXMLPath()).getParent().toUri().toString(); String deploymentDescriptorPath = metaInfPath + separator + DEPLOYMENT_DESCRIPTOR_FILE; Path ddVFSPath = ioService.get(URI.create(deploymentDescriptorPath)); org.uberfire.backend.vfs.Path convertedDDVFSPath = Paths.convert(ddVFSPath); if (!ioService.exists(ddVFSPath)) { ddEditorService.createIfNotExists(convertedDDVFSPath); logger.debug("Created deployment descriptor in {}", convertedDDVFSPath); } logger.debug("Loading deployment descriptor from {}", convertedDDVFSPath); DeploymentDescriptorModel descriptorModel = ddEditorService.load(convertedDDVFSPath); descriptorModel.setRuntimeStrategy("PER_CASE"); List<ItemObjectModel> modelList = descriptorModel.getMarshallingStrategies(); if (modelList == null) { modelList = new ArrayList<>(); } modelList.add(new ItemObjectModel(null, CASE_FILE_MARSHALLER, "mvel", new ArrayList<Parameter>())); modelList.add(new ItemObjectModel(null, DOCUMENT_MARSHALLER, "mvel", new ArrayList<Parameter>())); descriptorModel.setMarshallingStrategies(modelList); List<ItemObjectModel> wiModelList = descriptorModel.getWorkItemHandlers(); if (wiModelList == null) { wiModelList = new ArrayList<>(); } wiModelList.add(new ItemObjectModel(START_CASE_WORK_ITEM, START_CASE_HANDLER, "mvel", new ArrayList<Parameter>())); descriptorModel.setWorkItemHandlers(wiModelList); logger.debug("Deployment descriptor model updated with case information {}", descriptorModel); ddEditorService.save(convertedDDVFSPath, descriptorModel, null, "Updated with case project configuration"); logger.debug("Updated deployment model saved"); addWorkDefinitionsFileToProjectResources(kieModule, project, separator); } CaseProjectServiceImpl(); @Inject  CaseProjectServiceImpl(DDEditorService ddEditorService,
                                  @Named("ioStrategy") IOService ioService); @Override void configureNewCaseProject(WorkspaceProject project); void addWorkDefinitionsFileToProjectResources(KieModule kieModule, WorkspaceProject project, String separator); void configurePackage(@Observes NewPackageEvent pkg);  }
@Test public void testConfigureNewCaseProject() { final ArgumentCaptor<DeploymentDescriptorModel> ddArgumentCaptor = ArgumentCaptor.forClass(DeploymentDescriptorModel.class); DirectoryStream directoryStream = Mockito.mock(DirectoryStream.class); when(ioService.newDirectoryStream(any(), any())).thenReturn((DirectoryStream<Path>) directoryStream); when(directoryStream.iterator()).thenReturn(new ArrayList().iterator()); final Branch branch = mock(Branch.class); doReturn(projectPath).when(branch).getPath(); caseProjectService.configureNewCaseProject(new WorkspaceProject(mock(OrganizationalUnit.class), mock(Repository.class), branch, kieModule)); verify(ddEditorService, times(1)).save(any(), ddArgumentCaptor.capture(), any(), eq("Updated with case project configuration")); DeploymentDescriptorModel updatedDD = ddArgumentCaptor.getValue(); assertNotNull(updatedDD); assertEquals("PER_CASE", updatedDD.getRuntimeStrategy()); List<ItemObjectModel> marshallingStrategies = updatedDD.getMarshallingStrategies(); assertEquals(2, marshallingStrategies.size()); Map<String, String> mappedStrategies = marshallingStrategies.stream().collect(Collectors.toMap(ItemObjectModel::getValue, ItemObjectModel::getResolver)); assertTrue(mappedStrategies.containsKey(CaseProjectServiceImpl.CASE_FILE_MARSHALLER)); assertTrue(mappedStrategies.containsKey(CaseProjectServiceImpl.DOCUMENT_MARSHALLER)); assertEquals("mvel", mappedStrategies.get(CaseProjectServiceImpl.CASE_FILE_MARSHALLER)); assertEquals("mvel", mappedStrategies.get(CaseProjectServiceImpl.DOCUMENT_MARSHALLER)); List<ItemObjectModel> workItemHandlers = updatedDD.getWorkItemHandlers(); assertEquals(1, workItemHandlers.size()); ItemObjectModel startCaseHandler = workItemHandlers.get(0); assertEquals("mvel", startCaseHandler.getResolver()); assertEquals(CaseProjectServiceImpl.START_CASE_WORK_ITEM, startCaseHandler.getName()); assertEquals(CaseProjectServiceImpl.START_CASE_HANDLER, startCaseHandler.getValue()); verify(ioService, times(1)).write(any(), any(byte[].class)); }
@Test public void testConfigureNewCaseProjectWithPackages() { final ArgumentCaptor<DeploymentDescriptorModel> ddArgumentCaptor = ArgumentCaptor.forClass(DeploymentDescriptorModel.class); Path packagePath = Mockito.mock(Path.class); when(packagePath.toUri()).thenReturn(URI.create("default: DirectoryStream directoryStream = Mockito.mock(DirectoryStream.class); when(ioService.newDirectoryStream(any(), any())).thenReturn((DirectoryStream<Path>) directoryStream); when(directoryStream.iterator()).thenReturn(Arrays.asList(packagePath).iterator()); final Branch branch = mock(Branch.class); doReturn(projectPath).when(branch).getPath(); caseProjectService.configureNewCaseProject(new WorkspaceProject(mock(OrganizationalUnit.class), mock(Repository.class), branch, kieModule)); verify(ddEditorService, times(1)).save(any(), ddArgumentCaptor.capture(), any(), eq("Updated with case project configuration")); DeploymentDescriptorModel updatedDD = ddArgumentCaptor.getValue(); assertNotNull(updatedDD); assertEquals("PER_CASE", updatedDD.getRuntimeStrategy()); List<ItemObjectModel> marshallingStrategies = updatedDD.getMarshallingStrategies(); assertEquals(2, marshallingStrategies.size()); Map<String, String> mappedStrategies = marshallingStrategies.stream().collect(Collectors.toMap(ItemObjectModel::getValue, ItemObjectModel::getResolver)); assertTrue(mappedStrategies.containsKey(CaseProjectServiceImpl.CASE_FILE_MARSHALLER)); assertTrue(mappedStrategies.containsKey(CaseProjectServiceImpl.DOCUMENT_MARSHALLER)); assertEquals("mvel", mappedStrategies.get(CaseProjectServiceImpl.CASE_FILE_MARSHALLER)); assertEquals("mvel", mappedStrategies.get(CaseProjectServiceImpl.DOCUMENT_MARSHALLER)); verify(ioService, times(1)).write(any(), any(byte[].class)); }
@Test public void testConfigureNewCaseProjectNoDeploymentDescriptor() { final ArgumentCaptor<DeploymentDescriptorModel> ddArgumentCaptor = ArgumentCaptor.forClass(DeploymentDescriptorModel.class); DirectoryStream directoryStream = Mockito.mock(DirectoryStream.class); when(ioService.newDirectoryStream(any(), any())).thenReturn((DirectoryStream<Path>) directoryStream); when(directoryStream.iterator()).thenReturn(new ArrayList().iterator()); when(ioService.exists(ddPath)).thenReturn(false); final Branch branch = mock(Branch.class); doReturn(projectPath).when(branch).getPath(); caseProjectService.configureNewCaseProject(new WorkspaceProject(mock(OrganizationalUnit.class), mock(Repository.class), branch, kieModule)); verify(ddEditorService, times(1)).save(any(), ddArgumentCaptor.capture(), any(), eq("Updated with case project configuration")); verify(ddEditorService, times(1)).createIfNotExists(any()); DeploymentDescriptorModel updatedDD = ddArgumentCaptor.getValue(); assertNotNull(updatedDD); assertEquals("PER_CASE", updatedDD.getRuntimeStrategy()); List<ItemObjectModel> marshallingStrategies = updatedDD.getMarshallingStrategies(); assertEquals(2, marshallingStrategies.size()); Map<String, String> mappedStrategies = marshallingStrategies.stream().collect(Collectors.toMap(ItemObjectModel::getValue, ItemObjectModel::getResolver)); assertTrue(mappedStrategies.containsKey(CaseProjectServiceImpl.CASE_FILE_MARSHALLER)); assertTrue(mappedStrategies.containsKey(CaseProjectServiceImpl.DOCUMENT_MARSHALLER)); assertEquals("mvel", mappedStrategies.get(CaseProjectServiceImpl.CASE_FILE_MARSHALLER)); assertEquals("mvel", mappedStrategies.get(CaseProjectServiceImpl.DOCUMENT_MARSHALLER)); List<ItemObjectModel> workItemHandlers = updatedDD.getWorkItemHandlers(); assertEquals(1, workItemHandlers.size()); ItemObjectModel startCaseHandler = workItemHandlers.get(0); assertEquals("mvel", startCaseHandler.getResolver()); assertEquals(CaseProjectServiceImpl.START_CASE_WORK_ITEM, startCaseHandler.getName()); assertEquals(CaseProjectServiceImpl.START_CASE_HANDLER, startCaseHandler.getValue()); verify(ioService, times(1)).write(any(), any(byte[].class)); }
@Test public void testConfigureNewCaseProjectErrorWritingDotFile() { when(ioService.write(any(), anyString())).thenThrow(IllegalArgumentException.class); final ArgumentCaptor<DeploymentDescriptorModel> ddArgumentCaptor = ArgumentCaptor.forClass(DeploymentDescriptorModel.class); DirectoryStream directoryStream = Mockito.mock(DirectoryStream.class); when(ioService.newDirectoryStream(any(), any())).thenReturn((DirectoryStream<Path>) directoryStream); when(directoryStream.iterator()).thenReturn(new ArrayList().iterator()); final Branch branch = mock(Branch.class); doReturn(projectPath).when(branch).getPath(); caseProjectService.configureNewCaseProject(new WorkspaceProject(mock(OrganizationalUnit.class), mock(Repository.class), branch, kieModule)); verify(ddEditorService, times(1)).save(any(), ddArgumentCaptor.capture(), any(), eq("Updated with case project configuration")); DeploymentDescriptorModel updatedDD = ddArgumentCaptor.getValue(); assertNotNull(updatedDD); assertEquals("PER_CASE", updatedDD.getRuntimeStrategy()); List<ItemObjectModel> marshallingStrategies = updatedDD.getMarshallingStrategies(); assertEquals(2, marshallingStrategies.size()); Map<String, String> mappedStrategies = marshallingStrategies.stream().collect(Collectors.toMap(ItemObjectModel::getValue, ItemObjectModel::getResolver)); assertTrue(mappedStrategies.containsKey(CaseProjectServiceImpl.CASE_FILE_MARSHALLER)); assertTrue(mappedStrategies.containsKey(CaseProjectServiceImpl.DOCUMENT_MARSHALLER)); assertEquals("mvel", mappedStrategies.get(CaseProjectServiceImpl.CASE_FILE_MARSHALLER)); assertEquals("mvel", mappedStrategies.get(CaseProjectServiceImpl.DOCUMENT_MARSHALLER)); List<ItemObjectModel> workItemHandlers = updatedDD.getWorkItemHandlers(); assertEquals(1, workItemHandlers.size()); ItemObjectModel startCaseHandler = workItemHandlers.get(0); assertEquals("mvel", startCaseHandler.getResolver()); assertEquals(CaseProjectServiceImpl.START_CASE_WORK_ITEM, startCaseHandler.getName()); assertEquals(CaseProjectServiceImpl.START_CASE_HANDLER, startCaseHandler.getValue()); verify(ioService, times(1)).write(any(), any(byte[].class)); verify(mockAppender, times(1)).doAppend(captorLoggingEvent.capture()); final LoggingEvent loggingEvent = captorLoggingEvent.getValue(); assertEquals(loggingEvent.getLevel(), Level.ERROR); assertEquals(loggingEvent.getFormattedMessage(), "Unable to write caseproject marker (dot) file: null"); }