Skip to content

Commit

Permalink
Enhance filters tests (kiegroup#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianonicolai authored Mar 13, 2018
1 parent 38dfc79 commit b325769
Show file tree
Hide file tree
Showing 20 changed files with 1,096 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,19 @@ public void init() {
});

saveFilterPopover = jQueryPopover.wrap(this.saveFilter);
saveFilterPopoverView.setCancelCallback(() -> closeSaveFilter());
saveFilterPopoverView.setSaveCallback(name -> saveFilter(name));
setSaveFilterPopoverCallback();
final PopoverOptions popoverOptions = new PopoverOptions();
popoverOptions.setContent(e -> saveFilterPopoverView.getElement());
saveFilterPopover.popover(popoverOptions);
saveFilterPopover.addShowListener(() -> saveFilterPopoverView.onOpen());
saveFilterPopover.addShownListener(() -> saveFilterPopoverView.onShow());
}

protected void setSaveFilterPopoverCallback() {
saveFilterPopoverView.setCancelCallback(() -> closeSaveFilter());
saveFilterPopoverView.setSaveCallback(name -> saveFilter(name));
}

@PreDestroy
public void destroy() {
saveFilterPopover.destroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.uberfire.workbench.model.menu.MenuFactory;
import org.uberfire.workbench.model.menu.Menus;

public abstract class SavedFiltersPresenter implements RestoreDefaultFiltersMenuBuilder.SupportsRestoreDefaultFilters {
public class SavedFiltersPresenter implements RestoreDefaultFiltersMenuBuilder.SupportsRestoreDefaultFilters {

private final Constants constants = Constants.INSTANCE;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* 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 org.jbpm.workbench.common.client.filters.active;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.uberfire.client.views.pfly.widgets.Popover;
import org.uberfire.mvp.Command;
import org.uberfire.mvp.ParameterizedCommand;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class ActiveFiltersViewImplTest {

@Mock
Popover saveFilterPopover;

@Mock
SaveFilterPopoverView saveFilterPopoverView;

@InjectMocks
@Spy
ActiveFiltersViewImpl view;

@Test
public void testSaveFilterCallback() {
final String name = "filterName";
doAnswer(invocation -> {
ParameterizedCommand<String> callback = (ParameterizedCommand<String>) invocation.getArguments()[0];
callback.execute(name);
return null;
}).when(saveFilterPopoverView).setSaveCallback(any());

view.setSaveFilterPopoverCallback();

verify(view).saveFilter(name);
}

@Test
public void testCancelFilterCallback() {
doAnswer(invocation -> {
Command callback = (Command) invocation.getArguments()[0];
callback.execute();
return null;
}).when(saveFilterPopoverView).setCancelCallback(any());

view.setSaveFilterPopoverCallback();

verify(saveFilterPopover).hide();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* 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 org.jbpm.workbench.common.client.filters.active;

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import elemental2.dom.HTMLInputElement;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.uberfire.mvp.Command;
import org.uberfire.mvp.ParameterizedCommand;

import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class SaveFilterPopoverViewTest {

@Mock
HTMLInputElement filterName;

@InjectMocks
SaveFilterPopoverView popoverView;

@Test
public void testOnSaveCallback() {
final String value = "value";
filterName.value = value;

final ParameterizedCommand<String> callback = mock(ParameterizedCommand.class);

popoverView.setSaveCallback(callback);

popoverView.onSave(null);

verify(callback).execute(value);
}

@Test
public void testOnKeyPressEvent() {
final String value = "value";
filterName.value = value;

final ParameterizedCommand<String> callback = mock(ParameterizedCommand.class);

popoverView.setSaveCallback(callback);

final KeyDownEvent event = mock(KeyDownEvent.class);
when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_ENTER,
KeyCodes.KEY_A);
popoverView.onKeyPressEvent(event);
popoverView.onKeyPressEvent(event);

verify(callback).execute(value);
}

@Test
public void testCancelCallback() {
final Command callback = mock(Command.class);

popoverView.setCancelCallback(callback);

popoverView.onCancel(null);

verify(callback).execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* 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 org.jbpm.workbench.common.client.filters.basic;

import java.util.function.Consumer;
import javax.enterprise.event.Event;

import org.jbpm.workbench.common.client.filters.saved.SavedFilterSelectedEvent;
import org.jbpm.workbench.df.client.filter.FilterEditorPopup;
import org.jbpm.workbench.df.client.filter.FilterSettings;
import org.jbpm.workbench.df.client.filter.FilterSettingsManager;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Spy;
import org.uberfire.mocks.EventSourceMock;
import org.uberfire.mvp.Command;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;

public abstract class AbstractBasicFiltersPresenterTest {

@Mock
BasicFiltersView view;

@Mock
FilterEditorPopup filterEditorPopup;

@Mock
FilterSettingsManager filterSettingsManager;

@Spy
Event<BasicFilterAddEvent> activeFilters = new EventSourceMock<>();

@Spy
Event<SavedFilterSelectedEvent> savedFilterSelectedEvent = new EventSourceMock<>();

public abstract BasicFiltersPresenter getPresenter();

public BasicFiltersView getView() {
return view;
}

@Before
public void init() {
doNothing().when(savedFilterSelectedEvent).fire(any(SavedFilterSelectedEvent.class));
doNothing().when(activeFilters).fire(any(BasicFilterAddEvent.class));
}

@Test
public void testSaveAdvancedFiltersCallback() {
doAnswer(invocation -> {
Command callback = (Command) invocation.getArguments()[0];
callback.execute();
return null;
}).when(view).setAdvancedFiltersCallback(any());

getPresenter().init();

verify(filterEditorPopup).setTitle(any());
ArgumentCaptor<Consumer> captor = ArgumentCaptor.forClass(Consumer.class);
verify(filterEditorPopup).show(any(),
captor.capture());
final FilterSettings filterSettings = new FilterSettings();
captor.getValue().accept(filterSettings);

verify(filterSettingsManager).saveFilterIntoPreferences(eq(filterSettings),
captor.capture());
captor.getValue().accept(true);

verify(filterEditorPopup).hide();
verify(savedFilterSelectedEvent).fire(any());
}

@Test
public void testSaveInvalidAdvancedFiltersCallback() {
doAnswer(invocation -> {
Command callback = (Command) invocation.getArguments()[0];
callback.execute();
return null;
}).when(view).setAdvancedFiltersCallback(any());

getPresenter().init();

verify(filterEditorPopup).setTitle(any());
ArgumentCaptor<Consumer> captor = ArgumentCaptor.forClass(Consumer.class);
verify(filterEditorPopup).show(any(),
captor.capture());
final FilterSettings filterSettings = new FilterSettings();
captor.getValue().accept(filterSettings);

verify(filterSettingsManager).saveFilterIntoPreferences(eq(filterSettings),
captor.capture());
captor.getValue().accept(false);

verify(filterEditorPopup).setTableNameError(any());
}

public abstract void testLoadFilters();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* 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 org.jbpm.workbench.common.client.filters.basic;

import org.jbpm.workbench.common.client.filters.active.ActiveFilterItem;
import org.jbpm.workbench.common.client.filters.active.ActiveFiltersImpl;
import org.jbpm.workbench.common.client.filters.active.ActiveFiltersView;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.uberfire.mvp.ParameterizedCommand;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class ActiveFiltersImplTest {

@Mock
ActiveFiltersView view;

@InjectMocks
ActiveFiltersImpl activeFilters;

@Test
public void testRemoveAllActiveFilters() {
activeFilters.removeAllActiveFilters();

verify(view).removeAllActiveFilters();
}

@Test
public void testAddActiveFilter() {
final ActiveFilterItem<Object> filter = new ActiveFilterItem<>();
activeFilters.addActiveFilter(filter);

verify(view).addActiveFilter(filter);
}

@Test
public void testSaveFilterCallbackNull() {
doAnswer(invocation -> {
ParameterizedCommand<String> callback = (ParameterizedCommand<String>) invocation.getArguments()[0];
callback.execute(null);
return null;
}).when(view).setSaveFilterCallback(any());

activeFilters.setSaveFilterCallback((name, callback) -> callback.accept(null));
activeFilters.init();

verify(view).setSaveFilterCallback(any());
verify(view).closeSaveFilter();
verify(view,
never()).setSaveFilterErrorMessage(any());
}

@Test
public void testSaveFilterCallbackError() {
final String message = "error";
doAnswer(invocation -> {
ParameterizedCommand<String> callback = (ParameterizedCommand<String>) invocation.getArguments()[0];
callback.execute(null);
return null;
}).when(view).setSaveFilterCallback(any());

activeFilters.setSaveFilterCallback((name, callback) -> callback.accept(message));
activeFilters.init();

verify(view).setSaveFilterCallback(any());
verify(view,
never()).closeSaveFilter();
verify(view).setSaveFilterErrorMessage(message);
}
}
Loading

0 comments on commit b325769

Please sign in to comment.