@Test public void addCommandNotification() throws EncodingException, DecodingException { ResourceSet rset = new ResourceSetImpl(); when(modelRepository.getResourceSet()).thenReturn(rset); JsonResource res = new JsonResource( URI.createURI("SuperBrewer3000.json").resolve(serverConfiguration.getWorkspaceRootURI())); rset.getResources().add(res); final Workflow workflow = CoffeeFactory.eINSTANCE.createWorkflow(); res.getContents().add(workflow); final AutomaticTask task = CoffeeFactory.eINSTANCE.createAutomaticTask(); CCommand addCommand = CCommandFactory.eINSTANCE.createCommand(); addCommand.setType(CommandKind.ADD); addCommand.setOwner(workflow); addCommand.setFeature("nodes"); addCommand.getObjectsToAdd().add(task); addCommand.getObjectValues().add(task); JsonResource cmdRes = new JsonResource(URI.createURI("$command.json")); cmdRes.getContents().add(addCommand); String commandAsString = Json.object(Json.prop("data", Json.text(new JsonCodec().encode(addCommand).toString()))) .toString(); final LinkedHashMap<String, List<String>> queryParams = new LinkedHashMap<>(); queryParams.put("modeluri", Collections.singletonList("SuperBrewer3000.json")); when(context.queryParamMap()).thenReturn(queryParams); when(context.body()).thenReturn(commandAsString); when(modelRepository.getResourceSet()).thenReturn(rset); when(modelRepository.getModel("SuperBrewer3000.json")).thenReturn(Optional.of(task)); modelController.executeCommand(context, "SuperBrewer3000.json"); res.unload(); verify(modelRepository).updateModel(eq("SuperBrewer3000.json"), argThat(eEqualTo(addCommand))); verify(sessionController).modelChanged(eq("SuperBrewer3000.json"), argThat(eEqualTo(addCommand))); }
public void executeCommand(final Context ctx, final String modelURI) { this.modelRepository.getModel(modelURI).ifPresentOrElse( model -> { if (model == null) { handleError(ctx, 404, String.format("Model '%s' not found!", modelURI)); } else { readPayload(ctx).filter(CCommand.class::isInstance) .map(CCommand.class::cast) .ifPresent(cmd -> { try { URI uri = URI.createURI("$command.res") .resolve(serverConfiguration.getWorkspaceRootURI()); Resource resource = new ResourceImpl(uri); modelRepository.getResourceSet().getResources().add(resource); resource.getContents().add(cmd); try { EcoreUtil.resolveAll(resource); modelRepository.updateModel(modelURI, EcoreUtil.copy(cmd)); sessionController.modelChanged(modelURI, EcoreUtil.copy(cmd)); ctx.json(JsonResponse.success()); } finally { resource.unload(); modelRepository.getResourceSet().getResources().remove(resource); } } catch (DecodingException e) { handleDecodingError(ctx, e); } }); ctx.json(JsonResponse.success("Model '" + modelURI + "' successfully updated")); } }, () -> handleError(ctx, 404, String.format("Model '%s' not found!", modelURI))); }
ModelController { public void executeCommand(final Context ctx, final String modelURI) { this.modelRepository.getModel(modelURI).ifPresentOrElse( model -> { if (model == null) { handleError(ctx, 404, String.format("Model '%s' not found!", modelURI)); } else { readPayload(ctx).filter(CCommand.class::isInstance) .map(CCommand.class::cast) .ifPresent(cmd -> { try { URI uri = URI.createURI("$command.res") .resolve(serverConfiguration.getWorkspaceRootURI()); Resource resource = new ResourceImpl(uri); modelRepository.getResourceSet().getResources().add(resource); resource.getContents().add(cmd); try { EcoreUtil.resolveAll(resource); modelRepository.updateModel(modelURI, EcoreUtil.copy(cmd)); sessionController.modelChanged(modelURI, EcoreUtil.copy(cmd)); ctx.json(JsonResponse.success()); } finally { resource.unload(); modelRepository.getResourceSet().getResources().remove(resource); } } catch (DecodingException e) { handleDecodingError(ctx, e); } }); ctx.json(JsonResponse.success("Model '" + modelURI + "' successfully updated")); } }, () -> handleError(ctx, 404, String.format("Model '%s' not found!", modelURI))); } }
ModelController { public void executeCommand(final Context ctx, final String modelURI) { this.modelRepository.getModel(modelURI).ifPresentOrElse( model -> { if (model == null) { handleError(ctx, 404, String.format("Model '%s' not found!", modelURI)); } else { readPayload(ctx).filter(CCommand.class::isInstance) .map(CCommand.class::cast) .ifPresent(cmd -> { try { URI uri = URI.createURI("$command.res") .resolve(serverConfiguration.getWorkspaceRootURI()); Resource resource = new ResourceImpl(uri); modelRepository.getResourceSet().getResources().add(resource); resource.getContents().add(cmd); try { EcoreUtil.resolveAll(resource); modelRepository.updateModel(modelURI, EcoreUtil.copy(cmd)); sessionController.modelChanged(modelURI, EcoreUtil.copy(cmd)); ctx.json(JsonResponse.success()); } finally { resource.unload(); modelRepository.getResourceSet().getResources().remove(resource); } } catch (DecodingException e) { handleDecodingError(ctx, e); } }); ctx.json(JsonResponse.success("Model '" + modelURI + "' successfully updated")); } }, () -> handleError(ctx, 404, String.format("Model '%s' not found!", modelURI))); } @Inject  ModelController(final ModelRepository modelRepository, final SessionController sessionController,
      final ServerConfiguration serverConfiguration); }
ModelController { public void executeCommand(final Context ctx, final String modelURI) { this.modelRepository.getModel(modelURI).ifPresentOrElse( model -> { if (model == null) { handleError(ctx, 404, String.format("Model '%s' not found!", modelURI)); } else { readPayload(ctx).filter(CCommand.class::isInstance) .map(CCommand.class::cast) .ifPresent(cmd -> { try { URI uri = URI.createURI("$command.res") .resolve(serverConfiguration.getWorkspaceRootURI()); Resource resource = new ResourceImpl(uri); modelRepository.getResourceSet().getResources().add(resource); resource.getContents().add(cmd); try { EcoreUtil.resolveAll(resource); modelRepository.updateModel(modelURI, EcoreUtil.copy(cmd)); sessionController.modelChanged(modelURI, EcoreUtil.copy(cmd)); ctx.json(JsonResponse.success()); } finally { resource.unload(); modelRepository.getResourceSet().getResources().remove(resource); } } catch (DecodingException e) { handleDecodingError(ctx, e); } }); ctx.json(JsonResponse.success("Model '" + modelURI + "' successfully updated")); } }, () -> handleError(ctx, 404, String.format("Model '%s' not found!", modelURI))); } @Inject  ModelController(final ModelRepository modelRepository, final SessionController sessionController,
      final ServerConfiguration serverConfiguration); void create(final Context ctx, final String modeluri); void delete(final Context ctx, final String modeluri); void getAll(final Context ctx); void getOne(final Context ctx, final String modeluri); void update(final Context ctx, final String modeluri); void save(final Context ctx, final String modeluri); Handler getModelUrisHandler(); void executeCommand(final Context ctx, final String modelURI); }
ModelController { public void executeCommand(final Context ctx, final String modelURI) { this.modelRepository.getModel(modelURI).ifPresentOrElse( model -> { if (model == null) { handleError(ctx, 404, String.format("Model '%s' not found!", modelURI)); } else { readPayload(ctx).filter(CCommand.class::isInstance) .map(CCommand.class::cast) .ifPresent(cmd -> { try { URI uri = URI.createURI("$command.res") .resolve(serverConfiguration.getWorkspaceRootURI()); Resource resource = new ResourceImpl(uri); modelRepository.getResourceSet().getResources().add(resource); resource.getContents().add(cmd); try { EcoreUtil.resolveAll(resource); modelRepository.updateModel(modelURI, EcoreUtil.copy(cmd)); sessionController.modelChanged(modelURI, EcoreUtil.copy(cmd)); ctx.json(JsonResponse.success()); } finally { resource.unload(); modelRepository.getResourceSet().getResources().remove(resource); } } catch (DecodingException e) { handleDecodingError(ctx, e); } }); ctx.json(JsonResponse.success("Model '" + modelURI + "' successfully updated")); } }, () -> handleError(ctx, 404, String.format("Model '%s' not found!", modelURI))); } @Inject  ModelController(final ModelRepository modelRepository, final SessionController sessionController,
      final ServerConfiguration serverConfiguration); void create(final Context ctx, final String modeluri); void delete(final Context ctx, final String modeluri); void getAll(final Context ctx); void getOne(final Context ctx, final String modeluri); void update(final Context ctx, final String modeluri); void save(final Context ctx, final String modeluri); Handler getModelUrisHandler(); void executeCommand(final Context ctx, final String modelURI);  }