You are on page 1of 5

/*

* COPYRIGHT:
Copyright (c) 2015 by Nuance Communications, Inc.
* Warning:
This product is protected by United States copyright law.
*
Unauthorized use or duplication of this software, in whole
*
or in part, is prohibited.
*/
package com.nuance.him.utmc.client.ui.mtso.manager;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import

java.lang.reflect.Field;
java.util.ResourceBundle;
org.junit.Before;
org.junit.BeforeClass;
org.junit.Test;
org.junit.runner.RunWith;
org.mockito.ArgumentCaptor;
org.mockito.Mockito;
com.google.gwt.event.dom.client.HasClickHandlers;
com.google.gwt.event.shared.EventBus;
com.google.gwt.place.shared.PlaceController;
com.google.gwt.user.client.ui.AcceptsOneWidget;
com.google.gwtmockito.GwtMock;
com.google.gwtmockito.GwtMockitoTestRunner;
com.nuance.him.gwt.client.ReflectionUtil;
com.nuance.him.utmc.client.Utmc;
com.nuance.him.utmc.client.resources.MainConstants;
com.nuance.him.utmc.client.services.mtso.MtsoService;
com.nuance.him.utmc.client.ui.ClientFactory;
com.nuance.him.utmc.client.ui.UtmcApp;
com.nuance.him.utmc.client.ui.mtso.editor.MtsoEditorActionBarView;
com.nuance.him.utmc.client.ui.mtso.editor.MtsoEditorView;
com.nuance.him.utmc.client.ui.mtso.editor.MtsoModalView;
static org.junit.Assert.assertNull;
static org.mockito.Mockito.verify;
static org.mockito.Mockito.when;

/**
* Unit test case implementation for {@link MtsoManagerActivity}.
*
* @author parag_paralikar Created: Dec 16, 2015
*/
@RunWith(GwtMockitoTestRunner.class)
public class MtsoManagerActivityTest {
/**
* Mock {@link ClientFactory} object.
*/
@GwtMock
private ClientFactory clientFactoryMock;
/**
* Mock {@link MtsoService} object.
*/
@GwtMock
private MtsoService mtsoServiceMock;
/**
* Mock {@link PlaceController} object.
*/
@GwtMock
private PlaceController mockPlaceController;

/**
* Mock {@link EventBus} object.
*/
@GwtMock
private EventBus eventBusMock;
/**
* Mock {@link AcceptsOneWidget} object.
*/
@GwtMock
private AcceptsOneWidget acceptsOneWidgetMock;
/**
* Mock {@link MtsoManagerViewImpl} object.
*/
@GwtMock
private MtsoManagerViewImpl mtsoManagerViewImplMock;
@GwtMock
private MtsoEditorView mockMtsoEditorView;
@GwtMock
private MtsoModalView mockMtsoModalView;
@GwtMock
private MtsoEditorActionBarView mockMtsoEditorActionBarView;
/**
* Mock {@link MtsoManagerActionBarView} instance.
*/
@GwtMock
private MtsoManagerActionBarView mockMtsoManagerActionBarView;
@GwtMock
private MtsoManagerPresenter mockPresenter;
/**
* Mock {@link UtmcApp} instance.
*/
@GwtMock
private UtmcApp mockUtmcApp;
/**
* MTSOManagerPlace place object {@link MtsoManagerPlace}.
*/
private final MtsoManagerPlace mtsoManagerPlace = new MtsoManagerPlace("mtso
man");
@GwtMock
private MainConstants mockMainConstants;
/**
* {@link MtsoManagerActivity} instance.
*/
private MtsoManagerActivity mtsoManagerActivity;
@GwtMock
private HasClickHandlers mockHasClickHandlers;

/** The presenter field constant. */


private static final String PRESENTER_FIELD = "mtsoManagerPresenter";
/** The property constant field. */
private static final String CONSTANTS_FIELD = "constant";
/** The resource bundle instance. */
private static ResourceBundle constants;
/** The browser confirmation content. */
private static String unsavedDataConfirmation;
/** The main constants field constant. */
private static final String MAIN_CONSTANT_CLASS_NAME =
"com.nuance.him.utmc.client.resources.MainConstants";
/** The unsaved confirmation title. */
private static final String UNDAVED_DATA_CNF_KEY = "unsavedDataConfirmation"
;
/** The presenter field. */
private static Field presenterField;
/**
* Executes once before all test.
*
* @throws Exception is the {@link Exception} while initializing.
*/
@BeforeClass
public static void init() throws Exception {
constants = ResourceBundle.getBundle(MAIN_CONSTANT_CLASS_NAME);
unsavedDataConfirmation = constants.getString(UNDAVED_DATA_CNF_KEY);
presenterField = ReflectionUtil.getFieldObject(MtsoManagerActivity.class
.getName(), PRESENTER_FIELD);
}
/**
* Execute before each test.
*
* @throws Exception The exception thrown during execution.
*/
@Before
public void setup() throws Exception {
when(clientFactoryMock.getEventBus()).thenReturn(eventBusMock);
when(clientFactoryMock.getMtsoManager()).thenReturn(mtsoManagerViewImplM
ock);
when(clientFactoryMock.getPlaceController()).thenReturn((mockPlaceContro
ller));
when(clientFactoryMock.getApplication()).thenReturn(mockUtmcApp);
when(clientFactoryMock.getMtsoEditor()).thenReturn(mockMtsoEditorView);
when(clientFactoryMock.getMtsoModalView()).thenReturn(mockMtsoModalView)
;
when(clientFactoryMock.getMtsoEditorActionBarView()).thenReturn(mockMtso
EditorActionBarView);
when(mockMtsoModalView.getMtsoModalPanelEventBus()).thenReturn(eventBusM
ock);
when(mtsoManagerViewImplMock.getCreateMtsoAnchor()).thenReturn(mockHasCl
ickHandlers);
when(clientFactoryMock.getMtsoManagerActionBarView()).thenReturn(mockMts
oManagerActionBarView);

when(Utmc.getAppConstants().unsavedDataConfirmation()).thenReturn(unsave
dDataConfirmation);
mtsoManagerActivity = new MtsoManagerActivity(mtsoManagerPlace, clientFa
ctoryMock, mtsoServiceMock);
mtsoManagerActivity.doStart(acceptsOneWidgetMock, eventBusMock);
presenterField.set(mtsoManagerActivity, mockPresenter);
}
/**
* Confirm whether null pointer is thrown when {@link ClientFactory} is set
to null.
*/
@Test(expected = NullPointerException.class)
public void clientFactoryConstructorNullTest() {
mtsoManagerActivity = new MtsoManagerActivity(mtsoManagerPlace, null, mt
soServiceMock);
}
/**
* Confirm whether {@link IllegalArgumentException} is thrown when {@link Mt
soService} is set to null.
*/
@Test(expected = IllegalArgumentException.class)
public void mtsoServiceNullTest() {
mtsoManagerActivity = new MtsoManagerActivity(mtsoManagerPlace, clientFa
ctoryMock, null);
}
/**
* Confirm whether null pointer is thrown when {@link MtsoManagerPlace} is s
et to null.
*/
@Test(expected = IllegalArgumentException.class)
public void mtsoManagerPlaceNullTest() {
mtsoManagerActivity = new MtsoManagerActivity(null, clientFactoryMock, m
tsoServiceMock);
}
/**
* Test that {@link AcceptsOneWidget#setWidget(com.google.gwt.user.client.ui
.IsWidget)} is called at least
* once when {@link MtsoManagerActivity#start(AcceptsOneWidget, EventBus)} i
n invoked ie verify that the
* view is added to the panel.
*/
@Test
public void doStartTest() {
mtsoManagerActivity.doStart(acceptsOneWidgetMock, eventBusMock);
final ArgumentCaptor<MtsoManagerViewImpl> argument =
ArgumentCaptor.forClass(MtsoManagerViewImpl.class);
Mockito.verify(acceptsOneWidgetMock, Mockito.atLeastOnce()).setWidget(ar
gument.capture());
}
/**
* Test whether {@link MtsoManagerActivity#mayStop()} return null. Verifies
if activity returns null when
* user navigate away from activity.
*/

@Test
public void mayStopTest() {
when(mockPresenter.mayStop()).thenReturn(true);
assertNull(mtsoManagerActivity.mayStop());
}
/**
* Test the {@link MtsoManagerActivity#mayStop()}.
*
* @throws Exception is the {@link Exception} while executing the test.
*/
@Test
public void mayStopFalseTest() throws Exception {
when(mockPresenter.mayStop()).thenReturn(false);
Field field = ReflectionUtil.getFieldObject(MtsoManagerActivity.class.ge
tName(), CONSTANTS_FIELD);
field.set(mtsoManagerActivity, mockMainConstants);
mtsoManagerActivity.mayStop();
verify(mockMainConstants).unsavedDataConfirmation();
}
/**
* Test whether {@link MtsoManagerActivity#onCancel()} is invoked.
*/
@Test
public void onCancelTest() {
mtsoManagerActivity.onCancel();
}
/**
* Test whether {@link MtsoManagerActivity#doStop()} is invoked.
*/
@Test
public void doStopTest() {
mtsoManagerActivity.doStop();
}
}

You might also like