Skip to content

Commit

Permalink
CHE-4236: fix factories (eclipse-che#4530)
Browse files Browse the repository at this point in the history
  • Loading branch information
Valeriy Svydenko authored Mar 23, 2017
1 parent 9bb90c3 commit efbca14
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@
import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;

import org.eclipse.che.api.promises.client.Operation;
import org.eclipse.che.api.promises.client.OperationException;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.api.workspace.shared.dto.SourceStorageDto;
import org.eclipse.che.ide.CoreLocalizationConstant;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.data.tree.Node;
import org.eclipse.che.ide.api.dialogs.DialogFactory;
import org.eclipse.che.ide.api.event.SelectionChangedEvent;
import org.eclipse.che.ide.api.event.SelectionChangedHandler;
import org.eclipse.che.ide.api.notification.NotificationManager;
import org.eclipse.che.ide.api.resources.Project;
import org.eclipse.che.ide.api.resources.Resource;
import org.eclipse.che.ide.api.resources.marker.Marker;
import org.eclipse.che.ide.part.explorer.project.ProjectExplorerPresenter;
import org.eclipse.che.ide.resources.tree.ResourceNode;
import org.eclipse.che.ide.util.loging.Log;

import java.util.List;
import java.util.Map;

import static com.google.common.base.Strings.isNullOrEmpty;
Expand All @@ -43,16 +45,16 @@
* Project problem with code 10 means that project exist in workspace but it is absent on file system.
*/
@Singleton
public class ProjectConfigSynchronized implements SelectionChangedHandler {
public class ProjectConfigSynchronized {
private final AppContext appContext;
private final DialogFactory dialogFactory;
private final CoreLocalizationConstant locale;
private final NotificationManager notificationManager;
private final ChangeLocationWidget changeLocationWidget;

@Inject
public ProjectConfigSynchronized(EventBus eventBus,
AppContext appContext,
public ProjectConfigSynchronized(AppContext appContext,
ProjectExplorerPresenter projectExplorerPresenter,
DialogFactory dialogFactory,
CoreLocalizationConstant locale,
NotificationManager notificationManager,
Expand All @@ -63,16 +65,23 @@ public ProjectConfigSynchronized(EventBus eventBus,
this.notificationManager = notificationManager;
this.changeLocationWidget = changeLocationWidget;

eventBus.addHandler(SelectionChangedEvent.TYPE, this);
}
projectExplorerPresenter.getTree().getNodeLoader().addBeforeLoadHandler(event -> {
final Node requestedNode = event.getRequestedNode();
if (requestedNode == null || !(requestedNode instanceof ResourceNode)) {
return;
}

@Override
public void onSelectionChanged(SelectionChangedEvent event) {
final Project project = appContext.getRootProject();
if (project == null) {
return;
}
final ResourceNode resourceNode = (ResourceNode)requestedNode;
final Resource data = resourceNode.getData();
if (!data.isProject()) {
return;
}

checkProjectProblems((Project)data);
});
}

private void checkProjectProblems(Project project) {
final Optional<Marker> marker = project.getMarker(PROBLEM_PROJECT);
if (!marker.isPresent()) {
return;
Expand All @@ -83,7 +92,8 @@ public void onSelectionChanged(SelectionChangedEvent event) {

//If no project folder on file system
final String noProjectFolderProblem = problems.get(NO_PROJECT_ON_FILE_SYSTEM);
if (!isNullOrEmpty(noProjectFolderProblem)) {
final List<String> importingProjects = appContext.getImportingProjects();
if (!isNullOrEmpty(noProjectFolderProblem) && !importingProjects.contains(project.getPath())) {
showImportDialog(project);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.google.common.base.Optional;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwtmockito.GwtMockitoTestRunner;
import com.google.web.bindery.event.shared.EventBus;

import org.eclipse.che.api.promises.client.Operation;
import org.eclipse.che.api.promises.client.Promise;
Expand All @@ -24,17 +23,20 @@
import org.eclipse.che.ide.api.dialogs.ConfirmCallback;
import org.eclipse.che.ide.api.dialogs.ConfirmDialog;
import org.eclipse.che.ide.api.dialogs.DialogFactory;
import org.eclipse.che.ide.api.event.SelectionChangedEvent;
import org.eclipse.che.ide.api.notification.NotificationManager;
import org.eclipse.che.ide.api.resources.Container;
import org.eclipse.che.ide.api.resources.Project;
import org.eclipse.che.ide.api.resources.marker.Marker;
import org.eclipse.che.ide.part.explorer.project.ProjectExplorerPresenter;
import org.eclipse.che.ide.resources.tree.ResourceNode;
import org.eclipse.che.ide.ui.smartTree.NodeLoader;
import org.eclipse.che.ide.ui.smartTree.Tree;
import org.eclipse.che.ide.ui.smartTree.event.BeforeLoadEvent;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Matchers;
import org.mockito.Mock;

Expand All @@ -59,11 +61,11 @@ public class ProjectConfigSynchronizedTest {
private final static String IMPORT_BUTTON = "Import";
private final static String REMOVE_BUTTON = "Remove";

@Mock
private EventBus eventBus;
@Mock
private AppContext appContext;
@Mock
private ProjectExplorerPresenter projectExplorerPresenter;
@Mock
private DialogFactory dialogFactory;
@Mock
private CoreLocalizationConstant locale;
Expand All @@ -72,11 +74,6 @@ public class ProjectConfigSynchronizedTest {
@Mock
private ChangeLocationWidget changeLocationWidget;

@InjectMocks
private ProjectConfigSynchronized projectConfigSynchronized;

@Mock
private Project rootProject;
@Mock
private Optional<Marker> problemMarker;
@Mock
Expand All @@ -92,26 +89,42 @@ public class ProjectConfigSynchronizedTest {
@Mock
private SourceStorageDto sourceStorage;
@Mock
private BeforeLoadEvent beforeLoadEvent;
@Mock
private Container wsRoot;
@Mock
private Project.ProjectRequest projectRequest;
@Mock
private Tree tree;
@Mock
private NodeLoader nodeLoader;
@Mock
private ResourceNode requestedNode;
@Mock
private Project resource;

@Captor
private ArgumentCaptor<ConfirmCallback> confirmCallbackArgumentCaptor;
private ArgumentCaptor<ConfirmCallback> confirmCallbackArgumentCaptor;
@Captor
private ArgumentCaptor<CancelCallback> cancelCallbackArgumentCaptor;
private ArgumentCaptor<CancelCallback> cancelCallbackArgumentCaptor;
@Captor
private ArgumentCaptor<Operation<Void>> projectDeleted;
private ArgumentCaptor<Operation<Void>> projectDeleted;
@Captor
private ArgumentCaptor<BeforeLoadEvent.BeforeLoadHandler> beforeLoadHandlerArgumentCaptor;

Map<Integer, String> problems;
private Map<Integer, String> problems;

@Before
public void setUp() {
when(appContext.getRootProject()).thenReturn(rootProject);
when(rootProject.getMarker(Project.ProblemProjectMarker.PROBLEM_PROJECT)).thenReturn(problemMarker);
when(rootProject.getName()).thenReturn(PROJECT_NAME);
when(resource.getMarker(Project.ProblemProjectMarker.PROBLEM_PROJECT)).thenReturn(problemMarker);
when(resource.getName()).thenReturn(PROJECT_NAME);
when(problemMarker.isPresent()).thenReturn(true);
when(problemMarker.get()).thenReturn(problemProjectMarker);
when(projectExplorerPresenter.getTree()).thenReturn(tree);
when(tree.getNodeLoader()).thenReturn(nodeLoader);
when(beforeLoadEvent.getRequestedNode()).thenReturn(requestedNode);
when(requestedNode.getData()).thenReturn(resource);
when(resource.isProject()).thenReturn(true);

problems = new HashMap<>();
problems.put(10, "Error project");
Expand All @@ -128,33 +141,31 @@ public void setUp() {
anyObject(),
anyObject())).thenReturn(confirmDialog);

when(rootProject.getSource()).thenReturn(sourceStorage);
when(resource.getSource()).thenReturn(sourceStorage);
when(sourceStorage.getLocation()).thenReturn(PROJECT_LOCATION);
when(appContext.getWorkspaceRoot()).thenReturn(wsRoot);
when(wsRoot.importProject()).thenReturn(projectRequest);
when(projectRequest.withBody(anyObject())).thenReturn(projectRequest);
when(projectRequest.send()).thenReturn(projectPromise);
}

@Test
public void shouldSubscribeOnSelectionChangedEvent() throws Exception {
verify(eventBus).addHandler(SelectionChangedEvent.TYPE, projectConfigSynchronized);
ProjectConfigSynchronized projectConfigSynchronized = new ProjectConfigSynchronized(appContext,
projectExplorerPresenter,
dialogFactory,
locale,
notificationManager,
changeLocationWidget);
}

@Test
public void dialogIsNotShownIfRootProjectIsNull() throws Exception {
when(appContext.getRootProject()).thenReturn(null);

projectConfigSynchronized.onSelectionChanged(null);

verify(confirmDialog, never()).show();
private void subscribeToOnBeforeLoadNodeEvent() throws Exception {
verify(nodeLoader).addBeforeLoadHandler(beforeLoadHandlerArgumentCaptor.capture());
beforeLoadHandlerArgumentCaptor.getValue().onBeforeLoad(beforeLoadEvent);
}

@Test
public void dialogIsNotShownIfProjectHasNotMarkers() throws Exception {
when(problemMarker.isPresent()).thenReturn(false);

projectConfigSynchronized.onSelectionChanged(null);
subscribeToOnBeforeLoadNodeEvent();

verify(confirmDialog, never()).show();
}
Expand All @@ -163,27 +174,20 @@ public void dialogIsNotShownIfProjectHasNotMarkers() throws Exception {
public void dialogIsNotShownIfNoProjectProblem() throws Exception {
problems.clear();

projectConfigSynchronized.onSelectionChanged(null);
subscribeToOnBeforeLoadNodeEvent();

verify(confirmDialog, never()).show();
}

@Test
public void dialogShouldBeShow() throws Exception {
projectConfigSynchronized.onSelectionChanged(null);

verify(confirmDialog).show();
}

@Test
public void removeButtonIsClicked() throws Exception {
String projectRemoved = "project removed";

when(rootProject.delete()).thenReturn(deleteProjectPromise);
when(resource.delete()).thenReturn(deleteProjectPromise);
when(deleteProjectPromise.then(Matchers.<Operation<Void>>any())).thenReturn(deleteProjectPromise);
when(locale.projectRemoved(PROJECT_NAME)).thenReturn(projectRemoved);

projectConfigSynchronized.onSelectionChanged(null);
subscribeToOnBeforeLoadNodeEvent();

verify(dialogFactory).createConfirmDialog(eq(SYNCH_DIALOG_TITLE),
eq(SYNCH_DIALOG_CONTENT),
Expand All @@ -194,7 +198,7 @@ public void removeButtonIsClicked() throws Exception {
verify(confirmDialog).show();

cancelCallbackArgumentCaptor.getValue().cancelled();
verify(rootProject).delete();
verify(resource).delete();

verify(deleteProjectPromise).then(projectDeleted.capture());
projectDeleted.getValue().apply(null);
Expand All @@ -203,7 +207,7 @@ public void removeButtonIsClicked() throws Exception {

@Test
public void importButtonIsClicked() throws Exception {
projectConfigSynchronized.onSelectionChanged(null);
subscribeToOnBeforeLoadNodeEvent();

verify(dialogFactory).createConfirmDialog(eq(SYNCH_DIALOG_TITLE),
eq(SYNCH_DIALOG_CONTENT),
Expand All @@ -215,7 +219,7 @@ public void importButtonIsClicked() throws Exception {

confirmCallbackArgumentCaptor.getValue().accepted();
verify(wsRoot).importProject();
verify(projectRequest).withBody(rootProject);
verify(projectRequest).withBody(resource);
verify(projectRequest).send();
}

Expand All @@ -230,7 +234,7 @@ public void changeLocationWindowShouldBeShown() throws Exception {
anyObject(),
anyObject())).thenReturn(changeConfirmDialog);

projectConfigSynchronized.onSelectionChanged(null);
subscribeToOnBeforeLoadNodeEvent();

verify(dialogFactory).createConfirmDialog(eq(SYNCH_DIALOG_TITLE),
eq(SYNCH_DIALOG_CONTENT),
Expand All @@ -253,7 +257,7 @@ public void changeLocationWindowShouldBeShown() throws Exception {
verify(sourceStorage).setType("github");

verify(wsRoot).importProject();
verify(projectRequest).withBody(rootProject);
verify(projectRequest).withBody(resource);
verify(projectRequest).send();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,10 @@ public TreeElement getTree(@ApiParam(value = "Path to resource. Can be project o
ServerException {
final FolderEntry folder = projectManager.asFolder(path);

if (folder == null) {
throw new NotFoundException("Folder " + path + " was not found");
}

return newDto(TreeElement.class).withNode(injectFolderLinks(asDto(folder)))
.withChildren(getTree(folder, depth, includeFiles));
}
Expand Down

0 comments on commit efbca14

Please sign in to comment.