Skip to content

Commit

Permalink
[Import Export] Enhance unit tests for Import action
Browse files Browse the repository at this point in the history
Improve test coverage for Import actions.
  • Loading branch information
VWoeltjen committed Dec 20, 2013
1 parent e5092ab commit 132ed64
Showing 1 changed file with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,56 @@
*******************************************************************************/
package gov.nasa.arc.mct.importExport.provider;

import gov.nasa.arc.mct.importExport.provider.ImportAction;
import gov.nasa.arc.mct.components.AbstractComponent;
import gov.nasa.arc.mct.gui.ActionContext;
import gov.nasa.arc.mct.gui.View;
import gov.nasa.arc.mct.platform.spi.Platform;
import gov.nasa.arc.mct.platform.spi.PlatformAccess;
import gov.nasa.arc.mct.policy.ExecutionResult;
import gov.nasa.arc.mct.policy.PolicyContext;
import gov.nasa.arc.mct.services.component.PolicyManager;
import gov.nasa.jsc.mct.importExport.utilities.Utilities;
import gov.nasa.jsc.mct.importExport.utilities.XMLFileFilter;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class ImportActionTest {
ImportAction importAction;
ImportThisAction importThisAction;
List<File> okFiles;

private Platform oldPlatform;
private Platform mockPlatform;
private PolicyManager mockPolicy;

@BeforeClass
public void setupPlatform() {
oldPlatform = PlatformAccess.getPlatform();
mockPlatform = Mockito.mock(Platform.class);
mockPolicy = Mockito.mock(PolicyManager.class);
Mockito.when(mockPlatform.getPolicyManager()).thenReturn(mockPolicy);
new PlatformAccess().setPlatform(mockPlatform);
}

@AfterClass
public void teardownPlatform() {
new PlatformAccess().setPlatform(oldPlatform);
}

@BeforeMethod
void setup() {
importAction = new ImportAction();
importThisAction = new ImportThisAction();
}

@Test
Expand Down Expand Up @@ -67,9 +98,54 @@ public void testActionPerformedFilterNotafile(){
}

@Test
public void testTop(){
importAction = new ImportAction();
//importAction.selectFiles(null);
public void testCanHandle() {
// Setup mocks
ActionContext mockContext = Mockito.mock(ActionContext.class);
View mockView = Mockito.mock(View.class);
AbstractComponent mockComponent = Mockito.mock(AbstractComponent.class);
Mockito.when(mockView.getManifestedComponent()).thenReturn(mockComponent);

// This-style import needs a window manifestation
Mockito.when(mockContext.getWindowManifestation()).thenReturn(null);
Assert.assertFalse(importThisAction.canHandle(mockContext));
Mockito.when(mockContext.getWindowManifestation()).thenReturn(mockView);
Assert.assertTrue(importThisAction.canHandle(mockContext));

// Objects-style import needs exactly one selection
Mockito.when(mockContext.getSelectedManifestations())
.thenReturn(Arrays.<View>asList());
Assert.assertFalse(importAction.canHandle(mockContext));
Mockito.when(mockContext.getSelectedManifestations())
.thenReturn(Arrays.<View>asList(mockView, mockView));
Assert.assertFalse(importAction.canHandle(mockContext));
Mockito.when(mockContext.getSelectedManifestations())
.thenReturn(Arrays.<View>asList(mockView));
Assert.assertTrue(importAction.canHandle(mockContext));
}

@Test
public void testIsEnabled() {
// Setup mocks (need to invoke canHandle for Action lifecycle)
ActionContext mockContext = Mockito.mock(ActionContext.class);
View mockView = Mockito.mock(View.class);
AbstractComponent mockComponent = Mockito.mock(AbstractComponent.class);
Mockito.when(mockView.getManifestedComponent()).thenReturn(mockComponent);

// Call canHandle (for lifecycle)
Mockito.when(mockContext.getWindowManifestation()).thenReturn(mockView);
Assert.assertTrue(importThisAction.canHandle(mockContext));
Mockito.when(mockContext.getSelectedManifestations())
.thenReturn(Arrays.<View>asList(mockView));
Assert.assertTrue(importAction.canHandle(mockContext));

// Check isEnabled - should depend on policy
for (boolean truth : new boolean[] {true, false}) {
Mockito.when(mockPolicy.execute(Mockito.anyString(), Mockito.<PolicyContext>any()))
.thenReturn(new ExecutionResult(null, truth, ""));
Assert.assertEquals(importAction.isEnabled(), truth);
Assert.assertEquals(importThisAction.isEnabled(), truth);
}

}
}

0 comments on commit 132ed64

Please sign in to comment.