Exemplo n.º 1
0
  @Test
  public void testChildren() throws RepositoryException {
    when(mockRes1.getPath()).thenReturn(RDF_PATH + "/res1");
    when(mockRes2.getPath()).thenReturn(RDF_PATH + "/res2");
    when(mockRes3.getPath()).thenReturn(RDF_PATH + "/res3");
    when(mockResourceNode.hasNodes()).thenReturn(true);
    final Stream<FedoraResource> first = of(mockRes1, mockRes2, mockRes3);
    final Stream<FedoraResource> second = of(mockRes1, mockRes2, mockRes3);
    when(mockResource.getChildren()).thenReturn(first).thenReturn(second);

    try (final ChildrenRdfContext context = new ChildrenRdfContext(mockResource, idTranslator)) {
      final Model results = context.collect(toModel());
      final Resource subject = idTranslator.reverse().convert(mockResource);

      final StmtIterator stmts =
          results.listStatements(subject, RdfLexicon.HAS_CHILD_COUNT, (RDFNode) null);

      assertTrue("There should have been a statement!", stmts.hasNext());
      final Statement stmt = stmts.nextStatement();
      assertTrue("Object should be a literal! " + stmt.getObject(), stmt.getObject().isLiteral());
      assertEquals(3, stmt.getInt());

      assertFalse("There should not have been a second statement!", stmts.hasNext());
    }
  }
  @Test
  public void acl02Test() throws RepositoryException {
    final String agent = "Editors";
    final String accessTo = "/box/bag/collection";
    final String acl = "/acls/02";
    final String auth = acl + "/authorization.ttl";

    when(mockNodeService.find(mockSession, acl)).thenReturn(mockAclResource);
    when(mockProperty.getString()).thenReturn(acl);
    when(mockAclResource.getPath()).thenReturn(acl);
    when(mockResource.getPath()).thenReturn(accessTo);
    when(mockResource.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getResourceRdfStream(accessTo, acl));

    when(mockAuthorizationResource1.getTypes()).thenReturn(Arrays.asList(WEBAC_AUTHORIZATION));
    when(mockAuthorizationResource1.getPath()).thenReturn(auth);
    when(mockAuthorizationResource1.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getRdfStreamFromResource(auth, TTL));

    when(mockAclResource.getChildren())
        .thenReturn(Arrays.asList(mockAuthorizationResource1).iterator());

    final Map<String, List<String>> roles = roleProvider.getRoles(mockNode, true);

    assertEquals("There should be exactly one agent in the role map", 1, roles.size());
    assertEquals("The agent should have exactly two modes", 2, roles.get(agent).size());
    assertTrue(
        "The agent should be able to read", roles.get(agent).contains(WEBAC_MODE_READ_VALUE));
    assertTrue(
        "The agent should be able to write", roles.get(agent).contains(WEBAC_MODE_WRITE_VALUE));
  }
  @Test
  public void acl01Test2() throws RepositoryException {
    final String accessTo = "/webacl_box2";
    final String acl = "/acls/01";
    final String auth = acl + "/authorization.ttl";

    when(mockNodeService.find(mockSession, acl)).thenReturn(mockAclResource);
    when(mockProperty.getString()).thenReturn(acl);
    when(mockAclResource.getPath()).thenReturn(acl);
    when(mockResource.getPath()).thenReturn(accessTo);
    when(mockResource.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getResourceRdfStream(accessTo, acl));

    when(mockAuthorizationResource1.getTypes()).thenReturn(Arrays.asList(WEBAC_AUTHORIZATION));
    when(mockAuthorizationResource1.getPath()).thenReturn(auth);
    when(mockAuthorizationResource1.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getRdfStreamFromResource(auth, TTL));

    when(mockAclResource.getChildren())
        .thenReturn(Arrays.asList(mockAuthorizationResource1).iterator());

    final Map<String, List<String>> roles = roleProvider.getRoles(mockNode, true);

    assertTrue("There should be no agents associated with this object", roles.isEmpty());
  }
  /* (non-Javadoc)
   * Test that an in-repository resource used as a target for acl:agentClass has
   * the rdf:type of foaf:Group. This test mocks a foaf:Group resource and should
   * therefore retrieve two agents.
   */
  @Test
  public void acl09Test1() throws RepositoryException {
    final String agent1 = "person1";
    final String agent2 = "person2";
    final String accessTo = "/anotherCollection";

    final String groupResource = "/group/foo";
    final String acl = "/acls/09";
    final String auth = acl + "/authorization.ttl";
    final String group = acl + "/group.ttl";

    when(mockNodeService.find(mockSession, acl)).thenReturn(mockAclResource);
    when(mockNodeService.find(mockSession, groupResource)).thenReturn(mockAgentClassResource);
    when(mockProperty.getString()).thenReturn(acl);
    when(mockAclResource.getPath()).thenReturn(acl);
    when(mockResource.getPath()).thenReturn(accessTo);
    when(mockResource.getTypes()).thenReturn(new ArrayList<>());
    when(mockResource.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getResourceRdfStream(accessTo, acl));

    when(mockAuthorizationResource1.getTypes()).thenReturn(Arrays.asList(WEBAC_AUTHORIZATION));
    when(mockAuthorizationResource1.getPath()).thenReturn(auth);
    when(mockAuthorizationResource1.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getRdfStreamFromResource(auth, TTL));

    when(mockAgentClassResource.getTypes()).thenReturn(Arrays.asList(FOAF_GROUP));
    when(mockAgentClassResource.getPath()).thenReturn(groupResource);
    when(mockAgentClassResource.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getRdfStreamFromResource(group, TTL));

    when(mockAclResource.getChildren())
        .thenReturn(Arrays.asList(mockAuthorizationResource1).iterator());

    final Map<String, List<String>> roles = roleProvider.getRoles(mockNode, true);

    assertEquals("There should be exactly two agents", 2, roles.size());
    assertEquals("The agent should have two modes", 2, roles.get(agent1).size());
    assertTrue(
        "The agent should be able to read", roles.get(agent1).contains(WEBAC_MODE_READ_VALUE));
    assertTrue(
        "The agent should be able to write", roles.get(agent1).contains(WEBAC_MODE_WRITE_VALUE));
  }
  @Test
  public void acl04Test() throws RepositoryException {
    final String agent1 = "http://xmlns.com/foaf/0.1/Agent";
    final String agent2 = "Editors";
    final String accessTo = "/public_collection";
    final String acl = "/acls/04";
    final String auth1 = acl + "/auth1.ttl";
    final String auth2 = acl + "/auth2.ttl";

    when(mockNodeService.find(mockSession, acl)).thenReturn(mockAclResource);
    when(mockProperty.getString()).thenReturn(acl);
    when(mockAclResource.getPath()).thenReturn(acl);
    when(mockResource.getPath()).thenReturn(accessTo);
    when(mockResource.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getResourceRdfStream(accessTo, acl));

    when(mockAuthorizationResource1.getTypes()).thenReturn(Arrays.asList(WEBAC_AUTHORIZATION));
    when(mockAuthorizationResource1.getPath()).thenReturn(auth1);
    when(mockAuthorizationResource1.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getRdfStreamFromResource(auth1, TTL));

    when(mockAuthorizationResource2.getTypes()).thenReturn(Arrays.asList(WEBAC_AUTHORIZATION));
    when(mockAuthorizationResource2.getPath()).thenReturn(auth2);
    when(mockAuthorizationResource2.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getRdfStreamFromResource(auth2, TTL));

    when(mockAclResource.getChildren())
        .thenReturn(
            Arrays.asList(mockAuthorizationResource1, mockAuthorizationResource2).iterator());

    final Map<String, List<String>> roles = roleProvider.getRoles(mockNode, true);

    assertEquals("There should be exactly two agents", 2, roles.size());
    assertEquals("The agent should have one mode", 1, roles.get(agent1).size());
    assertTrue(
        "The agent should be able to read", roles.get(agent1).contains(WEBAC_MODE_READ_VALUE));
    assertEquals("The agent should have two modes", 2, roles.get(agent2).size());
    assertTrue(
        "The agent should be able to read", roles.get(agent2).contains(WEBAC_MODE_READ_VALUE));
    assertTrue(
        "The agent should be able to write", roles.get(agent2).contains(WEBAC_MODE_READ_VALUE));
  }
  @Test
  public void acl03Test2() throws RepositoryException {
    final String agent = "Restricted";
    final String accessTo = "/dark/archive";
    final String acl = "/acls/03";
    final String auth1 = acl + "/auth_restricted.ttl";
    final String auth2 = acl + "/auth_open.ttl";

    when(mockNodeService.find(mockSession, acl)).thenReturn(mockAclResource);
    when(mockProperty.getString()).thenReturn(acl);
    when(mockAclResource.getPath()).thenReturn(acl);
    when(mockResource.getPath()).thenReturn(accessTo);
    when(mockResource.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getResourceRdfStream(accessTo, acl));

    when(mockAuthorizationResource1.getTypes()).thenReturn(Arrays.asList(WEBAC_AUTHORIZATION));
    when(mockAuthorizationResource1.getPath()).thenReturn(auth1);
    when(mockAuthorizationResource1.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getRdfStreamFromResource(auth1, TTL));

    when(mockAuthorizationResource2.getTypes()).thenReturn(Arrays.asList(WEBAC_AUTHORIZATION));
    when(mockAuthorizationResource2.getPath()).thenReturn(auth2);
    when(mockAuthorizationResource2.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getRdfStreamFromResource(auth2, TTL));

    when(mockAclResource.getChildren())
        .thenReturn(
            Arrays.asList(mockAuthorizationResource1, mockAuthorizationResource2).iterator());

    final Map<String, List<String>> roles = roleProvider.getRoles(mockNode, true);

    assertEquals("There should be exactly one agent", 1, roles.size());
    assertEquals("The agent should have one mode", 1, roles.get(agent).size());
    assertTrue(
        "The agent should be able to read", roles.get(agent).contains(WEBAC_MODE_READ_VALUE));
  }