Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Oct 3, 2012
1 parent cd54e78 commit 2788c17
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 25 deletions.
Expand Up @@ -19,14 +19,16 @@
@ActionHandlerFor(EventServiceConnectedAction.class)
public class EventServiceConnectedHandler extends AbstractActionHandler<EventServiceConnectedAction, NoOpResult>
{
@In
ZanataIdentity identity;

@In
TranslationWorkspaceManager translationWorkspaceManager;

@Override
public NoOpResult execute(EventServiceConnectedAction action, ExecutionContext context) throws ActionException
{
ZanataIdentity.instance().checkLoggedIn();
identity.checkLoggedIn();
TranslationWorkspace workspace = translationWorkspaceManager.getOrRegisterWorkspace(action.getWorkspaceId());
workspace.onEventServiceConnected(action.getEditorClientId(), action.getConnectionId());
return new NoOpResult();
Expand Down
Expand Up @@ -19,6 +19,8 @@
@ActionHandlerFor(ExitWorkspaceAction.class)
public class ExitWorkspaceHandler extends AbstractActionHandler<ExitWorkspaceAction, ExitWorkspaceResult>
{
@In
ZanataIdentity identity;

@In
TranslationWorkspaceManager translationWorkspaceManager;
Expand All @@ -27,7 +29,7 @@ public class ExitWorkspaceHandler extends AbstractActionHandler<ExitWorkspaceAct
public ExitWorkspaceResult execute(ExitWorkspaceAction action, ExecutionContext context) throws ActionException
{

ZanataIdentity.instance().checkLoggedIn();
identity.checkLoggedIn();

TranslationWorkspace workspace = translationWorkspaceManager.getOrRegisterWorkspace(action.getWorkspaceId());

Expand Down
Expand Up @@ -26,10 +26,7 @@ public class GetStatusCountHandler extends AbstractActionHandler<GetStatusCount,
{

@In
Session session;

@In
TranslationWorkspaceManager translationWorkspaceManager;
ZanataIdentity identity;

@In
DocumentDAO documentDAO;
Expand All @@ -38,7 +35,7 @@ public class GetStatusCountHandler extends AbstractActionHandler<GetStatusCount,
public GetStatusCountResult execute(GetStatusCount action, ExecutionContext context) throws ActionException
{

ZanataIdentity.instance().checkLoggedIn();
identity.checkLoggedIn();

Long docId = action.getDocumentId().getValue();
LocaleId localeId = action.getWorkspaceId().getLocaleId();
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.zanata.security.ZanataIdentity;
import org.zanata.webtrans.server.ActionHandlerFor;
import org.zanata.webtrans.shared.model.TransUnitUpdatePreview;
import org.zanata.webtrans.shared.model.TransUnitUpdateRequest;
Expand All @@ -37,7 +38,7 @@
import net.customware.gwt.dispatch.shared.ActionException;

/**
* Show the result of a {@link ReplaceText} action without making any persistent
* Show the result of a {@link org.zanata.webtrans.shared.rpc.ReplaceText} action without making any persistent
* changes.
*
* @author David Mason, damason@redhat.com
Expand All @@ -48,13 +49,14 @@
@ActionHandlerFor(PreviewReplaceText.class)
public class PreviewReplaceTextHandler extends AbstractActionHandler<PreviewReplaceText, PreviewReplaceTextResult>
{

@In(value = "webtrans.gwt.UpdateTransUnitHandler", create = true)
UpdateTransUnitHandler updateTransUnitHandler;
@In
ZanataIdentity identity;

@Override
public PreviewReplaceTextResult execute(PreviewReplaceText previewAction, ExecutionContext context) throws ActionException
{
identity.checkLoggedIn();

ReplaceTextHandler.replaceTextInUpdateRequests(previewAction.getAction());
List<TransUnitUpdatePreview> previews = new ArrayList<TransUnitUpdatePreview>();
for (TransUnitUpdateRequest request : previewAction.getAction().getUpdateRequests())
Expand Down
14 changes: 10 additions & 4 deletions zanata-war/src/test/java/org/zanata/model/TestFixture.java
Expand Up @@ -88,18 +88,24 @@ public static HTextFlow makeHTextFlow(long id, HLocale hLocale, ContentState con

public static UserWorkspaceContext userWorkspaceContext()
{
return userWorkspaceContext(true, true, "project", "master");
return userWorkspaceContext(true, true);
}

public static UserWorkspaceContext userWorkspaceContext(boolean projectActive, boolean hasWriteAccess, String projectSlug, String iterationSlug)
{
return new UserWorkspaceContext(workspaceContext(new LocaleId("en-US")), projectActive, hasWriteAccess, true);
ProjectIterationId projectIterationId = new ProjectIterationId(projectSlug, iterationSlug);
return new UserWorkspaceContext(new WorkspaceContext(new WorkspaceId(projectIterationId, LocaleId.EN_US), "workspaceName", LocaleId.EN_US.getId()), projectActive, hasWriteAccess, true);
}

public static WorkspaceContext workspaceContext(LocaleId localeId)
public static UserWorkspaceContext userWorkspaceContext(boolean projectActive, boolean hasWriteAccess)
{
return new UserWorkspaceContext(new WorkspaceContext(workspaceId(), "workspaceName", LocaleId.EN_US.getId()), projectActive, hasWriteAccess, true);
}

public static WorkspaceId workspaceId()
{
ProjectIterationId projectIterationId = new ProjectIterationId("project", "master");
return new WorkspaceContext(new WorkspaceId(projectIterationId, localeId), "workspaceName", localeId.getId());
return new WorkspaceId(projectIterationId, LocaleId.EN_US);
}

public static <E extends GwtEvent<?>> E extractFromEvents(List<? extends GwtEvent> events, final Class<E> eventType)
Expand Down
Expand Up @@ -63,7 +63,7 @@ public class AppPresenterTest
private DocumentListPresenter documentListPresenter;
@Mock
private SearchResultsPresenter searchResultPresenter;
private UserWorkspaceContext userWorkspace = TestFixture.userWorkspaceContext(true, true, "project", "master");
private UserWorkspaceContext userWorkspace = TestFixture.userWorkspaceContext();
@Mock
private WebTransMessages messages;
@Mock
Expand Down
Expand Up @@ -130,7 +130,7 @@ public void beforeMethod()
{
MockitoAnnotations.initMocks(this);
configHolder = new UserConfigHolder();
userWorkspaceContext = TestFixture.userWorkspaceContext(true, true, "project", "master");
userWorkspaceContext = TestFixture.userWorkspaceContext();
presenter = new TargetContentsPresenter(displayProvider, editorTranslators, eventBus, tableEditorMessages, sourceContentPresenter, configHolder, userWorkspaceContext, editorKeyShortcuts, historyPresenter);

verify(eventBus).addHandler(UserConfigChangeEvent.getType(), presenter);
Expand Down
Expand Up @@ -23,7 +23,6 @@
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.common.ContentState;
import org.zanata.common.LocaleId;
import org.zanata.common.TranslationStats;
import org.zanata.model.TestFixture;
Expand All @@ -41,7 +40,6 @@
import org.zanata.webtrans.shared.model.ProjectIterationId;
import org.zanata.webtrans.shared.model.TransMemoryResultItem;
import org.zanata.webtrans.shared.model.TransUnit;
import org.zanata.webtrans.shared.model.TransUnitId;
import org.zanata.webtrans.shared.model.UserWorkspaceContext;
import org.zanata.webtrans.shared.model.WorkspaceContext;
import org.zanata.webtrans.shared.model.WorkspaceId;
Expand Down Expand Up @@ -196,7 +194,9 @@ public void willDoNothingIfAlreadyHaveSubmittedRequest()
// Given: already have submitted request
GetTranslationMemory submittedRequest = mock(GetTranslationMemory.class);
presenter.setStatesForTesting(null, submittedRequest);
when(userWorkspaceContext.getWorkspaceContext()).thenReturn(TestFixture.workspaceContext(new LocaleId("zh")));
LocaleId localeId = new LocaleId("zh");
ProjectIterationId projectIterationId = new ProjectIterationId("project", "master");
when(userWorkspaceContext.getWorkspaceContext()).thenReturn(new WorkspaceContext(new WorkspaceId(projectIterationId, localeId), "workspaceName", localeId.getId()));
when(userWorkspaceContext.getSelectedDoc()).thenReturn(new DocumentInfo(new DocumentId(1), "doc.txt", "/pot", new LocaleId("en-US"), new TranslationStats()));

// When:
Expand Down Expand Up @@ -239,7 +239,8 @@ public void canFireSearchEvent()
{
// Given:
LocaleId targetLocale = new LocaleId("zh");
when(userWorkspaceContext.getWorkspaceContext()).thenReturn(TestFixture.workspaceContext(targetLocale));
ProjectIterationId projectIterationId = new ProjectIterationId("project", "master");
when(userWorkspaceContext.getWorkspaceContext()).thenReturn(new WorkspaceContext(new WorkspaceId(projectIterationId, targetLocale), "workspaceName", targetLocale.getId()));
LocaleId sourceLocale = new LocaleId("en-US");
when(userWorkspaceContext.getSelectedDoc()).thenReturn(new DocumentInfo(new DocumentId(1), "doc.txt", "/pot", sourceLocale, new TranslationStats()));
when(display.getTmTextBox()).thenReturn(tMTextBox);
Expand All @@ -266,7 +267,9 @@ public void canFireSearchEvent()
public void fireSearchEventCallbackOnFailure()
{
// Given:
when(userWorkspaceContext.getWorkspaceContext()).thenReturn(TestFixture.workspaceContext(new LocaleId("zh")));
LocaleId localeId = new LocaleId("zh");
ProjectIterationId projectIterationId = new ProjectIterationId("project", "master");
when(userWorkspaceContext.getWorkspaceContext()).thenReturn(new WorkspaceContext(new WorkspaceId(projectIterationId, localeId), "workspaceName", localeId.getId()));
when(userWorkspaceContext.getSelectedDoc()).thenReturn(new DocumentInfo(new DocumentId(1), "doc.txt", "/pot", new LocaleId("en-US"), new TranslationStats()));
when(display.getTmTextBox()).thenReturn(tMTextBox);
when(display.getSearchType()).thenReturn(searchType);
Expand All @@ -289,7 +292,9 @@ public void fireSearchEventCallbackOnFailure()
public void fireSearchEventCallbackOnSuccess()
{
// Given:
when(userWorkspaceContext.getWorkspaceContext()).thenReturn(TestFixture.workspaceContext(new LocaleId("zh")));
LocaleId localeId = new LocaleId("zh");
ProjectIterationId projectIterationId = new ProjectIterationId("project", "master");
when(userWorkspaceContext.getWorkspaceContext()).thenReturn(new WorkspaceContext(new WorkspaceId(projectIterationId, localeId), "workspaceName", localeId.getId()));
when(userWorkspaceContext.getSelectedDoc()).thenReturn(new DocumentInfo(new DocumentId(1), "doc.txt", "/pot", new LocaleId("en-US"), new TranslationStats()));
when(display.getTmTextBox()).thenReturn(tMTextBox);
when(tMTextBox.getText()).thenReturn("search query");
Expand All @@ -316,7 +321,9 @@ public void fireSearchEventCallbackOnSuccess()
public void fireSearchEventCallbackOnSuccessButResultIsEmpty()
{
// Given:
when(userWorkspaceContext.getWorkspaceContext()).thenReturn(TestFixture.workspaceContext(new LocaleId("zh")));
LocaleId localeId = new LocaleId("zh");
ProjectIterationId projectIterationId = new ProjectIterationId("project", "master");
when(userWorkspaceContext.getWorkspaceContext()).thenReturn(new WorkspaceContext(new WorkspaceId(projectIterationId, localeId), "workspaceName", localeId.getId()));
when(userWorkspaceContext.getSelectedDoc()).thenReturn(new DocumentInfo(new DocumentId(1), "doc.txt", "/pot", new LocaleId("en-US"), new TranslationStats()));
when(display.getTmTextBox()).thenReturn(tMTextBox);
when(display.getSearchType()).thenReturn(searchType);
Expand Down
Expand Up @@ -68,7 +68,7 @@ public class TranslationPresenterTest
public void beforeMethod()
{
MockitoAnnotations.initMocks(this);
userWorkspaceContext = TestFixture.userWorkspaceContext(true, true, "project", "master");
userWorkspaceContext = TestFixture.userWorkspaceContext();
presenter = new TranslationPresenter(display, eventBus, targetContentsPresenter, translationEditorPresenter, transMemoryPresenter, glossaryPresenter, messages, userWorkspaceContext, keyShortcutPresenter, navigationService);
}

Expand Down
@@ -0,0 +1,63 @@
package org.zanata.webtrans.server.rpc;

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.common.LocaleId;
import org.zanata.model.TestFixture;
import org.zanata.seam.SeamAutowire;
import org.zanata.security.ZanataIdentity;
import org.zanata.webtrans.server.TranslationWorkspace;
import org.zanata.webtrans.server.TranslationWorkspaceManager;
import org.zanata.webtrans.shared.auth.EditorClientId;
import org.zanata.webtrans.shared.model.WorkspaceId;
import org.zanata.webtrans.shared.rpc.EventServiceConnectedAction;

import net.customware.gwt.dispatch.shared.ActionException;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Test(groups = "unit-tests")
public class EventServiceConnectedHandlerTest
{
private EventServiceConnectedHandler handler;
@Mock
private ZanataIdentity identity;
@Mock
private TranslationWorkspaceManager translationWorkspaceManager;
@Mock
private TranslationWorkspace translationWorkspace;

@BeforeMethod
public void setUp() throws Exception
{
MockitoAnnotations.initMocks(this);
// @formatter:off
handler = SeamAutowire.instance()
.use("identity", identity)
.use("translationWorkspaceManager", translationWorkspaceManager)
.ignoreNonResolvable()
.autowire(EventServiceConnectedHandler.class);
// @formatter:on
}

@Test
public void testExecute() throws ActionException
{
WorkspaceId workspaceId = TestFixture.workspaceId();
EditorClientId editorClientId = new EditorClientId("sessionId", 1);
EventServiceConnectedAction action = new EventServiceConnectedAction("connectionId");
action.setWorkspaceId(workspaceId);
action.setEditorClientId(editorClientId);
when(translationWorkspaceManager.getOrRegisterWorkspace(workspaceId)).thenReturn(translationWorkspace);

handler.execute(action, null);

verify(identity).checkLoggedIn();
verify(translationWorkspace).onEventServiceConnected(editorClientId, "connectionId");
}
}
@@ -0,0 +1,69 @@
package org.zanata.webtrans.server.rpc;

import org.hamcrest.Matchers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.common.LocaleId;
import org.zanata.model.TestFixture;
import org.zanata.seam.SeamAutowire;
import org.zanata.security.ZanataIdentity;
import org.zanata.webtrans.server.TranslationWorkspace;
import org.zanata.webtrans.server.TranslationWorkspaceManager;
import org.zanata.webtrans.shared.auth.EditorClientId;
import org.zanata.webtrans.shared.model.Person;
import org.zanata.webtrans.shared.model.PersonId;
import org.zanata.webtrans.shared.model.WorkspaceId;
import org.zanata.webtrans.shared.rpc.ExitWorkspaceAction;
import org.zanata.webtrans.shared.rpc.ExitWorkspaceResult;

import static org.hamcrest.MatcherAssert.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Test(groups = "unit-tests")
public class ExitWorkspaceHandlerTest
{
private ExitWorkspaceHandler handler;
@Mock
private ZanataIdentity identity;
@Mock
private TranslationWorkspaceManager translationWorkspaceManager;
@Mock
private TranslationWorkspace translationWorkspace;

@BeforeMethod
public void setUp() throws Exception
{
MockitoAnnotations.initMocks(this);
// @formatter:off
handler = SeamAutowire.instance()
.use("identity", identity)
.use("translationWorkspaceManager", translationWorkspaceManager)
.ignoreNonResolvable()
.autowire(ExitWorkspaceHandler.class);
// @formatter:on
}

@Test
public void testExecute() throws Exception
{
Person person = new Person(new PersonId("pid"), "admin", null);
EditorClientId editorClientId = new EditorClientId("sessionId", 1);
WorkspaceId workspaceId = TestFixture.workspaceId();
when(translationWorkspaceManager.getOrRegisterWorkspace(workspaceId)).thenReturn(translationWorkspace);
ExitWorkspaceAction action = new ExitWorkspaceAction(person);
action.setEditorClientId(editorClientId);
action.setWorkspaceId(workspaceId);

ExitWorkspaceResult result = handler.execute(action, null);

verify(identity).checkLoggedIn();
verify(translationWorkspace).removeEditorClient(action.getEditorClientId());
assertThat(result.getuserName(), Matchers.equalTo(person.getId().toString()));
}
}

0 comments on commit 2788c17

Please sign in to comment.