use of org.uberfire.io.IOService in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method nestedClassUpdateTest.
@Test
public void nestedClassUpdateTest() {
try {
// First read the NestedClassUpdateTest
Path nestedClassUpdateTestFilePath = rootPath.resolve("package5").resolve("NestedClassUpdateTest.java");
String source = ioService.readAllString(nestedClassUpdateTestFilePath);
JavaClassSource nestedClassUpdateTestJavaClassSource = (JavaClassSource) Roaster.parse(source);
ClassLoader classLoader = getClass().getClassLoader();
ClassTypeResolver classTypeResolver = DriverUtils.createClassTypeResolver(nestedClassUpdateTestJavaClassSource, classLoader);
final SourceFilter pojo1Filter = javaType -> false;
final NestedClassFilter nestedClassFilter = javaType -> javaType.isClass() && javaType.getAnnotation(Generated.class) != null;
final MethodFilter methodFilter = method -> !method.isConstructor() && method.getAnnotation(Generated.class) != null;
FilterHolder filterHolder = mock(FilterHolder.class);
when(filterHolder.getSourceFilters()).thenReturn(Collections.singleton(pojo1Filter));
when(filterHolder.getNestedClassFilters()).thenReturn(Collections.singleton(nestedClassFilter));
when(filterHolder.getMethodFilters()).thenReturn(Collections.singleton(methodFilter));
JavaRoasterModelDriver javaRoasterModelDriver = new JavaRoasterModelDriver(ioService, rootPath, classLoader, filterHolder);
ModelDriverResult result = javaRoasterModelDriver.loadDataObject(source, nestedClassUpdateTestFilePath);
// 1) read the NestedClassUpdateTest
DataObject nestedClassUpdateTest = result.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package5.NestedClassUpdateTest");
// 2) modify the NestedClassUpdateTest according to the expected result
JavaClass nestedClass = nestedClassUpdateTest.getNestedClasses().stream().filter(t -> t.getName().equals("NestedClass")).findFirst().get();
assertNotNull(nestedClass);
nestedClass.setName("UpdatedNestedClass");
String removedInterface = nestedClass.removeInterface("ParametrizedInterface<Integer>");
assertNotNull(removedInterface);
nestedClass.addInterface("ParametrizedInterface<Double>");
Method method = nestedClass.getMethod("method", Collections.EMPTY_LIST);
assertNotNull(method);
method.setName("updatedMethod");
nestedClassUpdateTest.setName("NestedClassUpdateTestResult");
// 3) compare the modified data object with the expected data object.
Path expectedFilePath = rootPath.resolve("package5").resolve("NestedClassUpdateTestResult.java");
String expectedSource = ioService.readAllString(expectedFilePath);
JavaClassSource expectedJavaClassSource = (JavaClassSource) Roaster.parse(expectedSource);
ModelDriverResult expectedResult = javaRoasterModelDriver.loadDataObject(expectedSource, expectedFilePath);
DataObject nestedClassUpdateTestResult = expectedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package5.NestedClassUpdateTestResult");
// First check, the modified data object in memory should be the same as the readed from the model.
DataModelerAssert.assertEqualsDataObject(nestedClassUpdateTestResult, nestedClassUpdateTest);
// Second check, update the JavaClassSource corresponding to the NestedClassUpdateTest
javaRoasterModelDriver.updateSource(nestedClassUpdateTestJavaClassSource, nestedClassUpdateTest, new UpdateInfo(), classTypeResolver);
ModelDriverResult updatedResult = javaRoasterModelDriver.loadDataObject(nestedClassUpdateTestJavaClassSource.toString(), nestedClassUpdateTestFilePath);
// and now compare the updatedResult with the expected value.
DataModelerAssert.assertEqualsDataObject(nestedClassUpdateTestResult, updatedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package5.NestedClassUpdateTestResult"));
logger.debug(nestedClassUpdateTestJavaClassSource.toString());
} catch (Exception e) {
e.printStackTrace();
fail("Test failed: " + e.getMessage());
}
}
use of org.uberfire.io.IOService in project kie-wb-common by kiegroup.
the class ExamplesServiceImplTest method setup.
@Before
public void setup() throws Exception {
when(spaceConfigStorageRegistry.get(Mockito.<String>any())).thenReturn(spaceConfigStorage);
when(spaceConfigStorageRegistry.getBatch(Mockito.<String>any())).thenReturn(new SpaceConfigStorageRegistryImpl.SpaceStorageBatchImpl(spaceConfigStorage));
when(spaceConfigStorageRegistry.exist(Mockito.<String>any())).thenReturn(true);
when(ou.getSpace()).thenReturn(space);
when(space.getName()).thenReturn("ou");
service = spy(new ExamplesServiceImpl(ioService, repositoryFactory, moduleService, ouService, projectService, metadataService, newProjectEvent, projectScreenService, validators, spaceConfigStorageRegistry, systemFS, pathUtil, repositoryService));
FileUtils.deleteDirectory(new File(".kie-wb-playground"));
when(this.validators.getValidators()).thenReturn(new ArrayList<>());
doAnswer(invocationOnMock -> {
String spaceName = (String) invocationOnMock.getArguments()[0];
String defaultGroupId = (String) invocationOnMock.getArguments()[1];
OrganizationalUnitImpl o = new OrganizationalUnitImpl(spaceName, defaultGroupId);
organizationalUnits.put(spaceName, o);
return o;
}).when(ouService).createOrganizationalUnit(Mockito.<String>any(), Mockito.<String>any());
doAnswer(invocationOnMock -> organizationalUnits.get(invocationOnMock.getArguments()[0])).when(ouService).getOrganizationalUnit(Mockito.<String>any());
when(ouService.getOrganizationalUnits()).thenReturn(new HashSet<OrganizationalUnit>() {
{
add(new OrganizationalUnitImpl("ou1Name", "ou1GroupId"));
add(new OrganizationalUnitImpl("ou2Name", "ou2GroupId"));
}
});
when(moduleService.resolveModule(Mockito.<Path>any())).thenAnswer((Answer<KieModule>) invocationOnMock -> {
final Path path = (Path) invocationOnMock.getArguments()[0];
final KieModule module = new KieModule(path, path, path, path, path, path, mock(POM.class));
return module;
});
when(sessionInfo.getId()).thenReturn("sessionId");
when(sessionInfo.getIdentity()).thenReturn(user);
when(user.getIdentifier()).thenReturn("user");
doAnswer(invocationOnMock -> organizationalUnits.containsKey(invocationOnMock.getArguments()[0])).when(service).existSpace(any());
}
use of org.uberfire.io.IOService in project kie-wb-common by kiegroup.
the class BaseIndexingTest method setup.
@Before
public void setup() throws IOException {
if (!created) {
final String repositoryName = getRepositoryName();
final String path = createTempDirectory().getAbsolutePath();
System.setProperty("org.uberfire.nio.git.dir", path);
System.setProperty("org.uberfire.nio.git.daemon.enabled", "false");
System.setProperty("org.uberfire.nio.git.ssh.enabled", "false");
System.setProperty("org.uberfire.sys.repo.monitor.disabled", "true");
logger.debug(".niogit: " + path);
final URI newRepo = URI.create("git://" + repositoryName);
try {
IOService ioService = ioService();
ioService.newFileSystem(newRepo, new HashMap<String, Object>());
// Don't ask, but we need to write a single file first in order for indexing to work
basePath = getDirectoryPath().resolveSibling("someNewOtherPath");
ioService().write(basePath.resolve("dummy"), "<none>");
} catch (final Exception e) {
e.printStackTrace();
logger.warn("Failed to initialize IOService instance: " + e.getClass().getSimpleName() + ": " + e.getMessage(), e);
} finally {
created = true;
}
final Instance<NamedQuery> namedQueriesProducer = mock(Instance.class);
when(namedQueriesProducer.iterator()).thenReturn(getQueries().iterator());
service = new RefactoringQueryServiceImpl(getConfig(), new NamedQueries(namedQueriesProducer));
service.init();
}
}
use of org.uberfire.io.IOService in project drools-wb by kiegroup.
the class DecisionTableXLSServiceImplTest method inputStreamShouldNotBeReused.
@Test
public void inputStreamShouldNotBeReused() throws IOException {
when(ioService.write(any(org.uberfire.java.nio.file.Path.class), any(byte[].class), any())).thenReturn(null);
this.service = getServiceWithValidationOverride((tempFile) -> {
// Do nothing; tests do not use a *real* XLS file
});
mockStatic(IOUtils.class);
mockStatic(Paths.class);
when(Paths.convert(any(Path.class))).thenReturn(nioPath);
service.create(path, inputstream, sessionId, comment);
final InOrder inOrder = inOrder(ioService);
inOrder.verify(ioService).startBatch(any());
inOrder.verify(ioService).write(any(org.uberfire.java.nio.file.Path.class), Mockito.<byte[]>any(), any(CommentedOption.class));
inOrder.verify(ioService).endBatch();
verifyStatic(IOUtils.class, times(1));
IOUtils.copy(eq(inputstream), any(OutputStream.class));
}
use of org.uberfire.io.IOService in project drools-wb by kiegroup.
the class DecisionTableXLSServiceImplTest method testInvalidTable.
private void testInvalidTable(Consumer<DecisionTableXLSServiceImpl> serviceConsumer) throws IOException {
this.service = getServiceWithValidationOverride((tempFile) -> {
// mock an invalid file
Throwable t = new Throwable("testing invalid xls dt creation");
throw new DecisionTableParseException("DecisionTableParseException: " + t.getMessage(), t);
});
mockStatic(IOUtils.class);
when(IOUtils.copy(any(InputStream.class), any(OutputStream.class))).thenReturn(0);
try {
serviceConsumer.accept(service);
} catch (RuntimeException e) {
// this is expected correct behavior
}
verify(ioService, never()).newOutputStream(any(org.uberfire.java.nio.file.Path.class), any(CommentedOption.class));
verifyStatic(IOUtils.class, never());
IOUtils.toByteArray(Mockito.<InputStream>any());
}
Aggregations