use of org.eclipse.emfcloud.jackson.resource.JsonResource in project emfcloud-modelserver by eclipse-emfcloud.
the class DefaultModelControllerTest method executeCommand.
@Test
public void executeCommand() throws EncodingException, DecodingException {
ResourceSet rset = new ResourceSetImpl();
String modeluri = "SuperBrewer3000.json";
JsonResource res = createJsonResource(modeluri);
rset.getResources().add(res);
final EClass task = EcoreFactory.eINSTANCE.createEClass();
res.getContents().add(task);
CCommand setCommand = CCommandFactory.eINSTANCE.createCommand();
setCommand.setType(EMFCommandType.SET);
setCommand.setOwner(task);
setCommand.setFeature("name");
setCommand.getDataValues().add("Foo");
JsonResource cmdRes = new JsonResource(URI.createURI("$command.json"));
cmdRes.getContents().add(setCommand);
final LinkedHashMap<String, List<String>> queryParams = new LinkedHashMap<>();
queryParams.put(ModelServerPathParametersV1.MODEL_URI, Collections.singletonList(modeluri));
when(context.queryParamMap()).thenReturn(queryParams);
when(context.body()).thenReturn(Json.object(Json.prop(JsonResponseMember.DATA, new JsonCodec().encode(setCommand))).toString());
when(modelRepository.getModel(modeluri)).thenReturn(Optional.of(task));
CCommandExecutionResult result = CCommandFactory.eINSTANCE.createCommandExecutionResult();
result.setSource(EcoreUtil.copy(setCommand));
result.setType(CommandExecutionType.EXECUTE);
when(modelRepository.executeCommand(eq(modeluri), any(CCommand.class))).thenReturn(result);
modelController.executeCommand(context, modeluri);
// unload to proxify
res.unload();
verify(modelRepository).executeCommand(eq(modeluri), argThat(eEqualTo(setCommand)));
// No subscribers registered for this incrementalUpdate, therefore no pre-encoded commands are created and the map
// can remain empty for this test
verify(sessionController).commandExecuted(eq(modeluri), eq(Suppliers.ofInstance(result)), anySupplier());
}
use of org.eclipse.emfcloud.jackson.resource.JsonResource in project emfcloud-modelserver by eclipse-emfcloud.
the class DefaultJsonCodec method decode.
@Override
public Optional<EObject> decode(final String payload, final URI workspaceURI) throws DecodingException {
URI uri = URI.createURI("virtual.json");
if (workspaceURI != null) {
uri = uri.resolve(workspaceURI);
}
final JsonResource jsonResource = new JsonResource(uri, getObjectMapper());
try (InputStream input = new ByteArrayInputStream(payload.getBytes())) {
jsonResource.load(input, null);
} catch (IOException e) {
throw new DecodingException(new JSONException(e, JsonLocation.NA));
}
return Optional.of(jsonResource.getContents().remove(0));
}
use of org.eclipse.emfcloud.jackson.resource.JsonResource in project emfcloud-modelserver by eclipse-emfcloud.
the class DefaultModelControllerTest method undoRedo.
@Test
public void undoRedo() throws EncodingException, DecodingException {
ResourceSet rset = new ResourceSetImpl();
String modeluri = "SuperBrewer3000.json";
JsonResource res = createJsonResource(modeluri);
rset.getResources().add(res);
final EClass eClass = EcoreFactory.eINSTANCE.createEClass();
res.getContents().add(eClass);
final EAttribute attribute = EcoreFactory.eINSTANCE.createEAttribute();
CCommand addCommand = CCommandFactory.eINSTANCE.createCommand();
addCommand.setType(EMFCommandType.ADD);
addCommand.setOwner(eClass);
addCommand.setFeature("eAttributes");
addCommand.getObjectsToAdd().add(attribute);
addCommand.getObjectValues().add(attribute);
JsonResource cmdRes = new JsonResource(URI.createURI("$command.json"));
cmdRes.getContents().add(addCommand);
String commandAsString = Json.object(Json.prop(JsonResponseMember.DATA, Json.text(new JsonCodec().encode(addCommand).toString()))).toString();
final LinkedHashMap<String, List<String>> queryParams = new LinkedHashMap<>();
queryParams.put(ModelServerPathParametersV2.MODEL_URI, Collections.singletonList(modeluri));
// This param is important to indicate that the result should include the JSON Patch
queryParams.put(ModelServerPathParametersV2.FORMAT, List.of(ModelServerPathParametersV2.FORMAT_JSON_V2));
when(context.queryParamMap()).thenReturn(queryParams);
when(context.body()).thenReturn(commandAsString);
when(context.matchedPath()).thenReturn("/api/v2/undo", "/api/v2/redo");
when(modelRepository.getModel(modeluri)).thenReturn(Optional.of(attribute));
CCommandExecutionResult result = CCommandFactory.eINSTANCE.createCommandExecutionResult();
result.setSource(EcoreUtil.copy(addCommand));
result.setType(CommandExecutionType.EXECUTE);
when(modelRepository.executeCommand(eq(modeluri), any(CCommand.class))).thenReturn(result);
when(modelRepository.undo(modeluri)).thenReturn(Optional.of(CCommandFactory.eINSTANCE.createCommandExecutionResult()));
when(modelRepository.redo(modeluri)).thenReturn(Optional.of(CCommandFactory.eINSTANCE.createCommandExecutionResult()));
when(jsonPatchHelper.getJsonPatch(any(), any())).thenReturn(Json.array(Json.object(Map.of("op", Json.text("add")))), Json.array(Json.object(Map.of("op", Json.text("remove")))), Json.array(Json.object(Map.of("op", Json.text("add")))));
modelController.executeCommand(context, modeluri);
// unload to proxify
res.unload();
// No subscribers registered for this incrementalUpdate, therefore no pre-encoded commands are created and the map
// can be remain for this test
modelController.undo(context, modeluri);
modelController.redo(context, modeluri);
// Verify the execution
verify(context).json(argThat(jsonStringThat(CoreMatchers.containsString("successfully updated"))));
// And the undo/redo
verify(context, times(2)).json(argThat(successWithDataThat(jsonStringThat(CoreMatchers.containsString("\"op\":")))));
}
use of org.eclipse.emfcloud.jackson.resource.JsonResource in project emfcloud-modelserver by eclipse-emfcloud.
the class DefaultModelControllerTest method addCommandNotification.
@Test
public void addCommandNotification() throws EncodingException, DecodingException {
ResourceSet rset = new ResourceSetImpl();
String modeluri = "SuperBrewer3000.json";
JsonResource res = createJsonResource(modeluri);
rset.getResources().add(res);
final EClass eClass = EcoreFactory.eINSTANCE.createEClass();
res.getContents().add(eClass);
final EAttribute attribute = EcoreFactory.eINSTANCE.createEAttribute();
CCommand addCommand = CCommandFactory.eINSTANCE.createCommand();
addCommand.setType(EMFCommandType.ADD);
addCommand.setOwner(eClass);
addCommand.setFeature("eAttributes");
addCommand.getObjectsToAdd().add(attribute);
addCommand.getObjectValues().add(attribute);
JsonResource cmdRes = new JsonResource(URI.createURI("$command.json"));
cmdRes.getContents().add(addCommand);
String commandAsString = Json.object(Json.prop(JsonResponseMember.DATA, Json.text(new JsonCodec().encode(addCommand).toString()))).toString();
final LinkedHashMap<String, List<String>> queryParams = new LinkedHashMap<>();
queryParams.put(ModelServerPathParametersV1.MODEL_URI, Collections.singletonList(modeluri));
when(context.queryParamMap()).thenReturn(queryParams);
when(context.body()).thenReturn(commandAsString);
when(modelRepository.getModel(modeluri)).thenReturn(Optional.of(attribute));
CCommandExecutionResult result = CCommandFactory.eINSTANCE.createCommandExecutionResult();
result.setSource(EcoreUtil.copy(addCommand));
result.setType(CommandExecutionType.EXECUTE);
when(modelRepository.executeCommand(eq(modeluri), any(CCommand.class))).thenReturn(result);
modelController.executeCommand(context, modeluri);
// unload to proxify
res.unload();
verify(modelRepository).executeCommand(eq(modeluri), argThat(eEqualTo(addCommand)));
// No subscribers registered for this incrementalUpdate, therefore no pre-encoded commands are created and the map
// can be remain for this test
verify(sessionController).commandExecuted(eq(modeluri), eq(Suppliers.ofInstance(result)), anySupplier());
}
use of org.eclipse.emfcloud.jackson.resource.JsonResource in project emfcloud-modelserver by eclipse-emfcloud.
the class ModelServerClientV1Test method edit.
@Test
@SuppressWarnings({ "checkstyle:ThrowsCount" })
public void edit() throws EncodingException, ExecutionException, InterruptedException, MalformedURLException {
EClass eClass = EcoreFactory.eINSTANCE.createEClass();
((InternalEObject) eClass).eSetProxyURI(URI.createURI("SuperBrewer3000.json#//workflows.0"));
CCommand add = CCommandFactory.eINSTANCE.createCommand();
add.setType(EMFCommandType.ADD);
add.setOwner(eClass);
add.setFeature("eAttributes");
add.getObjectsToAdd().add(EcoreFactory.eINSTANCE.createEAttribute());
add.getObjectValues().addAll(add.getObjectsToAdd());
JsonResource cmdRes = new JsonResource(URI.createURI("$command.json"));
cmdRes.getContents().add(add);
final JsonNode expected = jsonCodec.encode(add);
// Don't unload because that creates proxies
cmdRes.getContents().clear();
// Issue #115: Ensure correct JSON encoding
assertThat(expected.toString(), containsString("\"type\":\"add\""));
assertThat(expected.toString(), containsString("\"objectValues\":[{\"eClass\":"));
String editUrl = baseHttpUrlBuilder.addPathSegment(ModelServerPathsV1.EDIT).addQueryParameter(ModelServerPathParametersV1.MODEL_URI, "SuperBrewer3000.json").addQueryParameter(ModelServerPathParametersV1.FORMAT, ModelServerPathParametersV1.FORMAT_JSON).build().toString();
interceptor.addRule().url(editUrl).patch().answer(request -> {
Buffer buffer = new Buffer();
try {
request.body().writeTo(buffer);
} catch (IOException e) {
e.printStackTrace();
fail("Failed to capture request body content: " + e.getMessage());
}
// The resulting string is escaped as though for a Java string literal
String body = buffer.readString(Charsets.UTF_8).replace("\\\\", "\\").replace("\\\"", "\"");
// This is the test's assertion
if (body.contains(expected.toString())) {
return new Rule.Builder().respond(JsonResponse.success("confirmed").toString());
}
return new Rule.Builder().respond(JsonResponse.error().toString());
});
ModelServerClientV1 client = createClient();
final CompletableFuture<Response<Boolean>> f = client.edit("SuperBrewer3000.json", add, ModelServerPathParametersV1.FORMAT_JSON);
assertThat(f.get().body(), is(true));
}
Aggregations