AddNamedViewHandler implements OperationHandler<AddNamedView> { @Override public Object doOperation(final AddNamedView operation, final Context context, final Store store) throws OperationException { if (null == operation.getName() || operation.getName().isEmpty()) { throw new IllegalArgumentException("NamedView name must be set and not empty"); } final NamedViewDetail namedViewDetail = new NamedViewDetail.Builder() .name(operation.getName()) .view(operation.getViewAsString()) .description(operation.getDescription()) .creatorId(context.getUser().getUserId()) .writers(operation.getWriteAccessRoles()) .parameters(operation.getParameters()) .readAccessPredicate(operation.getReadAccessPredicate()) .writeAccessPredicate(operation.getWriteAccessPredicate()) .build(); validate(namedViewDetail.getViewWithDefaultParams(), namedViewDetail); try { cache.addNamedView(namedViewDetail, operation.isOverwriteFlag(), context.getUser(), store.getProperties().getAdminAuth()); } catch (final CacheOperationFailedException e) { throw new OperationException(e.getMessage(), e); } return null; } AddNamedViewHandler();  AddNamedViewHandler(final NamedViewCache cache); @Override Object doOperation(final AddNamedView operation, final Context context, final Store store);  }
@Test public void shouldAddNamedViewCorrectly() throws OperationException, CacheOperationFailedException { handler.doOperation(addNamedView, context, store); final NamedViewDetail result = namedViewCache.getNamedView(testNamedViewName); assertTrue(cacheContains(testNamedViewName)); assertEquals(addNamedView.getName(), result.getName()); assertEquals(new String(addNamedView.getView().toCompactJson()), result.getView()); assertEquals(context.getUser().getUserId(), result.getCreatorId()); assertEquals(new UnrestrictedAccessPredicate(), result.getReadAccessPredicate()); final AccessPredicate expectedWriteAccessPredicate = new NamedViewWriteAccessPredicate(context.getUser(), Arrays.asList(writeAccessRoles)); assertEquals(expectedWriteAccessPredicate, result.getWriteAccessPredicate()); }
@Test public void shouldAddNamedViewContainingCustomAccessPredicatesCorrectly() throws OperationException, CacheOperationFailedException { final AccessPredicate readAccessPredicate = new AccessPredicate(new CustomUserPredicate()); final AccessPredicate writeAccessPredicate = new AccessPredicate(new CustomUserPredicate()); addNamedView.setReadAccessPredicate(readAccessPredicate); addNamedView.setWriteAccessPredicate(writeAccessPredicate); handler.doOperation(addNamedView, context, store); final NamedViewDetail result = namedViewCache.getNamedView(testNamedViewName); assertTrue(cacheContains(testNamedViewName)); assertEquals(addNamedView.getName(), result.getName()); assertEquals(new String(addNamedView.getView().toCompactJson()), result.getView()); assertEquals(context.getUser().getUserId(), result.getCreatorId()); assertEquals(readAccessPredicate, result.getReadAccessPredicate()); assertEquals(writeAccessPredicate, result.getWriteAccessPredicate()); }
@Test public void shouldNotAddNamedViewWithNoName() throws OperationException { addNamedView.setName(null); try { handler.doOperation(addNamedView, context, store); } catch (final IllegalArgumentException e) { assertTrue(e.getMessage().equals("NamedView name must be set and not empty")); } }
@Test public void shouldNotAddNestedNamedView() throws OperationException { final NamedView nestedNamedView = new NamedView.Builder() .name(testNamedViewName + 1) .edge(TestGroups.EDGE) .build(); addNamedView = new AddNamedView.Builder() .name(testNamedViewName) .view(nestedNamedView) .overwrite(false) .build(); try { handler.doOperation(addNamedView, context, store); } catch (final OperationException e) { assertTrue(e.getMessage().equals("NamedView can not be nested within NamedView")); } }