org.powermock.core.classloader.annotations.PrepareForTest Java Examples

The following examples show how to use org.powermock.core.classloader.annotations.PrepareForTest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BddStepPrinterTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest({ Vividus.class, BeanFactory.class, FileUtils.class })
public void testPrintToFile() throws Exception
{
    List<String> expectedOutput = mockStepCandidates();
    String filePath = "mocked" + File.separator + "file";
    mockStatic(FileUtils.class);
    BddStepPrinter.main(new String[] {"-f", filePath});
    Path file = Paths.get(filePath);
    assertOutput(List.of("File with BDD steps: " + file.toAbsolutePath()));
    PowerMockito.verifyStatic(FileUtils.class);
    FileUtils.writeLines(argThat(f -> filePath.equals(f.toString())), argThat(steps -> steps.stream()
                .map(Object::toString)
                .collect(Collectors.toList())
            .equals(expectedOutput)));
}
 
Example #2
Source File: TransformWithMappingMetaTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest( TransformWithMappingMeta.class )
public void activateParamsTest() throws Exception {
  String childParam = "childParam";
  String childValue = "childValue";
  String paramOverwrite = "paramOverwrite";
  String parentValue = "parentValue";
  String transformValue = "transformValue";

  IVariables parent = new Variables();
  parent.setVariable( paramOverwrite, parentValue );

  PipelineMeta childVariableSpace = new PipelineMeta();
  childVariableSpace.addParameterDefinition( childParam, "", "" );
  childVariableSpace.setParameterValue( childParam, childValue );

  String[] parameters = childVariableSpace.listParameters();
  TransformWithMappingMeta.activateParams( childVariableSpace, childVariableSpace, parent,
    parameters, new String[] { childParam, paramOverwrite }, new String[] { childValue, transformValue }, true );

  Assert.assertEquals( childValue, childVariableSpace.getVariable( childParam ) );
  // the transform parameter prevails
  Assert.assertEquals( transformValue, childVariableSpace.getVariable( paramOverwrite ) );
}
 
Example #3
Source File: PluginManagerTest.java    From plugin-installation-manager-tool with MIT License 6 votes vote down vote up
@Test
@PrepareForTest({HttpClients.class, HttpClientContext.class, HttpHost.class})
public void bundledPluginsTest() {
    URL warURL = this.getClass().getResource("/bundledplugintest.war");
    File testWar = new File(warURL.getFile());

    Config config = Config.builder()
            .withJenkinsWar(testWar.toString())
            .build();
    PluginManager pluginManager = new PluginManager(config);

    Map <String, Plugin> expectedPlugins = new HashMap<>();
    expectedPlugins.put("credentials", new Plugin("credentials","2.1.18", null, null));
    expectedPlugins.put("display-url-api", new Plugin("display-url-api","2.0", null, null));
    expectedPlugins.put("github-branch-source", new Plugin("github-branch-source", "1.8", null, null));

    Map<String, Plugin> actualPlugins = pluginManager.bundledPlugins();

    List<String> actualPluginInfo = convertPluginsToStrings(new ArrayList(actualPlugins.values()));
    List<String> expectedPluginInfo = convertPluginsToStrings(new ArrayList<>(expectedPlugins.values()));

    assertEquals(expectedPluginInfo, actualPluginInfo);
}
 
Example #4
Source File: TransformWithMappingMetaTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest( TransformWithMappingMeta.class )
public void activateParamsTestWithNoParameterChild() throws Exception {
  String newParam = "newParamParent";
  String parentValue = "parentValue";

  PipelineMeta parentMeta = new PipelineMeta();
  PipelineMeta childVariableSpace = new PipelineMeta();

  String[] parameters = childVariableSpace.listParameters();

  TransformWithMappingMeta.activateParams( childVariableSpace, childVariableSpace, parentMeta,
    parameters, new String[] { newParam }, new String[] { parentValue }, true );

  Assert.assertEquals( parentValue, childVariableSpace.getParameterValue( newParam ) );
}
 
Example #5
Source File: FileUtilsTest.java    From lsp4intellij with Apache License 2.0 6 votes vote down vote up
@PrepareForTest(FileUtils.class)
@Test
public void testEditorToProjectFolderPath() throws Exception {
  Assert.assertNull(FileUtils.editorToProjectFolderPath(null));

  PowerMockito.spy(FileUtils.class);
  Editor editor = PowerMockito.mock(Editor.class);
  PowerMockito.when(editor.getProject()).thenReturn(PowerMockito.mock(Project.class));
  PowerMockito.when(editor.getProject().getBasePath()).thenReturn("test");

  File file = PowerMockito.mock(File.class);
  PowerMockito.when(file.getAbsolutePath()).thenReturn("fooBar");
  PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(file);

  Assert.assertEquals("fooBar", FileUtils.editorToProjectFolderPath(editor));
}
 
Example #6
Source File: PrepareExecutionPipelineServletTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest( { Encode.class } )
public void testPausePipelineServletEscapesHtmlWhenPipelineNotFound() throws ServletException, IOException {
  HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
  HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );

  StringWriter out = new StringWriter();
  PrintWriter printWriter = new PrintWriter( out );

  PowerMockito.spy( Encode.class );
  when( mockHttpServletRequest.getContextPath() ).thenReturn( PrepareExecutionPipelineServlet.CONTEXT_PATH );
  when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST );
  when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );

  prepareExecutionPipelineServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );
  assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H1", out.toString() ) ) );

  PowerMockito.verifyStatic( atLeastOnce() );
  Encode.forHtml( anyString() );
}
 
Example #7
Source File: FileUtilsTest.java    From lsp4intellij with Apache License 2.0 6 votes vote down vote up
@PrepareForTest(FileEditorManager.class)
@Test
public void testEditorFromVirtualFile() {
  VirtualFile file = PowerMockito.mock(VirtualFile.class);
  Project project = PowerMockito.mock(Project.class);

  Editor editor = PowerMockito.mock(Editor.class);
  TextEditor textEditor = PowerMockito.mock(TextEditor.class);
  PowerMockito.when(textEditor.getEditor()).thenReturn(editor);

  FileEditorManagerEx fileEditorManagerEx = PowerMockito.mock(FileEditorManagerEx.class);
  PowerMockito.mockStatic(FileEditorManager.class);
  PowerMockito.when(fileEditorManagerEx.getAllEditors(file))
      .thenReturn(new FileEditor[]{textEditor})
      .thenReturn(new FileEditor[0]);
  PowerMockito.when(FileEditorManager.getInstance(project)).thenReturn(fileEditorManagerEx);

  Assert.assertEquals(editor, FileUtils.editorFromVirtualFile(file, project));

  Assert.assertNull(FileUtils.editorFromVirtualFile(file, project));
}
 
Example #8
Source File: SniffTransformServletTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest( { Encode.class } )
public void testSniffTransformServletEscapesHtmlWhenPipelineNotFound() throws ServletException, IOException {
  HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
  HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );

  StringWriter out = new StringWriter();
  PrintWriter printWriter = new PrintWriter( out );

  PowerMockito.spy( Encode.class );
  when( mockHttpServletRequest.getContextPath() ).thenReturn( SniffTransformServlet.CONTEXT_PATH );
  when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST );
  when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );

  sniffTransformServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );
  assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H1", out.toString() ) ) );

  PowerMockito.verifyStatic( atLeastOnce() );
  Encode.forHtml( anyString() );
}
 
Example #9
Source File: StartWorkflowServletTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest( { Encode.class } )
public void testStartWorkflowServletEscapesHtmlWhenPipelineNotFound() throws ServletException, IOException {
  HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
  HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );

  StringWriter out = new StringWriter();
  PrintWriter printWriter = new PrintWriter( out );

  PowerMockito.spy( Encode.class );
  when( mockHttpServletRequest.getContextPath() ).thenReturn( StartWorkflowServlet.CONTEXT_PATH );
  when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST );
  when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );

  startJobServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );
  assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H1", out.toString() ) ) );

  PowerMockito.verifyStatic( atLeastOnce() );
  Encode.forHtml( anyString() );
}
 
Example #10
Source File: GetPipelineStatusServletTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest( { Encode.class } )
public void testGetPipelineStatusServletEscapesHtmlWhenPipelineNotFound() throws ServletException, IOException {
  HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
  HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );

  StringWriter out = new StringWriter();
  PrintWriter printWriter = new PrintWriter( out );

  PowerMockito.spy( Encode.class );
  when( mockHttpServletRequest.getContextPath() ).thenReturn( GetPipelineStatusServlet.CONTEXT_PATH );
  when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST );
  when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );

  getPipelineStatusServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );
  assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H1", out.toString() ) ) );

  PowerMockito.verifyStatic( atLeastOnce() );
  Encode.forHtml( anyString() );
}
 
Example #11
Source File: GetWorkflowStatusServletTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest( { Encode.class } )
public void testGetJobStatusServletEscapesHtmlWhenPipelineNotFound() throws ServletException, IOException {
  HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
  HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );

  StringWriter out = new StringWriter();
  PrintWriter printWriter = new PrintWriter( out );

  PowerMockito.spy( Encode.class );
  when( mockHttpServletRequest.getContextPath() ).thenReturn( GetWorkflowStatusServlet.CONTEXT_PATH );
  when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST );
  when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );

  getWorkflowStatusServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );

  assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H1", out.toString() ) ) );
  PowerMockito.verifyStatic( atLeastOnce() );
  Encode.forHtml( anyString() );

}
 
Example #12
Source File: PausePipelineServletTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest( { Encode.class } )
public void testPausePipelineServletEscapesHtmlWhenPipelineNotFound() throws ServletException, IOException {
  HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
  HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );

  StringWriter out = new StringWriter();
  PrintWriter printWriter = new PrintWriter( out );

  PowerMockito.spy( Encode.class );
  when( mockHttpServletRequest.getContextPath() ).thenReturn( PausePipelineServlet.CONTEXT_PATH );
  when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST );
  when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );

  pausePipelineServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );
  assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H1", out.toString() ) ) );

  PowerMockito.verifyStatic( atLeastOnce() );
  Encode.forHtml( anyString() );
}
 
Example #13
Source File: StartPipelineServletTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest( { Encode.class } )
public void testStartPipelineServletEscapesHtmlWhenPipelineNotFound() throws ServletException, IOException {
  HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
  HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );

  StringWriter out = new StringWriter();
  PrintWriter printWriter = new PrintWriter( out );

  PowerMockito.spy( Encode.class );
  when( mockHttpServletRequest.getContextPath() ).thenReturn( StartPipelineServlet.CONTEXT_PATH );
  when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST );
  when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );

  startPipelineServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );
  assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H1", out.toString() ) ) );

  PowerMockito.verifyStatic( atLeastOnce() );
  Encode.forHtml( anyString() );
}
 
Example #14
Source File: RemoveWorkflowServletTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest( { Encode.class } )
public void testRemoveWorkflowServletEscapesHtmlWhenPipelineNotFound() throws ServletException, IOException {
  HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
  HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );

  StringWriter out = new StringWriter();
  PrintWriter printWriter = new PrintWriter( out );

  PowerMockito.spy( Encode.class );
  when( mockHttpServletRequest.getContextPath() ).thenReturn( RemoveWorkflowServlet.CONTEXT_PATH );
  when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST );
  when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );

  removeWorkflowServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );
  assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H1", out.toString() ) ) );

  PowerMockito.verifyStatic( atLeastOnce() );
  Encode.forHtml( anyString() );
}
 
Example #15
Source File: StopWorkflowServletTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest( { Encode.class } )
public void testStopJobServletEscapesHtmlWhenPipelineNotFound() throws ServletException, IOException {
  HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
  HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );

  StringWriter out = new StringWriter();
  PrintWriter printWriter = new PrintWriter( out );

  PowerMockito.spy( Encode.class );
  when( mockHttpServletRequest.getContextPath() ).thenReturn( StopWorkflowServlet.CONTEXT_PATH );
  when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST );
  when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );

  stopWorkflowServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );
  assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H1", out.toString() ) ) );

  PowerMockito.verifyStatic( atLeastOnce() );
  Encode.forHtml( anyString() );
}
 
Example #16
Source File: RemovePipelineServletTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest( { Encode.class } )
public void testRemovePipelineServletEscapesHtmlWhenPipelineNotFound() throws ServletException, IOException {
  HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
  HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );

  StringWriter out = new StringWriter();
  PrintWriter printWriter = new PrintWriter( out );

  PowerMockito.spy( Encode.class );
  when( mockHttpServletRequest.getContextPath() ).thenReturn( RemovePipelineServlet.CONTEXT_PATH );
  when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST );
  when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );

  removePipelineServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );
  assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H1", out.toString() ) ) );

  PowerMockito.verifyStatic( atLeastOnce() );
  Encode.forHtml( anyString() );
}
 
Example #17
Source File: ProxyServerFactoryTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest({BrowserUpProxyServer.class, ThreadPoolConfiguration.class, ProxyServerFactory.class})
public void testCreateProxyServerConfigDisableMitm() throws Exception
{
    MitmManagerOptions mitmManagerOptions = mock(MitmManagerOptions.class);
    IMitmManagerFactory mitmManagerFactory = mock(IMitmManagerFactory.class);
    MitmManager mitmManager = mock(MitmManager.class);
    when(mitmManagerFactory.createMitmManager(mitmManagerOptions)).thenReturn(mitmManager);
    BrowserUpProxyServer mockedServer = mock(BrowserUpProxyServer.class);
    PowerMockito.whenNew(BrowserUpProxyServer.class).withNoArguments().thenReturn(mockedServer);

    proxyServerFactory.setMitmManagerOptions(mitmManagerOptions);
    proxyServerFactory.setMitmManagerFactory(mitmManagerFactory);
    proxyServerFactory.setMitmEnabled(true);
    proxyServerFactory.createProxyServer();

    verify(mockedServer).setMitmManager(mitmManager);
}
 
Example #18
Source File: ElementStepsTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest({ ElementSteps.class, FileUtils.class })
public void testLoadFileFromJar() throws Exception
{
    File file = mockFileForUpload();
    PowerMockito.mockStatic(FileUtils.class);
    PowerMockito.whenNew(File.class).withArguments(FILE_PATH).thenReturn(file);
    Resource resource = mockResource("jar:file:/D:/archive.jar!/file.txt", file, true);
    InputStream inputStream = new ByteArrayInputStream(TEXT.getBytes(StandardCharsets.UTF_8));
    when(resource.getInputStream()).thenReturn(inputStream);
    mockResourceLoader(resource);
    when(softAssert.assertTrue(FILE_FILE_PATH_EXISTS, true)).thenReturn(true);
    SearchAttributes searchAttributes = new SearchAttributes(ActionAttributeType.XPATH,
            new SearchParameters(XPATH).setVisibility(Visibility.ALL));
    when(baseValidations.assertIfElementExists(AN_ELEMENT, searchAttributes)).thenReturn(webElement);
    elementSteps.uploadFile(searchAttributes, FILE_PATH);
    verify(webElement).sendKeys(ABSOLUTE_PATH);
    PowerMockito.verifyStatic(FileUtils.class);
    FileUtils.copyInputStreamToFile(eq(resource.getInputStream()), any(File.class));
}
 
Example #19
Source File: StartExecutionPipelineServletTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest( { Encode.class } )
public void testStartExecutionPipelineServletEscapesHtmlWhenPipelineNotFound() throws ServletException, IOException {
  HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
  HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );

  StringWriter out = new StringWriter();
  PrintWriter printWriter = new PrintWriter( out );

  PowerMockito.spy( Encode.class );
  when( mockHttpServletRequest.getContextPath() ).thenReturn( StartExecutionPipelineServlet.CONTEXT_PATH );
  when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST );
  when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );

  startExecutionPipelineServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );
  assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H1", out.toString() ) ) );

  PowerMockito.verifyStatic( atLeastOnce() );
  Encode.forHtml( anyString() );
}
 
Example #20
Source File: MongoDbStepsTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@PrepareForTest(MongoClients.class)
@Test
public void testExecuteCommands()
{
    MongoDatabase database = mockDatabase();
    MongoCollection<Document> collection = mock(MongoCollection.class);
    when(database.getCollection(COLLECTION_KEY)).thenReturn(collection);
    Bson argument = mock(Bson.class);
    FindIterable<Document> findIterable = mock(FindIterable.class);
    when(collection.find(argument)).thenReturn(findIterable);
    when(findIterable.spliterator()).thenReturn(List.of(DOCUMENT).spliterator());

    MongoDbSteps steps = new MongoDbSteps(Map.of(LOCAL_KEY, CONNECTION_KEY), jsonUtils, context);

    steps.executeCommands(List.of(
        commandEntry(MongoCommand.FIND, argument),
        commandEntry(MongoCommand.COLLECT, argument)
        ), COLLECTION_KEY, LOCAL_KEY, LOCAL_KEY, Set.of(VariableScope.STORY), VARIABLE_KEY);
    verify(context).putVariable(Set.of(VariableScope.STORY), VARIABLE_KEY, String.format("[%s]", DOCUMENT_JSON));
}
 
Example #21
Source File: ExtendedConfigurationTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest(ExtendedConfiguration.class)
public void testInit() throws Exception
{
    String compositePathPatterns = "**/*.steps";
    List<String> compositePaths = List.of("/path/to/composite.steps");
    when(pathFinder.findPaths(equalToCompositeStepsBatch(compositePathPatterns))).thenReturn(compositePaths);

    ExtendedConfiguration spy = spy(configuration);
    Keywords keywords = mock(Keywords.class);
    PowerMockito.whenNew(Keywords.class).withArguments(Keywords.defaultKeywords()).thenReturn(keywords);
    ExamplesTableFactory examplesTableFactory = mock(ExamplesTableFactory.class);
    when(spy.examplesTableFactory()).thenReturn(examplesTableFactory);
    RegexStoryParser regexStoryParser = mock(RegexStoryParser.class);
    PowerMockito.whenNew(RegexStoryParser.class).withArguments(keywords, examplesTableFactory)
            .thenReturn(regexStoryParser);
    ParameterConvertersDecorator parameterConverters = mock(ParameterConvertersDecorator.class);
    PowerMockito.whenNew(ParameterConvertersDecorator.class).withArguments(spy, parameterAdaptor, expressionAdaptor)
            .thenReturn(parameterConverters);
    List<ChainableParameterConverter<?, ?>> parameterConverterList = List.of();
    when(parameterConverters.addConverters(parameterConverterList)).thenReturn(parameterConverters);
    StoryControls storyControls = mock(StoryControls.class);
    spy.setCustomConverters(parameterConverterList);
    spy.setCompositePaths(compositePathPatterns);
    spy.setStoryControls(storyControls);
    List<StepMonitor> stepMonitors = List.of(mock(StepMonitor.class));
    spy.setStepMonitors(stepMonitors);
    spy.init();

    verify(spy).useKeywords(keywords);
    verify(spy).useCompositePaths(new HashSet<>(compositePaths));

    InOrder inOrder = inOrder(spy);
    inOrder.verify(spy).useParameterConverters(parameterConverters);
    inOrder.verify(spy).useStoryParser(regexStoryParser);

    verify(spy).useStoryControls(storyControls);
    verifyStepMonitor(spy, stepMonitors.get(0));
}
 
Example #22
Source File: StartExecutionPipelineServletTest.java    From hop with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest( { Encode.class } )
public void testStartExecutionPipelineServletEscapesHtmlWhenPipelineFound() throws ServletException, IOException {
  HopLogStore.init();
  HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
  HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );
  Pipeline mockPipeline = mock( Pipeline.class );
  PipelineMeta mockPipelineMeta = mock( PipelineMeta.class );
  ILogChannel mockChannelInterface = mock( ILogChannel.class );
  StringWriter out = new StringWriter();
  PrintWriter printWriter = new PrintWriter( out );

  PowerMockito.spy( Encode.class );
  when( mockHttpServletRequest.getContextPath() ).thenReturn( StartExecutionPipelineServlet.CONTEXT_PATH );
  when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST );
  when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );
  when( mockPipelineMap.getPipeline( any( HopServerObjectEntry.class ) ) ).thenReturn( mockPipeline );
  when( mockPipeline.getLogChannel() ).thenReturn( mockChannelInterface );
  when( mockPipeline.isReadyToStart() ).thenReturn( true );
  when( mockPipeline.getLogChannelId() ).thenReturn( "test" );
  when( mockPipeline.getPipelineMeta() ).thenReturn( mockPipelineMeta );
  when( mockPipelineMeta.getMaximum() ).thenReturn( new Point( 10, 10 ) );

  startExecutionPipelineServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );
  assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H1", out.toString() ) ) );

  PowerMockito.verifyStatic( atLeastOnce() );
  Encode.forHtml( anyString() );
}
 
Example #23
Source File: TransformWithMappingMetaTest.java    From hop with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest( TransformWithMappingMeta.class )
public void activateParamsWithFalsePassParametersFlagTest() throws Exception {
  String childParam = "childParam";
  String childValue = "childValue";
  String paramOverwrite = "paramOverwrite";
  String parentValue = "parentValue";
  String transformValue = "transformValue";
  String parentAndChildParameter = "parentAndChildParameter";

  IVariables parent = new Variables();
  parent.setVariable( paramOverwrite, parentValue );
  parent.setVariable( parentAndChildParameter, parentValue );

  PipelineMeta childVariableSpace = new PipelineMeta();
  childVariableSpace.addParameterDefinition( childParam, "", "" );
  childVariableSpace.setParameterValue( childParam, childValue );
  childVariableSpace.addParameterDefinition( parentAndChildParameter, "", "" );
  childVariableSpace.setParameterValue( parentAndChildParameter, childValue );

  String[] parameters = childVariableSpace.listParameters();
  TransformWithMappingMeta.activateParams( childVariableSpace, childVariableSpace, parent,
    parameters, new String[] { childParam, paramOverwrite }, new String[] { childValue, transformValue }, false );

  Assert.assertEquals( childValue, childVariableSpace.getVariable( childParam ) );
  // the transform parameter prevails
  Assert.assertEquals( transformValue, childVariableSpace.getVariable( paramOverwrite ) );

  Assert.assertEquals( childValue, childVariableSpace.getVariable( parentAndChildParameter ) );
}
 
Example #24
Source File: MongoDbStepsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@PrepareForTest(MongoClients.class)
@Test
public void testExecuteCommand()
{
    MongoDatabase database = mockDatabase();
    when(database.runCommand(COMMAND)).thenReturn(DOCUMENT);

    MongoDbSteps steps = new MongoDbSteps(Map.of(LOCAL_KEY, CONNECTION_KEY), jsonUtils, context);
    steps.executeCommand(COMMAND, LOCAL_KEY, LOCAL_KEY, Set.of(VariableScope.STORY), VARIABLE_KEY);

    verify(context).putVariable(Set.of(VariableScope.STORY), VARIABLE_KEY, Map.of("id", "1"));
}
 
Example #25
Source File: ClientBuilderUtilsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest({ CookieStoreCollector.class, ClientBuilderUtils.class })
public void testToCookieStore() throws Exception
{
    CookieStoreCollector cookieStoreCollector = new CookieStoreCollector();
    PowerMockito.whenNew(CookieStoreCollector.class).withNoArguments().thenReturn(cookieStoreCollector);
    Assertions.assertEquals(cookieStoreCollector, ClientBuilderUtils.toCookieStore());
}
 
Example #26
Source File: FileUtilsTest.java    From lsp4intellij with Apache License 2.0 5 votes vote down vote up
@PrepareForTest(LocalFileSystem.class)
@Test
public void testVirtualFileFromURI() {
  LocalFileSystem localFileSystem = PowerMockito.mock(LocalFileSystem.class);
  PowerMockito.mockStatic(LocalFileSystem.class);
  PowerMockito.when(LocalFileSystem.getInstance()).thenReturn(localFileSystem);
  PowerMockito.when(LocalFileSystem.getInstance().findFileByIoFile(Mockito.any()))
      .thenReturn(new BinaryLightVirtualFile("testFile"));

  Assert.assertEquals(new BinaryLightVirtualFile("testFile").toString(),
      FileUtils.virtualFileFromURI("file://foobar").toString());
}
 
Example #27
Source File: GenericDatabaseMetaTest.java    From hop with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest( DatabaseMeta.class )
public void testSettingDialect() {
  String dialect = "testDialect";
  IDatabase[] dbInterfaces = new IDatabase[] { mockedMeta };
  PowerMockito.mockStatic( DatabaseMeta.class );
  PowerMockito.when( DatabaseMeta.getDatabaseInterfaces() ).thenReturn( dbInterfaces );
  Mockito.when( mockedMeta.getPluginName() ).thenReturn( dialect );
  nativeMeta.addAttribute( "DATABASE_DIALECT_ID", dialect );
  assertEquals( mockedMeta, Whitebox.getInternalState( nativeMeta, "databaseDialect" ) );
}
 
Example #28
Source File: TransformWithMappingMetaTest.java    From hop with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest( TransformWithMappingMeta.class )
public void replaceVariablesWithJobInternalVariablesTest() {
  String variableOverwrite = "paramOverwrite";
  String variableChildOnly = "childValueVariable";
  String[] jobVariables = Const.INTERNAL_WORKFLOW_VARIABLES;
  IVariables ChildVariables = new Variables();
  IVariables replaceByParentVariables = new Variables();

  for ( String internalVariable : jobVariables ) {
    ChildVariables.setVariable( internalVariable, "childValue" );
    replaceByParentVariables.setVariable( internalVariable, "parentValue" );
  }

  ChildVariables.setVariable( variableChildOnly, "childValueVariable" );
  ChildVariables.setVariable( variableOverwrite, "childNotInternalValue" );
  replaceByParentVariables.setVariable( variableOverwrite, "parentNotInternalValue" );

  TransformWithMappingMeta.replaceVariableValues( ChildVariables, replaceByParentVariables );
  // do not replace internal variables
  Assert.assertEquals( "childValue", ChildVariables.getVariable( Const.INTERNAL_VARIABLE_ENTRY_CURRENT_DIRECTORY ) );
  // replace non internal variables
  Assert.assertEquals( "parentNotInternalValue", ChildVariables.getVariable( variableOverwrite ) );
  // keep child only variables
  Assert.assertEquals( variableChildOnly, ChildVariables.getVariable( variableChildOnly ) );

}
 
Example #29
Source File: RemovePipelineServletTest.java    From hop with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest( { Encode.class } )
public void testRemovePipelineServletEscapesHtmlWhenPipelineFound() throws ServletException, IOException {
  HopLogStore.init();
  HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
  HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );
  Pipeline mockPipeline = mock( Pipeline.class );
  PipelineMeta mockPipelineMeta = mock( PipelineMeta.class );
  ILogChannel mockChannelInterface = mock( ILogChannel.class );
  StringWriter out = new StringWriter();
  PrintWriter printWriter = new PrintWriter( out );

  PowerMockito.spy( Encode.class );
  when( mockHttpServletRequest.getContextPath() ).thenReturn( RemovePipelineServlet.CONTEXT_PATH );
  when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST );
  when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );
  when( mockPipelineMap.getPipeline( any( HopServerObjectEntry.class ) ) ).thenReturn( mockPipeline );
  when( mockPipeline.getLogChannel() ).thenReturn( mockChannelInterface );
  when( mockPipeline.getLogChannelId() ).thenReturn( "test" );
  when( mockPipeline.getPipelineMeta() ).thenReturn( mockPipelineMeta );
  when( mockPipelineMeta.getMaximum() ).thenReturn( new Point( 10, 10 ) );

  removePipelineServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );
  assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H3", out.toString() ) ) );

  PowerMockito.verifyStatic( atLeastOnce() );
  Encode.forHtml( anyString() );
}
 
Example #30
Source File: TransformWithMappingMetaTest.java    From hop with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest( TransformWithMappingMeta.class )
public void replaceVariablesWithPipelineInternalVariablesTest() {
  String variableOverwrite = "paramOverwrite";
  String variableChildOnly = "childValueVariable";
  String[] jobVariables = Const.INTERNAL_PIPELINE_VARIABLES;
  IVariables ChildVariables = new Variables();
  IVariables replaceByParentVariables = new Variables();

  for ( String internalVariable : jobVariables ) {
    ChildVariables.setVariable( internalVariable, "childValue" );
    replaceByParentVariables.setVariable( internalVariable, "parentValue" );
  }

  ChildVariables.setVariable( variableChildOnly, "childValueVariable" );
  ChildVariables.setVariable( variableOverwrite, "childNotInternalValue" );
  replaceByParentVariables.setVariable( variableOverwrite, "parentNotInternalValue" );

  TransformWithMappingMeta.replaceVariableValues( ChildVariables, replaceByParentVariables );
  // do not replace internal variables
  Assert.assertEquals( "childValue", ChildVariables.getVariable( Const.INTERNAL_VARIABLE_ENTRY_CURRENT_DIRECTORY ) );
  // replace non internal variables
  Assert.assertEquals( "parentNotInternalValue", ChildVariables.getVariable( variableOverwrite ) );
  // keep child only variables
  Assert.assertEquals( variableChildOnly, ChildVariables.getVariable( variableChildOnly ) );

}