Skip to content

Commit

Permalink
gh-1612 - ExtractWalkVertex and test added
Browse files Browse the repository at this point in the history
  • Loading branch information
m607123 committed Jan 9, 2018
1 parent 3117047 commit 73b045f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2017 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.gov.gchq.gaffer.data.graph.function.walk;

import com.google.common.collect.Iterables;

import uk.gov.gchq.gaffer.data.graph.Walk;
import uk.gov.gchq.koryphe.function.KorypheFunction;

public class ExtractWalkVertex extends KorypheFunction<Walk, Object> {
@Override
public Object apply(final Walk walk) {
if (null == walk) {
throw new IllegalArgumentException("Walk cannot be null");
}
return Iterables.get(walk.getEntities().get(0), 0).getVertex();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2017 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.gov.gchq.gaffer.data.graph.function.walk;

import org.junit.Test;

import uk.gov.gchq.gaffer.commonutil.TestGroups;
import uk.gov.gchq.gaffer.data.element.Edge;
import uk.gov.gchq.gaffer.data.element.Entity;
import uk.gov.gchq.gaffer.data.graph.Walk;

import java.util.function.Function;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class ExtractWalkVertexTest {
private static final Edge EDGE_AB = new Edge.Builder().group(TestGroups.EDGE).source("A").dest("B").directed(true).build();
private static final Edge EDGE_BC = new Edge.Builder().group(TestGroups.EDGE).source("B").dest("C").directed(true).build();
private static final Edge EDGE_CA = new Edge.Builder().group(TestGroups.EDGE).source("C").dest("A").directed(true).build();

private static final Entity ENTITY_A = new Entity.Builder().group(TestGroups.ENTITY).vertex("A").build();
private static final Entity ENTITY_B = new Entity.Builder().group(TestGroups.ENTITY).vertex("B").build();
private static final Entity ENTITY_C = new Entity.Builder().group(TestGroups.ENTITY).vertex("C").build();

@Test
public void shouldReturnVertexFromWalkObject() {
// Given
final Function<Walk, Object> function = new ExtractWalkVertex();
final Walk walk = new Walk.Builder()
.entity(ENTITY_A)
.edge(EDGE_AB)
.entity(ENTITY_B)
.edge(EDGE_BC)
.entity(ENTITY_C)
.edge(EDGE_CA)
.entity(ENTITY_A)
.build();

// When
final Object result = function.apply(walk);

// Then
assertEquals("A", result);
}

@Test
public void shouldThrowIAEForNullInput() {
// Given
final Function<Walk, Object> function = new ExtractWalkVertex();
final Walk walk = null;

// When / Then
try {
function.apply(walk);
} catch (final IllegalArgumentException e) {
assertTrue(e.getMessage().contains("Walk cannot be null"));
}
}
}

0 comments on commit 73b045f

Please sign in to comment.