use of java.util.Optional in project che by eclipse.
the class WorkspaceDaoTest method shouldUpdateWorkspace.
@Test(dependsOnMethods = "shouldGetWorkspaceById")
public void shouldUpdateWorkspace() throws Exception {
final WorkspaceImpl workspace = new WorkspaceImpl(workspaces[0], workspaces[0].getAccount());
// Remove an existing project configuration from workspace
workspace.getConfig().getProjects().remove(1);
// Add new project to the workspace configuration
final SourceStorageImpl source3 = new SourceStorageImpl();
source3.setType("type3");
source3.setLocation("location3");
source3.setParameters(new HashMap<>(ImmutableMap.of("param1", "value1", "param2", "value2", "param3", "value3")));
final ProjectConfigImpl newProjectCfg = new ProjectConfigImpl();
newProjectCfg.setPath("/path3");
newProjectCfg.setType("type3");
newProjectCfg.setName("project3");
newProjectCfg.setDescription("description3");
newProjectCfg.getMixins().addAll(asList("mixin3", "mixin4"));
newProjectCfg.setSource(source3);
newProjectCfg.getAttributes().put("new-key", asList("1", "2"));
workspace.getConfig().getProjects().add(newProjectCfg);
// Update an existing project configuration
final ProjectConfigImpl projectCfg = workspace.getConfig().getProjects().get(0);
projectCfg.getAttributes().clear();
projectCfg.getSource().setLocation("new-location");
projectCfg.getSource().setType("new-type");
projectCfg.getSource().getParameters().put("new-param", "new-param-value");
projectCfg.getMixins().add("new-mixin");
projectCfg.setPath("/new-path");
projectCfg.setDescription("new project description");
// Remove an existing command
workspace.getConfig().getCommands().remove(1);
// Add a new command
final CommandImpl newCmd = new CommandImpl();
newCmd.setName("name3");
newCmd.setType("type3");
newCmd.setCommandLine("cmd3");
newCmd.getAttributes().putAll(ImmutableMap.of("attr1", "value1", "attr2", "value2", "attr3", "value3"));
workspace.getConfig().getCommands().add(newCmd);
// Update an existing command
final CommandImpl command = workspace.getConfig().getCommands().get(0);
command.setName("new-name");
command.setType("new-type");
command.setCommandLine("new-command-line");
command.getAttributes().clear();
// Add a new environment
final EnvironmentRecipeImpl newRecipe = new EnvironmentRecipeImpl();
newRecipe.setLocation("new-location");
newRecipe.setType("new-type");
newRecipe.setContentType("new-content-type");
newRecipe.setContent("new-content");
final ExtendedMachineImpl newMachine = new ExtendedMachineImpl();
final ServerConf2Impl serverConf1 = new ServerConf2Impl("2265", "http", singletonMap("prop1", "val"));
final ServerConf2Impl serverConf2 = new ServerConf2Impl("2266", "ftp", singletonMap("prop1", "val"));
newMachine.setServers(ImmutableMap.of("ref1", serverConf1, "ref2", serverConf2));
newMachine.setAgents(ImmutableList.of("agent5", "agent4"));
newMachine.setAttributes(singletonMap("att1", "val"));
final EnvironmentImpl newEnv = new EnvironmentImpl();
newEnv.setMachines(ImmutableMap.of("new-machine", newMachine));
newEnv.setRecipe(newRecipe);
workspace.getConfig().getEnvironments().put("new-env", newEnv);
// Update an existing environment
final EnvironmentImpl defaultEnv = workspace.getConfig().getEnvironments().get(workspace.getConfig().getDefaultEnv());
// Remove an existing machine config
final List<String> machineNames = new ArrayList<>(defaultEnv.getMachines().keySet());
// Update an existing machine
final ExtendedMachineImpl existingMachine = defaultEnv.getMachines().get(machineNames.get(1));
existingMachine.setAgents(asList("new-agent1", "new-agent2"));
existingMachine.setAttributes(ImmutableMap.of("attr1", "value1", "attr2", "value2", "attr3", "value3"));
existingMachine.getServers().clear();
existingMachine.getServers().put("new-ref", new ServerConf2Impl("new-port", "new-protocol", ImmutableMap.of("prop1", "value")));
defaultEnv.getMachines().remove(machineNames.get(0));
defaultEnv.getRecipe().setContent("updated-content");
defaultEnv.getRecipe().setContentType("updated-content-type");
defaultEnv.getRecipe().setLocation("updated-location");
defaultEnv.getRecipe().setType("updated-type");
// Remove an existing environment
final Optional<String> nonDefaultEnv = workspace.getConfig().getEnvironments().keySet().stream().filter(key -> !key.equals(workspace.getConfig().getDefaultEnv())).findAny();
assertTrue(nonDefaultEnv.isPresent());
workspace.getConfig().getEnvironments().remove(nonDefaultEnv.get());
// Update workspace configuration
final WorkspaceConfigImpl wCfg = workspace.getConfig();
wCfg.setDefaultEnv("new-env");
wCfg.setName("new-name");
wCfg.setDescription("This is a new description");
// Update workspace object
workspace.setAccount(new AccountImpl("accId", "new-namespace", "test"));
workspace.getAttributes().clear();
workspaceDao.update(workspace);
assertEquals(workspaceDao.get(workspace.getId()), new WorkspaceImpl(workspace, workspace.getAccount()));
}
use of java.util.Optional in project hbase by apache.
the class AsyncMetaTableAccessor method getTableState.
public static CompletableFuture<Optional<TableState>> getTableState(RawAsyncTable metaTable, TableName tableName) {
CompletableFuture<Optional<TableState>> future = new CompletableFuture<>();
Get get = new Get(tableName.getName()).addColumn(getTableFamily(), getStateColumn());
long time = EnvironmentEdgeManager.currentTime();
try {
get.setTimeRange(0, time);
metaTable.get(get).whenComplete((result, error) -> {
if (error != null) {
future.completeExceptionally(error);
return;
}
try {
future.complete(getTableState(result));
} catch (IOException e) {
future.completeExceptionally(e);
}
});
} catch (IOException ioe) {
future.completeExceptionally(ioe);
}
return future;
}
use of java.util.Optional in project cas by apereo.
the class AbstractCasWebflowConfigurer method createFlowVariable.
/**
* Create flow variable flow variable.
*
* @param flow the flow
* @param id the id
* @param type the type
* @return the flow variable
*/
protected FlowVariable createFlowVariable(final Flow flow, final String id, final Class type) {
final Optional<FlowVariable> opt = Arrays.stream(flow.getVariables()).filter(v -> v.getName().equalsIgnoreCase(id)).findFirst();
if (opt.isPresent()) {
return opt.get();
}
final FlowVariable flowVar = new FlowVariable(id, new BeanFactoryVariableValueFactory(type, applicationContext.getAutowireCapableBeanFactory()));
flow.addVariable(flowVar);
return flowVar;
}
use of java.util.Optional in project cryptomator by cryptomator.
the class SingleInstanceManager method getRemoteInstance.
/**
* Checks if there is a valid port at
* {@link Preferences#userNodeForPackage(Class)} for {@link Cryptomator} under the
* given applicationKey, tries to connect to the port at the loopback
* address and checks if the port identifies with the applicationKey.
*
* @param applicationKey
* key used to load the port and check the identity of the
* connection.
* @return
*/
public static Optional<RemoteInstance> getRemoteInstance(String applicationKey) {
Optional<Integer> port = getSavedPort(applicationKey);
if (!port.isPresent()) {
return Optional.empty();
}
SocketChannel channel = null;
boolean close = true;
try {
channel = SocketChannel.open();
channel.configureBlocking(false);
LOG.debug("connecting to instance {}", port.get());
channel.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), port.get()));
SocketChannel fChannel = channel;
if (!TimeoutTask.attempt(t -> fChannel.finishConnect(), 1000, 10)) {
return Optional.empty();
}
LOG.debug("connected to instance {}", port.get());
final byte[] bytes = applicationKey.getBytes(StandardCharsets.UTF_8);
ByteBuffer buf = ByteBuffer.allocate(bytes.length);
tryFill(channel, buf, 1000);
if (buf.hasRemaining()) {
return Optional.empty();
}
buf.flip();
for (int i = 0; i < bytes.length; i++) {
if (buf.get() != bytes[i]) {
return Optional.empty();
}
}
close = false;
return Optional.of(new RemoteInstance(channel));
} catch (Exception e) {
return Optional.empty();
} finally {
if (close) {
IOUtils.closeQuietly(channel);
}
}
}
use of java.util.Optional in project buck by facebook.
the class GoTest method runTests.
@Override
public ImmutableList<Step> runTests(ExecutionContext executionContext, TestRunningOptions options, SourcePathResolver pathResolver, TestReportingCallback testReportingCallback) {
Optional<Long> processTimeoutMs = testRuleTimeoutMs.isPresent() ? Optional.of(testRuleTimeoutMs.get() + PROCESS_TIMEOUT_EXTRA_MS) : Optional.empty();
ImmutableList.Builder<String> args = ImmutableList.builder();
args.addAll(testMain.getExecutableCommand().getCommandPrefix(pathResolver));
args.add("-test.v");
if (testRuleTimeoutMs.isPresent()) {
args.add("-test.timeout", testRuleTimeoutMs.get() + "ms");
}
return ImmutableList.of(new MakeCleanDirectoryStep(getProjectFilesystem(), getPathToTestOutputDirectory()), new MakeCleanDirectoryStep(getProjectFilesystem(), getPathToTestWorkingDirectory()), new SymlinkTreeStep(getProjectFilesystem(), getPathToTestWorkingDirectory(), ImmutableMap.copyOf(FluentIterable.from(resources).transform(input -> Maps.immutableEntry(getProjectFilesystem().getPath(pathResolver.getSourcePathName(getBuildTarget(), input)), pathResolver.getAbsolutePath(input))))), new GoTestStep(getProjectFilesystem(), getPathToTestWorkingDirectory(), args.build(), testMain.getExecutableCommand().getEnvironment(pathResolver), getPathToTestExitCode(), processTimeoutMs, getPathToTestResults()));
}
Aggregations