Search in sources :

Example 1 with JsonResource

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());
}
Also used : EClass(org.eclipse.emf.ecore.EClass) JsonCodec(org.eclipse.emfcloud.modelserver.emf.common.codecs.JsonCodec) CCommandExecutionResult(org.eclipse.emfcloud.modelserver.command.CCommandExecutionResult) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) CCommand(org.eclipse.emfcloud.modelserver.command.CCommand) JsonResource(org.eclipse.emfcloud.jackson.resource.JsonResource) List(java.util.List) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 2 with JsonResource

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));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JSONException(org.eclipse.emfcloud.jackson.errors.JSONException) JsonResource(org.eclipse.emfcloud.jackson.resource.JsonResource) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI)

Example 3 with JsonResource

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\":")))));
}
Also used : EClass(org.eclipse.emf.ecore.EClass) EAttribute(org.eclipse.emf.ecore.EAttribute) JsonCodec(org.eclipse.emfcloud.modelserver.emf.common.codecs.JsonCodec) CCommandExecutionResult(org.eclipse.emfcloud.modelserver.command.CCommandExecutionResult) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) CCommand(org.eclipse.emfcloud.modelserver.command.CCommand) JsonResource(org.eclipse.emfcloud.jackson.resource.JsonResource) List(java.util.List) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 4 with JsonResource

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());
}
Also used : EClass(org.eclipse.emf.ecore.EClass) EAttribute(org.eclipse.emf.ecore.EAttribute) JsonCodec(org.eclipse.emfcloud.modelserver.emf.common.codecs.JsonCodec) CCommandExecutionResult(org.eclipse.emfcloud.modelserver.command.CCommandExecutionResult) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) CCommand(org.eclipse.emfcloud.modelserver.command.CCommand) JsonResource(org.eclipse.emfcloud.jackson.resource.JsonResource) List(java.util.List) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 5 with JsonResource

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));
}
Also used : Buffer(okio.Buffer) CCommand(org.eclipse.emfcloud.modelserver.command.CCommand) JsonResource(org.eclipse.emfcloud.jackson.resource.JsonResource) JsonNode(com.fasterxml.jackson.databind.JsonNode) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IOException(java.io.IOException) JsonResponse(org.eclipse.emfcloud.modelserver.emf.common.JsonResponse) Response(org.eclipse.emfcloud.modelserver.client.Response) EClass(org.eclipse.emf.ecore.EClass) Rule(okhttp3.mock.Rule) InternalEObject(org.eclipse.emf.ecore.InternalEObject) Test(org.junit.Test)

Aggregations

JsonResource (org.eclipse.emfcloud.jackson.resource.JsonResource)6 EClass (org.eclipse.emf.ecore.EClass)4 CCommand (org.eclipse.emfcloud.modelserver.command.CCommand)4 Test (org.junit.Test)4 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)3 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)3 CCommandExecutionResult (org.eclipse.emfcloud.modelserver.command.CCommandExecutionResult)3 JsonCodec (org.eclipse.emfcloud.modelserver.emf.common.codecs.JsonCodec)3 IOException (java.io.IOException)2 EAttribute (org.eclipse.emf.ecore.EAttribute)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Rule (okhttp3.mock.Rule)1 Buffer (okio.Buffer)1 URI (org.eclipse.emf.common.util.URI)1 InternalEObject (org.eclipse.emf.ecore.InternalEObject)1 JSONException (org.eclipse.emfcloud.jackson.errors.JSONException)1