Java Code Examples for com.baidu.hugegraph.testutil.Assert#assertNull()

The following examples show how to use com.baidu.hugegraph.testutil.Assert#assertNull() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OrderLimitMapTest.java    From hugegraph-common with Apache License 2.0 6 votes vote down vote up
@Test
public void testMap() {
    OrderLimitMap<Integer, Double> map = new OrderLimitMap<>(5);
    map.put(4, 0.4);
    map.put(2, 0.2);
    map.put(5, 0.5);
    map.put(1, 0.2);
    map.put(3, 0.3);

    Assert.assertEquals(5, map.size());

    Assert.assertEquals(0.2, map.get(2), 1E-9);
    Assert.assertEquals(0.4, map.get(4), 1E-9);

    Assert.assertTrue(map.containsKey(1));
    Assert.assertTrue(map.containsKey(3));
    Assert.assertFalse(map.containsKey(6));

    Assert.assertNull(map.get(6));

    Assert.assertEquals(0.5, map.getOrDefault(5, 0.0), 1E-9);
    Assert.assertEquals(0.0, map.getOrDefault(7, 0.0), 1E-9);
}
 
Example 2
Source File: CacheTest.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@Test
public void testExpire() {
    Cache<Id, Object> cache = newCache();

    Assert.assertEquals(0L, cache.expire());
    cache.expire(2000L); // 2 seconds
    Assert.assertEquals(2000L, cache.expire());

    cache.update(IdGenerator.of("1"), "value-1");
    cache.update(IdGenerator.of("2"), "value-2");

    Assert.assertEquals(2, cache.size());

    waitTillNext(2);
    cache.tick();

    Assert.assertNull(cache.get(IdGenerator.of("1")));
    Assert.assertNull(cache.get(IdGenerator.of("2")));
    Assert.assertEquals(0, cache.size());
}
 
Example 3
Source File: UsersTest.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@Test
public void testMatchUser() {
    HugeGraph graph = graph();
    UserManager userManager = graph.userManager();

    String password = StringEncoding.hashPassword("pass1");
    userManager.createUser(makeUser("tom", password));

    Assert.assertNotNull(userManager.matchUser("tom", "pass1"));
    Assert.assertNull(userManager.matchUser("tom", "pass2"));
    Assert.assertNull(userManager.matchUser("Tom", "pass1"));

    Assert.assertThrows(IllegalArgumentException.class, () -> {
        userManager.matchUser("Tom", null);
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        userManager.matchUser(null, "pass1");
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        userManager.matchUser(null, null);
    });
}
 
Example 4
Source File: RestResultTest.java    From hugegraph-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadVertex() {
    String json = "{"
            + "\"id\": \"person:marko\","
            + "\"label\": \"person\","
            + "\"type\": \"vertex\","
            + "\"properties\": {"
            + "\"name\": \"marko\""
            + "}"
            + "}";

    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class))
           .thenReturn(json);
    RestResult result = new RestResult(this.mockResponse);
    Assert.assertEquals(200, result.status());
    Assert.assertNull(result.headers());

    Vertex vertex = result.readObject(Vertex.class);

    Assert.assertEquals("person:marko", vertex.id());
    Assert.assertEquals("person", vertex.label());
    Assert.assertEquals(ImmutableMap.of("name", "marko"),
                        vertex.properties());
}
 
Example 5
Source File: RestResultTest.java    From hugegraph-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadIndexLabel() {
    String json = "{"
            + "\"id\": \"4\","
            + "\"index_type\": \"SEARCH\","
            + "\"base_value\": \"software\","
            + "\"name\": \"softwareByPrice\","
            + "\"fields\": [\"price\"],"
            + "\"base_type\": \"VERTEX_LABEL\""
            + "}";

    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class))
           .thenReturn(json);
    RestResult result = new RestResult(this.mockResponse);
    Assert.assertEquals(200, result.status());
    Assert.assertNull(result.headers());

    IndexLabel indexLabel = result.readObject(IndexLabel.class);

    Assert.assertEquals("softwareByPrice", indexLabel.name());
    Assert.assertEquals(HugeType.VERTEX_LABEL, indexLabel.baseType());
    Assert.assertEquals("software", indexLabel.baseValue());
    Assert.assertEquals(IndexType.SEARCH, indexLabel.indexType());
    Assert.assertEquals(ImmutableList.of("price"),
                        indexLabel.indexFields());
}
 
Example 6
Source File: FlatMapperIteratorTest.java    From hugegraph-common with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapperReturnNullThenNext() {
    Iterator<String> keys = ImmutableList.of("fifth").iterator();

    Iterator<Integer> results = new FlatMapperIterator<>(keys, key -> {
        Assert.assertNull(DATA.get(key));
        return null;
    });
    Assert.assertThrows(NoSuchElementException.class, () -> {
        results.next();
    });
    Assert.assertThrows(NoSuchElementException.class, () -> {
        results.next();
    });
}
 
Example 7
Source File: UsersTest.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateUser() {
    HugeGraph graph = graph();
    UserManager userManager = graph.userManager();

    Id id = userManager.createUser(makeUser("tom", "pass1"));

    HugeUser user = userManager.getUser(id);
    Assert.assertEquals("tom", user.name());
    Assert.assertEquals("pass1", user.password());
    Assert.assertEquals(user.create(), user.update());
    Assert.assertNull(user.phone());
    Assert.assertNull(user.email());
    Assert.assertNull(user.avatar());

    Map<String, Object> expected = new HashMap<>();
    expected.putAll(ImmutableMap.of("user_name", "tom",
                                    "user_password", "pass1",
                                    "user_creator", "admin"));
    expected.putAll(ImmutableMap.of("user_create", user.create(),
                                    "user_update", user.update(),
                                    "id", user.id()));

    Assert.assertEquals(expected, user.asMap());

    Assert.assertThrows(IllegalArgumentException.class, () -> {
        userManager.createUser(makeUser("tom", "pass1"));
    }, e -> {
        Assert.assertContains("Can't save user", e.getMessage());
        Assert.assertContains("that already exists", e.getMessage());
    });
}
 
Example 8
Source File: RestResultTest.java    From hugegraph-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadEdge() {
    String json = "{"
            + "\"id\": \"person:peter>created>>software:lop\","
            + "\"label\": \"created\","
            + "\"type\": \"edge\","
            + "\"outV\": \"person:peter\","
            + "\"inV\": \"software:lop\","
            + "\"outVLabel\": \"person\","
            + "\"inVLabel\": \"software\","
            + "\"properties\": {"
            + "\"city\": \"Hongkong\","
            + "\"date\": 1495036800000"
            + "}"
            + "}";

    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class))
           .thenReturn(json);
    RestResult result = new RestResult(this.mockResponse);
    Assert.assertEquals(200, result.status());
    Assert.assertNull(result.headers());

    Edge edge = result.readObject(Edge.class);

    Assert.assertEquals("person:peter>created>>software:lop", edge.id());
    Assert.assertEquals("created", edge.label());
    Assert.assertEquals("person:peter", edge.sourceId());
    Assert.assertEquals("software:lop", edge.targetId());
    Assert.assertEquals("person", edge.sourceLabel());
    Assert.assertEquals("software", edge.targetLabel());
    Assert.assertEquals(ImmutableMap.of("city", "Hongkong",
                                        "date", 1495036800000L),
                        edge.properties());
}
 
Example 9
Source File: CacheTest.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpireWithAddNewItem() {
    Cache<Id, Object> cache = newCache();
    cache.expire(2000L); // 2s

    cache.update(IdGenerator.of("1"), "value-1");
    cache.update(IdGenerator.of("2"), "value-2");

    Assert.assertEquals(2, cache.size());

    waitTillNext(1);
    cache.tick();

    cache.update(IdGenerator.of("3"), "value-3");
    cache.tick();

    Assert.assertEquals(3, cache.size());

    waitTillNext(1);
    cache.tick();

    Assert.assertNull(cache.get(IdGenerator.of("1")));
    Assert.assertNull(cache.get(IdGenerator.of("2")));
    Assert.assertNotNull(cache.get(IdGenerator.of("3")));
    Assert.assertEquals(1, cache.size());

    waitTillNext(1);
    cache.tick();

    // NOTE: OffheapCache should expire item by access
    Assert.assertNull(cache.get(IdGenerator.of("3")));
    Assert.assertEquals(0, cache.size());
}
 
Example 10
Source File: CacheTest.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetOrFetch() {
    Cache<Id, Object> cache = newCache();
    Id id = IdGenerator.of("1");
    Assert.assertNull(cache.get(id));

    Assert.assertEquals("value-1",  cache.getOrFetch(id, key -> {
        return "value-1";
    }));

    cache.update(id, "value-2");
    Assert.assertEquals("value-2",  cache.getOrFetch(id, key -> {
        return "value-1";
    }));
}
 
Example 11
Source File: TableBackendEntryTest.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Test
public void testType() {
    Id id = IdGenerator.of(1L);
    TableBackendEntry entry = new TableBackendEntry(id);
    Assert.assertNull(entry.type());
    Assert.assertEquals(id, entry.id());

    entry.type(HugeType.VERTEX);
    Assert.assertEquals(HugeType.VERTEX, entry.type());
}
 
Example 12
Source File: RestResultTest.java    From hugegraph-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadEdgeLabel() {
    String json = "{"
            + "\"id\": 2,"
            + "\"source_label\": \"person\","
            + "\"index_labels\": [\"createdByDate\"],"
            + "\"name\": \"created\","
            + "\"target_label\": \"software\","
            + "\"sort_keys\": [],"
            + "\"properties\": [\"date\"],"
            + "\"frequency\": \"SINGLE\""
            + "}";

    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class))
           .thenReturn(json);
    RestResult result = new RestResult(this.mockResponse);
    Assert.assertEquals(200, result.status());
    Assert.assertNull(result.headers());

    EdgeLabel edgeLabel = result.readObject(EdgeLabel.class);

    Assert.assertEquals("created", edgeLabel.name());
    Assert.assertEquals("person", edgeLabel.sourceLabel());
    Assert.assertEquals("software", edgeLabel.targetLabel());
    Assert.assertEquals(Frequency.SINGLE, edgeLabel.frequency());
    Assert.assertEquals(Collections.emptyList(), edgeLabel.sortKeys());
    Assert.assertEquals(ImmutableSet.of("date"), edgeLabel.properties());
}
 
Example 13
Source File: DataTypeTest.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Test
public void testValueToDate() {
    Date date = Utils.date("2019-01-01 12:00:00");
    Assert.assertEquals(date, DataType.DATE.valueToDate(date));
    Assert.assertEquals(date,
                        DataType.DATE.valueToDate("2019-01-01 12:00:00"));
    Assert.assertEquals(date, DataType.DATE.valueToDate(date.getTime()));

    Assert.assertNull(DataType.TEXT.valueToDate("2019-01-01 12:00:00"));
    Assert.assertNull(DataType.DATE.valueToDate(true));
}
 
Example 14
Source File: CommonTraverserApiTest.java    From hugegraph-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testScanEdge() {
    List<Shard> shards = edgesAPI.shards(1 * 1024 * 1024);
    List<Edge> edges = new LinkedList<>();
    for (Shard shard : shards) {
        Edges results = edgesAPI.scan(shard, null, 0L);
        Assert.assertNull(results.page());
        edges.addAll(ImmutableList.copyOf(results.results()));
    }
    Assert.assertEquals(6, edges.size());
}
 
Example 15
Source File: CommonTraverserApiTest.java    From hugegraph-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testScanVertex() {
    List<Shard> shards = verticesAPI.shards(1 * 1024 * 1024);
    List<Vertex> vertices = new LinkedList<>();
    for (Shard shard : shards) {
        Vertices results = verticesAPI.scan(shard, null, 0L);
        vertices.addAll(ImmutableList.copyOf(results.results()));
        Assert.assertNull(results.page());
    }
    Assert.assertEquals(6, vertices.size());
}
 
Example 16
Source File: RestResultTest.java    From hugegraph-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testReadEdgeLabels() {
    String json = "{\"edgelabels\": ["
            + "{"
            + "\"id\": 2,"
            + "\"source_label\": \"person\","
            + "\"index_labels\": [\"createdByDate\"],"
            + "\"name\": \"created\","
            + "\"target_label\": \"software\","
            + "\"sort_keys\": [],"
            + "\"properties\": [\"date\"],"
            + "\"frequency\": \"SINGLE\""
            + "},"
            + "{\"id\": 3,"
            + "\"source_label\": \"person\","
            + "\"index_labels\": [],"
            + "\"name\": \"knows\","
            + "\"target_label\": \"person\","
            + "\"sort_keys\": [],"
            + "\"properties\": [\"date\", \"city\"],"
            + "\"frequency\": \"SINGLE\""
            + "}"
            + "]}";

    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class))
           .thenReturn(json);
    RestResult result = new RestResult(this.mockResponse);
    Assert.assertEquals(200, result.status());
    Assert.assertNull(result.headers());

    List<EdgeLabel> edgeLabels = result.readList("edgelabels",
                                                 EdgeLabel.class);
    Assert.assertEquals(2, edgeLabels.size());
    EdgeLabel edgeLabel1 = edgeLabels.get(0);
    EdgeLabel edgeLabel2 = edgeLabels.get(1);

    Assert.assertEquals("created", edgeLabel1.name());
    Assert.assertEquals("person", edgeLabel1.sourceLabel());
    Assert.assertEquals("software", edgeLabel1.targetLabel());
    Assert.assertEquals(Frequency.SINGLE, edgeLabel1.frequency());
    Assert.assertEquals(Collections.emptyList(), edgeLabel1.sortKeys());
    Assert.assertEquals(ImmutableSet.of("date"), edgeLabel1.properties());

    Assert.assertEquals("knows", edgeLabel2.name());
    Assert.assertEquals("person", edgeLabel2.sourceLabel());
    Assert.assertEquals("person", edgeLabel2.targetLabel());
    Assert.assertEquals(Frequency.SINGLE, edgeLabel2.frequency());
    Assert.assertEquals(Collections.emptyList(), edgeLabel2.sortKeys());
    Assert.assertEquals(ImmutableSet.of("date", "city"),
                        edgeLabel2.properties());
}
 
Example 17
Source File: RestResultTest.java    From hugegraph-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testReadVertexLabels() {
    String json = "{\"vertexlabels\": ["
            + "{"
            + "\"id\": 1,"
            + "\"primary_keys\": [\"name\"],"
            + "\"index_labels\": [],"
            + "\"name\": \"software\","
            + "\"id_strategy\": \"PRIMARY_KEY\","
            + "\"properties\": [\"price\", \"name\", \"lang\"]"
            + "},"
            + "{"
            + "\"id\": 2,"
            + "\"primary_keys\": [],"
            + "\"index_labels\": [],"
            + "\"name\": \"person\","
            + "\"id_strategy\": \"CUSTOMIZE_STRING\","
            + "\"properties\": [\"city\", \"name\", \"age\"]"
            + "}"
            + "]}";

    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class))
           .thenReturn(json);
    RestResult result = new RestResult(this.mockResponse);
    Assert.assertEquals(200, result.status());
    Assert.assertNull(result.headers());

    List<VertexLabel> vertexLabels = result.readList("vertexlabels",
                                                     VertexLabel.class);
    Assert.assertEquals(2, vertexLabels.size());
    VertexLabel vertexLabel1 = vertexLabels.get(0);
    VertexLabel vertexLabel2 = vertexLabels.get(1);

    Assert.assertEquals("software", vertexLabel1.name());
    Assert.assertEquals(IdStrategy.PRIMARY_KEY, vertexLabel1.idStrategy());
    Assert.assertEquals(ImmutableList.of("name"),
                        vertexLabel1.primaryKeys());
    Assert.assertEquals(ImmutableSet.of("price", "name", "lang"),
                        vertexLabel1.properties());

    Assert.assertEquals("person", vertexLabel2.name());
    Assert.assertEquals(IdStrategy.CUSTOMIZE_STRING, vertexLabel2.idStrategy());
    Assert.assertEquals(Collections.emptyList(),
                        vertexLabel2.primaryKeys());
    Assert.assertEquals(ImmutableSet.of("city", "name", "age"),
                        vertexLabel2.properties());
}
 
Example 18
Source File: RestResultTest.java    From hugegraph-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testReadEdges() {
    String json = "{\"edges\": ["
            + "{"
            + "\"id\": \"person:peter>created>>software:lop\","
            + "\"label\": \"created\","
            + "\"type\": \"edge\","
            + "\"inVLabel\": \"software\","
            + "\"outVLabel\": \"person\","
            + "\"inV\": \"software:lop\","
            + "\"outV\": \"person:peter\","
            + "\"properties\": {"
            + "\"date\": 1495036800000,"
            + "\"city\": \"Hongkong\""
            + "}"
            + "},"
            + "{"
            + "\"id\": \"person:peter>knows>>person:marko\","
            + "\"label\": \"knows\","
            + "\"type\": \"edge\","
            + "\"inVLabel\": \"person\","
            + "\"outVLabel\": \"person\","
            + "\"inV\": \"person:marko\","
            + "\"outV\": \"person:peter\","
            + "\"properties\": {"
            + "\"date\": 1476720000000"
            + "}"
            + "}"
            + "]}";

    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class))
           .thenReturn(json);
    RestResult result = new RestResult(this.mockResponse);
    Assert.assertEquals(200, result.status());
    Assert.assertNull(result.headers());

    List<Edge> edges = result.readList("edges", Edge.class);
    Assert.assertEquals(2, edges.size());
    Edge edge1 = edges.get(0);
    Edge edge2 = edges.get(1);

    Assert.assertEquals("person:peter>created>>software:lop", edge1.id());
    Assert.assertEquals("created", edge1.label());
    Assert.assertEquals("person:peter", edge1.sourceId());
    Assert.assertEquals("software:lop", edge1.targetId());
    Assert.assertEquals("person", edge1.sourceLabel());
    Assert.assertEquals("software", edge1.targetLabel());
    Assert.assertEquals(ImmutableMap.of("city", "Hongkong",
                                        "date", 1495036800000L),
                        edge1.properties());

    Assert.assertEquals("person:peter>knows>>person:marko", edge2.id());
    Assert.assertEquals("knows", edge2.label());
    Assert.assertEquals("person:peter", edge2.sourceId());
    Assert.assertEquals("person:marko", edge2.targetId());
    Assert.assertEquals("person", edge2.sourceLabel());
    Assert.assertEquals("person", edge2.targetLabel());
    Assert.assertEquals(ImmutableMap.of("date", 1476720000000L),
                        edge2.properties());
}
 
Example 19
Source File: VertexLabelApiTest.java    From hugegraph-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateWithTtl() {
    VertexLabel vertexLabel = schema().vertexLabel("person1")
                                      .useAutomaticId()
                                      .properties("name", "age", "date")
                                      .build();
    vertexLabel = vertexLabelAPI.create(vertexLabel);

    Assert.assertEquals("person1", vertexLabel.name());
    Assert.assertEquals(IdStrategy.AUTOMATIC, vertexLabel.idStrategy());
    Assert.assertEquals(true, vertexLabel.enableLabelIndex());
    Set<String> props = ImmutableSet.of("name", "age", "date");
    Assert.assertEquals(props, vertexLabel.properties());
    Assert.assertEquals(0L, vertexLabel.ttl());
    Assert.assertNull(vertexLabel.ttlStartTime());

    vertexLabel = schema().vertexLabel("person2")
                          .useAutomaticId()
                          .properties("name", "age", "date")
                          .ttl(3000L)
                          .build();
    vertexLabel = vertexLabelAPI.create(vertexLabel);

    Assert.assertEquals("person2", vertexLabel.name());
    Assert.assertEquals(IdStrategy.AUTOMATIC, vertexLabel.idStrategy());
    Assert.assertEquals(true, vertexLabel.enableLabelIndex());
    Assert.assertEquals(props, vertexLabel.properties());
    Assert.assertEquals(3000L, vertexLabel.ttl());
    Assert.assertNull(vertexLabel.ttlStartTime());

    vertexLabel = schema().vertexLabel("person3")
                          .useAutomaticId()
                          .properties("name", "age", "date")
                          .ttl(3000L)
                          .ttlStartTime("date")
                          .build();
    vertexLabel = vertexLabelAPI.create(vertexLabel);

    Assert.assertEquals("person3", vertexLabel.name());
    Assert.assertEquals(IdStrategy.AUTOMATIC, vertexLabel.idStrategy());
    Assert.assertEquals(true, vertexLabel.enableLabelIndex());
    Assert.assertEquals(props, vertexLabel.properties());
    Assert.assertEquals(3000L, vertexLabel.ttl());
    Assert.assertEquals("date", vertexLabel.ttlStartTime());
}
 
Example 20
Source File: SchemaCoreTest.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
protected void assertNotContainsPk(Collection<Id> ids, String... keys) {
    for (String key : keys) {
        Assert.assertNull(graph().existsPropertyKey(key));
    }
}