Skip to content

Commit

Permalink
NIFI-5924 Labels should be searchable
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Burgess <mattyb149@apache.org>

This closes apache#4070
  • Loading branch information
MatthewKnight-NG authored and driesva committed Mar 19, 2021
1 parent 7270f77 commit 6c95002
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class SearchResultsDTO {
private List<ComponentSearchResultDTO> outputPortResults = new ArrayList<>();
private List<ComponentSearchResultDTO> remoteProcessGroupResults = new ArrayList<>();
private List<ComponentSearchResultDTO> funnelResults = new ArrayList<>();
private List<ComponentSearchResultDTO> labelResults = new ArrayList<>();
private List<ComponentSearchResultDTO> parameterContextResults = new ArrayList<>();
private List<ComponentSearchResultDTO> parameterResults = new ArrayList<>();

Expand Down Expand Up @@ -136,6 +137,20 @@ public void setFunnelResults(List<ComponentSearchResultDTO> funnelResults) {
this.funnelResults = funnelResults;
}

/**
* @return labels that matched the search
*/
@ApiModelProperty(
value = "The labels that matched the search."
)
public List<ComponentSearchResultDTO> getLabelResults() {
return labelResults;
}

public void setLabelResults(List<ComponentSearchResultDTO> labelResults) {
this.labelResults = labelResults;
}

/**
* @return parameter contexts that matched the search.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.nifi.controller.FlowController;
import org.apache.nifi.controller.ProcessorNode;
import org.apache.nifi.controller.ScheduledState;
import org.apache.nifi.controller.label.Label;
import org.apache.nifi.controller.queue.FlowFileQueue;
import org.apache.nifi.flowfile.FlowFilePrioritizer;
import org.apache.nifi.groups.ProcessGroup;
Expand Down Expand Up @@ -74,8 +75,8 @@ public class ControllerSearchService {
* Searches term in the controller beginning from a given process group.
*
* @param results Search results
* @param search The search term
* @param group The init process group
* @param search The search term
* @param group The init process group
*/
public void search(final SearchResultsDTO results, final String search, final ProcessGroup group) {
final NiFiUser user = NiFiUserUtils.getNiFiUser();
Expand Down Expand Up @@ -162,6 +163,18 @@ public void search(final SearchResultsDTO results, final String search, final Pr
}
}

for (final Label label : group.getLabels()) {
if (label.isAuthorized(authorizer, RequestAction.READ, user)) {
final ComponentSearchResultDTO match = search(search, label);
if (match != null) {
match.setGroupId(group.getIdentifier());
match.setParentGroup(buildResultGroup(group, user));
match.setVersionedGroup(buildVersionedGroup(group, user));
results.getLabelResults().add(match);
}
}
}

for (final ProcessGroup processGroup : group.getProcessGroups()) {
search(results, search, processGroup);
}
Expand Down Expand Up @@ -511,6 +524,22 @@ private ComponentSearchResultDTO search(final String searchStr, final Funnel fun
return dto;
}

private ComponentSearchResultDTO search(final String searchStr, final Label label) {
final List<String> matches = new ArrayList<>();
addIfAppropriate(searchStr, label.getIdentifier(), "Id", matches);
addIfAppropriate(searchStr, label.getValue(), "Value", matches);

if (matches.isEmpty()) {
return null;
}

final ComponentSearchResultDTO dto = new ComponentSearchResultDTO();
dto.setId(label.getIdentifier());
dto.setName(label.getValue());
dto.setMatches(matches);
return dto;
}

private ComponentSearchResultDTO search(final String searchString, final ParameterContext parameterContext) {
final List<String> matches = new ArrayList<>();
addIfAppropriate(searchString, parameterContext.getIdentifier(), "Id", matches);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.nifi.controller.ProcessorNode;
import org.apache.nifi.controller.StandardProcessorNode;
import org.apache.nifi.controller.flow.FlowManager;
import org.apache.nifi.controller.label.Label;
import org.apache.nifi.groups.ProcessGroup;
import org.apache.nifi.parameter.Parameter;
import org.apache.nifi.parameter.ParameterContext;
Expand Down Expand Up @@ -403,6 +404,49 @@ public void testSearchParameterContextNotAuthorized() {
assertEquals(0, searchResultsDTO.getParameterResults().size());
}

@Test
public void testSearchLabels() {
// root level PG
final ProcessGroup rootProcessGroup = setupMockedProcessGroup("root", null, true, variableRegistry, null);

// setup labels
setupMockedLabels(rootProcessGroup);

// perform search for foo
service.search(searchResultsDTO, "FOO", rootProcessGroup);

assertTrue(searchResultsDTO.getLabelResults().size() == 1);
assertTrue(searchResultsDTO.getLabelResults().get(0).getId().equals("foo"));
assertTrue(searchResultsDTO.getLabelResults().get(0).getName().equals("Value for label foo"));
}

/**
* Mocks Labels including isAuthorized() and their identifier and value
*
* @param containingProcessGroup The process group
*/
private static void setupMockedLabels(final ProcessGroup containingProcessGroup) {
final Label label1 = mock(Label.class);
Mockito.doReturn(true).when(label1).isAuthorized(AdditionalMatchers.or(any(Authorizer.class), isNull()), eq(RequestAction.READ),
AdditionalMatchers.or(any(NiFiUser.class), isNull()));
Mockito.doReturn("foo").when(label1).getIdentifier();
Mockito.doReturn("Value for label foo").when(label1).getValue();

final Label label2 = mock(Label.class);
Mockito.doReturn(false).when(label2).isAuthorized(AdditionalMatchers.or(any(Authorizer.class), isNull()), eq(RequestAction.READ),
AdditionalMatchers.or(any(NiFiUser.class), isNull()));
Mockito.doReturn("bar").when(label2).getIdentifier();
Mockito.doReturn("Value for label bar, but FOO is in here too").when(label2).getValue();

// assign labels to the PG
Mockito.doReturn(new HashSet<Label>() {
{
add(label1);
add(label2);
}
}).when(containingProcessGroup).getLabels();
}

/**
* Sets up a mock Parameter Context including isAuthorized()
* @param name name of the parameter context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@
});
}

// show all labels
if (!nfCommon.isEmpty(searchResults.labelResults)) {
ul.append('<li class="search-header"><div class="search-result-icon icon icon-label"></div>Labels</li>');
$.each(searchResults.labelResults, function (i, labelMatch) {
nfSearchAutocomplete._renderItem(ul, $.extend({}, labelMatch, { type: 'label' }));
});
}

// show all parameter contexts and parameters
if (!nfCommon.isEmpty(searchResults.parameterContextResults)) {
ul.append('<li class="search-header"><div class="search-result-icon icon"></div>Parameter Contexts</li>');
Expand Down

0 comments on commit 6c95002

Please sign in to comment.