/*
 * This program is part of Autonomiccs "autonomic-platform",
 * an open source autonomic cloud computing management platform.
 * Copyright (C) 2016 Autonomiccs, Inc.
 *
 * Licensed to the Autonomiccs, Inc. under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The Autonomiccs, Inc. licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package br.com.autonomiccs.autonomic.plugin.common.utils;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.slf4j.Logger;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ShellCommandUtils.class)
public class ShellCommandUtilsTest {

    private ShellCommandUtils shellCommandUtils;
    private String command = "test";

    @Before
    public void beforeTest(){
        shellCommandUtils = new ShellCommandUtils();
    }

    @Test
    public void executeCommandTest() throws IOException, InterruptedException{
        Process processMock = Mockito.mock(Process.class);
        Runtime runtimeMock = configureAndGetRuntimeMock();

        String commandOutput = "TEST";
        InputStream input = IOUtils.toInputStream(commandOutput, "utf-8");

        Mockito.when(runtimeMock.exec(command)).thenReturn(processMock);
        Mockito.when(processMock.waitFor()).thenReturn(1);
        Mockito.when(processMock.getInputStream()).thenReturn(input);

        String response = shellCommandUtils.executeCommand(command);
        Assert.assertEquals(commandOutput, response);
        Mockito.verify(processMock).waitFor();
    }

    @Test
    @SuppressWarnings("unchecked")
    public void executeCommandTestDealingWithIoException() throws IOException, InterruptedException {
        shellCommandUtils.logger = Mockito.mock(Logger.class);

        Runtime runtimeMock = configureAndGetRuntimeMock();
        Mockito.when(runtimeMock.exec(command)).thenThrow(IOException.class);

        String commandOutput = shellCommandUtils.executeCommand(command);

        executeChecksForExceptionTests(commandOutput);
    }

    @Test
    @SuppressWarnings("unchecked")
    public void executeCommandTestDealingWithInterruptedException() throws IOException, InterruptedException {
        shellCommandUtils.logger = Mockito.mock(Logger.class);

        Process processMock = Mockito.mock(Process.class);
        Runtime runtimeMock = configureAndGetRuntimeMock();
        Mockito.when(runtimeMock.exec(command)).thenReturn(processMock);
        Mockito.when(processMock.waitFor()).thenThrow(InterruptedException.class);

        String commandOutput = shellCommandUtils.executeCommand(command);

        Mockito.verify(processMock).waitFor();
        executeChecksForExceptionTests(commandOutput);
    }

    private void executeChecksForExceptionTests(String commandOutput) {
        Mockito.verify(shellCommandUtils.logger).error(Mockito.anyString());
        Assert.assertEquals("", commandOutput);
    }

    private Runtime configureAndGetRuntimeMock() {
        Runtime runtimeMock = Mockito.mock(Runtime.class);
        PowerMockito.mockStatic(Runtime.class);

        PowerMockito.when(Runtime.getRuntime()).thenReturn(runtimeMock);
        return runtimeMock;
    }
}
@@ -2922,32 +2922,51 @@
 ts(commandOutput
+, IOException.class
 );%0A    %7D%0A%0A    @T
@@ -3621,32 +3621,60 @@
 ts(commandOutput
+, InterruptedException.class
 );%0A    %7D%0A%0A    pr
@@ -3735,16 +3735,62 @@
 ndOutput
+, Class%3C? extends Exception%3E expectedException
 ) %7B%0A    
@@ -3852,18 +3852,122 @@
 ito.
-anyString(
+startsWith(String.format(%22An error happened while executing command%5B%25s%5D%22, command)), Mockito.any(expectedException
 ));%0A

Fix test cases that were broken due to a code change