use of au.gov.asd.tac.constellation.graph.Attribute in project constellation by constellation-app.
the class TableViewStateIoProvider method writeObject.
@Override
public void writeObject(final Attribute attribute, final int elementId, final JsonGenerator jsonGenerator, final GraphReadMethods graph, final GraphByteWriter byteWriter, final boolean verbose) throws IOException {
if (verbose || !graph.isDefaultValue(attribute.getId(), elementId)) {
final TableViewState state = (TableViewState) graph.getObjectValue(attribute.getId(), elementId);
if (state == null) {
jsonGenerator.writeNullField(attribute.getName());
} else {
jsonGenerator.writeObjectFieldStart(attribute.getName());
jsonGenerator.writeBooleanField("selectedOnly", state.isSelectedOnly());
jsonGenerator.writeStringField("elementType", state.getElementType().name());
if (state.getTransactionColumnAttributes() == null) {
jsonGenerator.writeNullField(TRANSACTION_COLUMN_ATTRIBUTES);
} else {
jsonGenerator.writeArrayFieldStart(TRANSACTION_COLUMN_ATTRIBUTES);
for (final Tuple<String, Attribute> columnAttribute : state.getTransactionColumnAttributes()) {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(ATTRIBUTE_PREFIX, columnAttribute.getFirst());
jsonGenerator.writeStringField(ATTRIBUTE_ELEMENT_TYPE, columnAttribute.getSecond().getElementType().name());
jsonGenerator.writeStringField(ATTRIBUTE_NAME, columnAttribute.getSecond().getName());
jsonGenerator.writeEndObject();
}
jsonGenerator.writeEndArray();
}
if (state.getVertexColumnAttributes() == null) {
jsonGenerator.writeNullField(VERTEX_COLUMN_ATTRIBUTES);
} else {
jsonGenerator.writeArrayFieldStart(VERTEX_COLUMN_ATTRIBUTES);
for (final Tuple<String, Attribute> columnAttribute : state.getVertexColumnAttributes()) {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(ATTRIBUTE_PREFIX, columnAttribute.getFirst());
jsonGenerator.writeStringField(ATTRIBUTE_ELEMENT_TYPE, columnAttribute.getSecond().getElementType().name());
jsonGenerator.writeStringField(ATTRIBUTE_NAME, columnAttribute.getSecond().getName());
jsonGenerator.writeEndObject();
}
jsonGenerator.writeEndArray();
}
jsonGenerator.writeEndObject();
}
}
}
use of au.gov.asd.tac.constellation.graph.Attribute in project constellation by constellation-app.
the class TableNGTest method getRowDataForVertex.
@Test
public void getRowDataForVertex() {
final ReadableGraph readableGraph = mock(ReadableGraph.class);
final Map<Integer, ObservableList<String>> elementIdToRowIndex = new HashMap<>();
final Map<ObservableList<String>, Integer> rowToElementIdIndex = new HashMap<>();
doReturn(elementIdToRowIndex).when(activeTableReference).getElementIdToRowIndex();
doReturn(rowToElementIdIndex).when(activeTableReference).getRowToElementIdIndex();
final int vertexId = 42;
// Set up the attributes for each column
when(readableGraph.getAttribute(GraphElementType.VERTEX, "COLUMN_A")).thenReturn(101);
when(readableGraph.getAttributeName(101)).thenReturn("COLUMN_A");
when(readableGraph.getAttributeElementType(101)).thenReturn(GraphElementType.VERTEX);
when(readableGraph.getAttributeType(101)).thenReturn("string");
final Object objectValue1 = new Object();
when(readableGraph.getObjectValue(101, vertexId)).thenReturn(objectValue1);
final Attribute attribute1 = new GraphAttribute(readableGraph, 101);
final CopyOnWriteArrayList<Column> columnIndex = new CopyOnWriteArrayList<>();
columnIndex.add(new Column(null, attribute1, null));
when(table.getColumnIndex()).thenReturn(columnIndex);
try (final MockedStatic<AbstractAttributeInteraction> attrInteractionMockedStatic = Mockito.mockStatic(AbstractAttributeInteraction.class)) {
final AbstractAttributeInteraction interaction = mock(AbstractAttributeInteraction.class);
attrInteractionMockedStatic.when(() -> AbstractAttributeInteraction.getInteraction("string")).thenReturn(interaction);
when(interaction.getDisplayText(objectValue1)).thenReturn("column1Value");
assertEquals(FXCollections.observableArrayList("column1Value"), table.getRowDataForVertex(readableGraph, vertexId));
assertEquals(Map.of(vertexId, FXCollections.observableArrayList("column1Value")), elementIdToRowIndex);
assertEquals(Map.of(FXCollections.observableArrayList("column1Value"), vertexId), rowToElementIdIndex);
}
}
use of au.gov.asd.tac.constellation.graph.Attribute in project constellation by constellation-app.
the class TableNGTest method getRowDataForTransaction.
@Test
public void getRowDataForTransaction() {
final ReadableGraph readableGraph = mock(ReadableGraph.class);
final Map<Integer, ObservableList<String>> elementIdToRowIndex = new HashMap<>();
final Map<ObservableList<String>, Integer> rowToElementIdIndex = new HashMap<>();
doReturn(elementIdToRowIndex).when(activeTableReference).getElementIdToRowIndex();
doReturn(rowToElementIdIndex).when(activeTableReference).getRowToElementIdIndex();
final int transactionId = 42;
final int sourceVertexId = 52;
final int destinationVertexId = 62;
// Set up the attributes for each column
when(readableGraph.getAttribute(GraphElementType.VERTEX, "COLUMN_1")).thenReturn(101);
when(readableGraph.getAttributeName(101)).thenReturn("COLUMN_1");
when(readableGraph.getAttributeElementType(101)).thenReturn(GraphElementType.VERTEX);
when(readableGraph.getAttributeType(101)).thenReturn("string");
final Attribute attribute1 = new GraphAttribute(readableGraph, 101);
when(readableGraph.getAttribute(GraphElementType.TRANSACTION, "COLUMN_2")).thenReturn(102);
when(readableGraph.getAttributeName(102)).thenReturn("COLUMN_2");
when(readableGraph.getAttributeElementType(102)).thenReturn(GraphElementType.TRANSACTION);
when(readableGraph.getAttributeType(102)).thenReturn("string");
final Attribute attribute2 = new GraphAttribute(readableGraph, 102);
// There are 2 columns. One called COLUMN_1 and the other COLUMN_2. COLUMN_1
// is present on source and destination verticies. COLUMN_2 is present on transactions
final CopyOnWriteArrayList<Column> columnIndex = new CopyOnWriteArrayList<>();
columnIndex.add(new Column("source.", attribute1, null));
columnIndex.add(new Column("destination.", attribute1, null));
columnIndex.add(new Column("transaction.", attribute2, null));
when(table.getColumnIndex()).thenReturn(columnIndex);
// When looking at a source vertex column, it gets the source vertex of
// the transaction and extracts the value for the column from that vertex
final Object sourceVertexCoulmnValue = new Object();
when(readableGraph.getTransactionSourceVertex(transactionId)).thenReturn(sourceVertexId);
when(readableGraph.getObjectValue(101, sourceVertexId)).thenReturn(sourceVertexCoulmnValue);
// When looking at a destination vertex column, it gets the destination vertex of
// the transaction and extracts the value for the column from that vertex
final Object destinationVertexCoulmnValue = new Object();
when(readableGraph.getTransactionDestinationVertex(transactionId)).thenReturn(destinationVertexId);
when(readableGraph.getObjectValue(101, destinationVertexId)).thenReturn(destinationVertexCoulmnValue);
// When looking at a transaction column, it extracts the value from the passed transaction
final Object transactionCoulmnValue = new Object();
when(readableGraph.getObjectValue(102, transactionId)).thenReturn(transactionCoulmnValue);
try (final MockedStatic<AbstractAttributeInteraction> attrInteractionMockedStatic = Mockito.mockStatic(AbstractAttributeInteraction.class)) {
final AbstractAttributeInteraction interaction = mock(AbstractAttributeInteraction.class);
attrInteractionMockedStatic.when(() -> AbstractAttributeInteraction.getInteraction("string")).thenReturn(interaction);
when(interaction.getDisplayText(sourceVertexCoulmnValue)).thenReturn("sourceVertex_COLUMN_1_Value");
when(interaction.getDisplayText(destinationVertexCoulmnValue)).thenReturn("destinationVertext_COLUMN_1_Value");
when(interaction.getDisplayText(transactionCoulmnValue)).thenReturn("transaction_COLUMN_2_Value");
assertEquals(FXCollections.observableArrayList("sourceVertex_COLUMN_1_Value", "destinationVertext_COLUMN_1_Value", "transaction_COLUMN_2_Value"), table.getRowDataForTransaction(readableGraph, transactionId));
assertEquals(Map.of(transactionId, FXCollections.observableArrayList("sourceVertex_COLUMN_1_Value", "destinationVertext_COLUMN_1_Value", "transaction_COLUMN_2_Value")), elementIdToRowIndex);
assertEquals(Map.of(FXCollections.observableArrayList("sourceVertex_COLUMN_1_Value", "destinationVertext_COLUMN_1_Value", "transaction_COLUMN_2_Value"), transactionId), rowToElementIdIndex);
}
}
use of au.gov.asd.tac.constellation.graph.Attribute in project constellation by constellation-app.
the class TableNGTest method updateColumns.
@Test
public void updateColumns() {
final ChangeListener<ObservableList<String>> tableSelectionListener = mock(ChangeListener.class);
final ListChangeListener selectedOnlySelectionListener = mock(ListChangeListener.class);
doReturn(tableSelectionListener).when(table).getTableSelectionListener();
doReturn(selectedOnlySelectionListener).when(table).getSelectedOnlySelectionListener();
final ReadableGraph readableGraph = mock(ReadableGraph.class);
when(graph.getReadableGraph()).thenReturn(readableGraph);
// Set up the initial column index. Column 4 will not be found in the graph and
// dropped in the column index created by the update call
final String columnType1 = "source.";
final Attribute attribute1 = mock(Attribute.class);
final TableColumn<ObservableList<String>, String> column1 = mock(TableColumn.class);
when(column1.getText()).thenReturn("source.COLUMN_A");
final String columnType2 = "destination.";
final Attribute attribute2 = mock(Attribute.class);
final TableColumn<ObservableList<String>, String> column2 = mock(TableColumn.class);
when(column2.getText()).thenReturn("destination.COLUMN_A");
final String columnType3 = "transaction.";
final Attribute attribute3 = mock(Attribute.class);
final TableColumn<ObservableList<String>, String> column3 = mock(TableColumn.class);
when(column3.getText()).thenReturn("transaction.COLUMN_B");
final String columnType4 = "source.";
final Attribute attribute4 = mock(Attribute.class);
final TableColumn<ObservableList<String>, String> column4 = mock(TableColumn.class);
when(column4.getText()).thenReturn("source.COLUMN_C");
final CopyOnWriteArrayList<Column> columnIndex = new CopyOnWriteArrayList<>();
columnIndex.add(new Column(columnType1, attribute1, column1));
columnIndex.add(new Column(columnType2, attribute2, column2));
columnIndex.add(new Column(columnType3, attribute3, column3));
columnIndex.add(new Column(columnType4, attribute4, column4));
when(activeTableReference.getColumnIndex()).thenReturn(columnIndex);
// This is a reference of the old column index that will be used whilst the new
// index is being created. Because that creation is mocked this is used only as a
// vertification that the parameter is being correctly constructed.
final Map<String, TableColumn<ObservableList<String>, String>> columnReferenceMap = Map.of("source.COLUMN_A", column1, "destination.COLUMN_A", column2, "transaction.COLUMN_B", column3, "source.COLUMN_C", column4);
// Mock out the re-population of the column index from the graph. This excludes column 4.
final CopyOnWriteArrayList<Column> sourceColumnIndex = new CopyOnWriteArrayList<>();
sourceColumnIndex.add(new Column(columnType1, attribute1, column1));
final CopyOnWriteArrayList<Column> destinationColumnIndex = new CopyOnWriteArrayList<>();
destinationColumnIndex.add(new Column(columnType2, attribute2, column2));
final CopyOnWriteArrayList<Column> transactionColumnIndex = new CopyOnWriteArrayList<>();
transactionColumnIndex.add(new Column(columnType3, attribute3, column3));
doReturn(sourceColumnIndex).when(table).createColumnIndexPart(readableGraph, GraphElementType.VERTEX, "source.", columnReferenceMap);
doReturn(destinationColumnIndex).when(table).createColumnIndexPart(readableGraph, GraphElementType.VERTEX, "destination.", columnReferenceMap);
doReturn(transactionColumnIndex).when(table).createColumnIndexPart(readableGraph, GraphElementType.TRANSACTION, "transaction.", columnReferenceMap);
// Set up the table state
final TableViewState tableViewState = new TableViewState();
tableViewState.setElementType(GraphElementType.TRANSACTION);
// This is used by the sort comparator. This will order the columnIndex
// in a certain way that we can then verify below
tableViewState.setColumnAttributes(List.of(Tuple.create("source.", attribute1), Tuple.create("transaction.", attribute3), Tuple.create("destination.", attribute2)));
try (final MockedStatic<Platform> platformMockedStatic = Mockito.mockStatic(Platform.class)) {
platformMockedStatic.when(Platform::isFxApplicationThread).thenReturn(false);
platformMockedStatic.when(() -> Platform.runLater(any(Runnable.class))).then(mockInvocation -> {
assertTrue(mockInvocation.getArgument(0) instanceof UpdateColumnsTask);
return null;
});
table.updateColumns(graph, tableViewState);
}
// Verify the new column index
final CopyOnWriteArrayList<Column> expectedColumnIndex = new CopyOnWriteArrayList<>();
expectedColumnIndex.add(new Column(columnType1, attribute1, column1));
expectedColumnIndex.add(new Column(columnType3, attribute3, column3));
expectedColumnIndex.add(new Column(columnType2, attribute2, column2));
assertEquals(expectedColumnIndex, columnIndex);
verify(column1, times(1)).setCellValueFactory(any(Callback.class));
verify(column2, times(1)).setCellValueFactory(any(Callback.class));
verify(column3, times(1)).setCellValueFactory(any(Callback.class));
verify(column1, times(1)).setCellFactory(any(Callback.class));
verify(column2, times(1)).setCellFactory(any(Callback.class));
verify(column3, times(1)).setCellFactory(any(Callback.class));
}
use of au.gov.asd.tac.constellation.graph.Attribute in project constellation by constellation-app.
the class TableViewStateIoProviderNGTest method writeObject.
@Test
public void writeObject() throws IOException {
final GraphAttribute transactionGraphAttr = mock(GraphAttribute.class);
final GraphAttribute vertexGraphAttr = mock(GraphAttribute.class);
when(transactionGraphAttr.getElementType()).thenReturn(GraphElementType.TRANSACTION);
when(transactionGraphAttr.getName()).thenReturn("My Transaction");
when(vertexGraphAttr.getElementType()).thenReturn(GraphElementType.VERTEX);
when(vertexGraphAttr.getName()).thenReturn("My Vertex");
final TableViewState state = new TableViewState();
state.setSelectedOnly(true);
state.setElementType(GraphElementType.VERTEX);
state.setTransactionColumnAttributes(List.of(Tuple.create("transactionPrefix", transactionGraphAttr)));
state.setVertexColumnAttributes(List.of(Tuple.create("vertexPrefix", vertexGraphAttr)));
final Attribute attribute = mock(Attribute.class);
when(attribute.getId()).thenReturn(ATTRIBUTE_ID);
when(attribute.getName()).thenReturn("ATTR NAME");
final GraphWriteMethods graph = mock(GraphWriteMethods.class);
when(graph.isDefaultValue(ATTRIBUTE_ID, ELEMENT_ID)).thenReturn(false);
when(graph.getObjectValue(ATTRIBUTE_ID, ELEMENT_ID)).thenReturn(state);
final JsonFactory factory = new JsonFactory();
final ByteArrayOutputStream output = new ByteArrayOutputStream();
JsonGenerator jsonGenerator = factory.createGenerator(output);
// The code is written with the assumption that it is called within a document
// that has already started being written. Without starting the object in the test
// the code would throw invalid json exceptions.
jsonGenerator.writeStartObject();
tableViewStateIoProvider.writeObject(attribute, ELEMENT_ID, jsonGenerator, graph, null, false);
jsonGenerator.writeEndObject();
jsonGenerator.flush();
final ObjectMapper objectMapper = new ObjectMapper();
final JsonNode expected = objectMapper.readTree(new FileInputStream(getClass().getResource("resources/tableViewStateWrite.json").getPath()));
final JsonNode actual = objectMapper.readTree(new String(output.toByteArray(), StandardCharsets.UTF_8));
assertEquals(actual, expected);
}
Aggregations