Search in sources :

Example 1 with EncodingException

use of org.eclipse.emfcloud.modelserver.common.codecs.EncodingException in project emfcloud-modelserver by eclipse-emfcloud.

the class DefaultModelControllerTest method getAllXmiFormat.

@Test
public void getAllXmiFormat() throws EncodingException, IOException {
    final AtomicReference<JsonNode> response = new AtomicReference<>();
    final EClass brewingUnit = EcoreFactory.eINSTANCE.createEClass();
    Answer<Void> answer = invocation -> {
        response.set(invocation.getArgument(0));
        return null;
    };
    doAnswer(answer).when(context).json(any(JsonNode.class));
    final LinkedHashMap<String, List<String>> queryParams = new LinkedHashMap<>();
    queryParams.put(ModelServerPathParametersV1.FORMAT, Collections.singletonList(ModelServerPathParametersV1.FORMAT_XMI));
    when(context.queryParamMap()).thenReturn(queryParams);
    final Map<URI, EObject> allModels = Collections.singletonMap(URI.createURI("test"), brewingUnit);
    when(modelRepository.getAllModels()).thenReturn(allModels);
    modelController.getAll(context);
    assertThat(response.get().get(JsonResponseMember.DATA), is(equalTo(Json.object(Json.prop("test", new XmiCodec().encode(brewingUnit))))));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) EcoreFactory(org.eclipse.emf.ecore.EcoreFactory) CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CCommandExecutionResult(org.eclipse.emfcloud.modelserver.command.CCommandExecutionResult) PatchCommandHandler(org.eclipse.emfcloud.modelserver.emf.patch.PatchCommandHandler) MockitoHamcrest.argThat(org.mockito.hamcrest.MockitoHamcrest.argThat) Context(io.javalin.http.Context) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) JsonPatchHelper(org.eclipse.emfcloud.modelserver.emf.util.JsonPatchHelper) JsonNode(com.fasterxml.jackson.databind.JsonNode) Json(org.eclipse.emfcloud.modelserver.jsonschema.Json) CCommandFactory(org.eclipse.emfcloud.modelserver.command.CCommandFactory) ServerConfiguration(org.eclipse.emfcloud.modelserver.emf.configuration.ServerConfiguration) JsonResource(org.eclipse.emfcloud.jackson.resource.JsonResource) EObject(org.eclipse.emf.ecore.EObject) Collectors(java.util.stream.Collectors) List(java.util.List) EcoreResourceFactoryImpl(org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl) Entry(java.util.Map.Entry) Resource(org.eclipse.emf.ecore.resource.Resource) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) DecodingException(org.eclipse.emfcloud.modelserver.common.codecs.DecodingException) EMFCommandType(org.eclipse.emfcloud.modelserver.edit.EMFCommandType) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ResourceImpl(org.eclipse.emf.ecore.resource.impl.ResourceImpl) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) URI(org.eclipse.emf.common.util.URI) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) EMFModule(org.eclipse.emfcloud.jackson.module.EMFModule) CommandExecutionType(org.eclipse.emfcloud.modelserver.edit.CommandExecutionType) CodecsManager(org.eclipse.emfcloud.modelserver.emf.common.codecs.CodecsManager) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) CustomTypeSafeMatcher(org.hamcrest.CustomTypeSafeMatcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) CCommand(org.eclipse.emfcloud.modelserver.command.CCommand) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) LinkedHashMap(java.util.LinkedHashMap) TypeSafeDiagnosingMatcher(org.hamcrest.TypeSafeDiagnosingMatcher) Json.prop(org.eclipse.emfcloud.modelserver.jsonschema.Json.prop) Answer(org.mockito.stubbing.Answer) EAttribute(org.eclipse.emf.ecore.EAttribute) EClass(org.eclipse.emf.ecore.EClass) Suppliers(com.google.common.base.Suppliers) EMFMatchers.eEqualTo(org.eclipse.emfcloud.modelserver.tests.util.EMFMatchers.eEqualTo) EncodingException(org.eclipse.emfcloud.modelserver.common.codecs.EncodingException) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) XmiCodec(org.eclipse.emfcloud.modelserver.common.codecs.XmiCodec) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) Description(org.hamcrest.Description) MockitoHamcrest.intThat(org.mockito.hamcrest.MockitoHamcrest.intThat) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) DICodecsManager(org.eclipse.emfcloud.modelserver.emf.common.codecs.DICodecsManager) ModelServerPathParametersV2(org.eclipse.emfcloud.modelserver.common.ModelServerPathParametersV2) FeatureMatcher(org.hamcrest.FeatureMatcher) ModelServerPathParametersV1(org.eclipse.emfcloud.modelserver.common.ModelServerPathParametersV1) Matcher(org.hamcrest.Matcher) OSUtil.osLineSeparator(org.eclipse.emfcloud.modelserver.tests.util.OSUtil.osLineSeparator) Collections(java.util.Collections) JsonCodec(org.eclipse.emfcloud.modelserver.emf.common.codecs.JsonCodec) JsonNode(com.fasterxml.jackson.databind.JsonNode) AtomicReference(java.util.concurrent.atomic.AtomicReference) URI(org.eclipse.emf.common.util.URI) LinkedHashMap(java.util.LinkedHashMap) EClass(org.eclipse.emf.ecore.EClass) EObject(org.eclipse.emf.ecore.EObject) List(java.util.List) XmiCodec(org.eclipse.emfcloud.modelserver.common.codecs.XmiCodec) Test(org.junit.Test)

Example 2 with EncodingException

use of org.eclipse.emfcloud.modelserver.common.codecs.EncodingException in project emfcloud-modelserver by eclipse-emfcloud.

the class DefaultModelController method getAll.

@Override
public void getAll(final Context ctx) {
    try {
        final Map<URI, EObject> allModels = this.modelRepository.getAllModels();
        Map<URI, JsonNode> encodedEntries = Maps.newLinkedHashMap();
        for (Map.Entry<URI, EObject> entry : allModels.entrySet()) {
            final JsonNode encoded = codecs.encode(ctx, entry.getValue());
            encodedEntries.put(entry.getKey(), encoded);
        }
        success(ctx, JsonCodec.encode(encodedEntries));
    } catch (EncodingException exception) {
        encodingError(ctx, exception);
    } catch (IOException exception) {
        internalError(ctx, "Could not load all models");
    }
}
Also used : EncodingException(org.eclipse.emfcloud.modelserver.common.codecs.EncodingException) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI) Map(java.util.Map)

Example 3 with EncodingException

use of org.eclipse.emfcloud.modelserver.common.codecs.EncodingException in project uml-glsp by eclipsesource.

the class UmlCodec method encode.

@Override
public JsonNode encode(final EObject eObject) throws EncodingException {
    Resource originalResource = eObject.eResource();
    URI originalURI = originalResource.getURI();
    // temporarily set different URI to serialize
    eObject.eResource().setURI(URI.createURI("virtual.uml"));
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        eObject.eResource().save(outputStream, Map.of(XMLResource.OPTION_KEEP_DEFAULT_CONTENT, Boolean.TRUE, XMLResource.OPTION_PROCESS_DANGLING_HREF, XMLResource.OPTION_PROCESS_DANGLING_HREF_DISCARD));
    } catch (IOException e) {
        throw new EncodingException(e);
    }
    // reset original URI
    originalResource.setURI(originalURI);
    return Json.text(outputStream.toString());
}
Also used : EncodingException(org.eclipse.emfcloud.modelserver.common.codecs.EncodingException) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) UMLResource(org.eclipse.uml2.uml.resource.UMLResource) Resource(org.eclipse.emf.ecore.resource.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI)

Example 4 with EncodingException

use of org.eclipse.emfcloud.modelserver.common.codecs.EncodingException in project emfcloud-modelserver by eclipse-emfcloud.

the class DefaultModelController method create.

@Override
public void create(final Context ctx, final String modeluri) {
    Optional<EObject> root = readPayload(ctx);
    if (root.isEmpty()) {
        return;
    }
    if (this.modelRepository.hasModel(modeluri)) {
        conflict(ctx, "Model already exists.");
        return;
    }
    try {
        this.modelRepository.addModel(modeluri, root.get());
        final JsonNode encoded = codecs.encode(ctx, root.get());
        success(ctx, encoded);
        this.sessionController.modelCreated(modeluri);
    } catch (EncodingException ex) {
        encodingError(ctx, ex);
    } catch (IOException ex) {
        internalError(ctx, "Could not save resource", ex);
    }
}
Also used : EncodingException(org.eclipse.emfcloud.modelserver.common.codecs.EncodingException) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 5 with EncodingException

use of org.eclipse.emfcloud.modelserver.common.codecs.EncodingException in project emfcloud-modelserver by eclipse-emfcloud.

the class DICommandCodec method serverToClient.

@Override
@SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" })
public CCommand serverToClient(final Command command) throws EncodingException {
    Command actualCommand = ModelServerCommand.unwrap(command);
    Optional<CCommand> userCommand = ModelServerCommand.getClientCommand(command);
    if (!userCommand.isPresent()) {
        throw new EncodingException("Cannot determine origin of command: " + command);
    }
    String commandType = userCommand.get().getType();
    CommandContribution codecContribution = typeToCodec.get(commandType);
    if (codecContribution == null) {
        throw new EncodingException("Unknown command type: " + commandType);
    }
    return codecContribution.serverToClient(actualCommand, userCommand.get());
}
Also used : Command(org.eclipse.emf.common.command.Command) CCommand(org.eclipse.emfcloud.modelserver.command.CCommand) CCommand(org.eclipse.emfcloud.modelserver.command.CCommand) EncodingException(org.eclipse.emfcloud.modelserver.common.codecs.EncodingException)

Aggregations

EncodingException (org.eclipse.emfcloud.modelserver.common.codecs.EncodingException)7 IOException (java.io.IOException)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 URI (org.eclipse.emf.common.util.URI)5 EObject (org.eclipse.emf.ecore.EObject)5 Map (java.util.Map)4 Resource (org.eclipse.emf.ecore.resource.Resource)4 CCommand (org.eclipse.emfcloud.modelserver.command.CCommand)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Suppliers (com.google.common.base.Suppliers)3 Context (io.javalin.http.Context)3 File (java.io.File)3 HttpURLConnection (java.net.HttpURLConnection)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 Entry (java.util.Map.Entry)3 Optional (java.util.Optional)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3