cs-database/src/test/java/io/cloudslang/content/database/actions/SQLQueryAllRowsTest.java
/*
* Copyright 2019-2024 Open Text
* This program and the accompanying materials
* are made available under the terms of the Apache License v2.0 which accompany this distribution.
*
* The Apache License is available 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 io.cloudslang.content.database.actions;
import io.cloudslang.content.database.services.SQLQueryAllRowsService;
import io.cloudslang.content.database.utils.SQLInputs;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Spy;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.Map;
import static io.cloudslang.content.constants.OutputNames.RETURN_CODE;
import static io.cloudslang.content.constants.OutputNames.RETURN_RESULT;
import static io.cloudslang.content.constants.ReturnCodes.FAILURE;
import static io.cloudslang.content.constants.ReturnCodes.SUCCESS;
import static io.cloudslang.content.database.constants.DBDefaultValues.AUTH_SQL;
import static io.cloudslang.content.database.constants.DBOtherValues.*;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* Created by victor on 13.02.2017.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({SQLQueryAllRows.class, SQLQueryAllRowsService.class})
public class SQLQueryAllRowsTest {
@Spy
private final SQLQueryAllRows sqlQueryAllRows = new SQLQueryAllRows();
@Test
public void execute() throws Exception {
final Map<String, String> resultMap = new SQLQueryAllRows().execute(EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY);
assertThat(resultMap.get(RETURN_CODE), is(FAILURE));
assertThat(resultMap.get(RETURN_RESULT), is("dbServerName can't be empty\n" +
"username input is empty.\n" +
"password input is empty.\n" +
"database input is empty.\n" +
"trustStore or trustStorePassword is mandatory if trustAllRoots is false\n" +
"command input is empty."));
}
@Test
public void executeSuccess() throws Exception {
final String res = "result";
PowerMockito.mockStatic(SQLQueryAllRowsService.class);
when(SQLQueryAllRowsService.execQueryAllRows(any(SQLInputs.class))).thenReturn(res);
final Map<String, String> resultMap = sqlQueryAllRows.execute("1", MSSQL_DB_TYPE, "username", "Password", "someInstance", "123", "db",
AUTH_SQL, EMPTY, EMPTY, "something", "true", EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, TYPE_FORWARD_ONLY, CONCUR_READ_ONLY);
verifyStatic();
assertThat(resultMap.get(RETURN_CODE), is(SUCCESS));
assertThat(resultMap.get(RETURN_RESULT), is(res));
}
}