5
0
mirror of https://github.com/apache/sqoop.git synced 2025-05-08 09:20:36 +08:00

SQOOP-2698: Sqoop2: RESTiliency: Split the InvalidRESTCallsTest into independent test cases

(Jarek Jarcec Cecho via Kate Ting)
This commit is contained in:
Kate Ting 2015-11-20 14:26:21 -08:00
parent 30fe4da2cd
commit cde1c3ac9e
4 changed files with 221 additions and 112 deletions

View File

@ -0,0 +1,73 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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 org.apache.sqoop.integration.server.rest;
import org.apache.sqoop.test.utils.ParametrizedUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import java.util.Iterator;
public class ConnectorRestTest extends RestTest {
public static TestDescription[] PROVIDER_DATA = new TestDescription[]{
new TestDescription("Get all connectors", "v1/connector/all", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(200);
}}),
new TestDescription("Get connector by ID", "v1/connector/1", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(200);
}}),
new TestDescription("Get connector by name", "v1/connector/generic-jdbc-connector", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(200);
}}),
new TestDescription("Get connector by non-existing ID", "v1/connector/666", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(500);
assertServerException("org.apache.sqoop.server.common.ServerError", "SERVER_0006");
}}),
new TestDescription("Get connector by non-existing name", "v1/connector/jarcecs-cool-connector", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(500);
assertServerException("org.apache.sqoop.server.common.ServerError", "SERVER_0005");
}}),
new TestDescription("Invalid post request", "v1/connector", "POST", "Random data", new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(500);
assertServerException("org.apache.sqoop.server.common.ServerError", "SERVER_0002");
}}),
};
@DataProvider(name="connector-rest-test", parallel=false)
public static Iterator<Object[]> data() {
return ParametrizedUtils.toArrayOfArrays(PROVIDER_DATA).iterator();
}
@Factory(dataProvider = "connector-rest-test")
public ConnectorRestTest(TestDescription desc) {
super(desc);
}
}

View File

@ -0,0 +1,79 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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 org.apache.sqoop.integration.server.rest;
import org.apache.sqoop.test.utils.ParametrizedUtils;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import java.util.Iterator;
public class LinkRestTest extends RestTest {
@BeforeMethod
public void setUp() {
createFirstLink();
}
public static TestDescription[] PROVIDER_DATA = new TestDescription[]{
// Get
new TestDescription("Get all links", "v1/link/all", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(200);
assertContains("first-link");
}}),
new TestDescription("Get link by name", "v1/link/first-link", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(200);
assertContains("first-link");
}}),
new TestDescription("Get all links for connector", "v1/link/all?cname=generic-jdbc-connector", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(200);
assertContains("first-link");
}}),
new TestDescription("Get non existing link", "v1/link/i-dont-exists", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(500);
assertServerException("org.apache.sqoop.server.common.ServerError", "SERVER_0006");
assertContains("Invalid link name: i-dont-exists");
}}),
new TestDescription("Get links for non existing connector", "v1/link/all?cname=i-dont-exists", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(500);
assertServerException("org.apache.sqoop.server.common.ServerError", "SERVER_0005");
assertContains("Invalid connector: i-dont-exists");
}}),
};
@DataProvider(name="link-rest-test", parallel=false)
public static Iterator<Object[]> data() {
return ParametrizedUtils.toArrayOfArrays(PROVIDER_DATA).iterator();
}
@Factory(dataProvider = "link-rest-test")
public LinkRestTest(TestDescription desc) {
super(desc);
}
}

View File

@ -15,23 +15,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.sqoop.integration.server;
package org.apache.sqoop.integration.server.rest;
import org.apache.log4j.Logger;
import com.google.common.collect.Iterables;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL;
import org.apache.sqoop.common.SqoopException;
import org.apache.sqoop.model.MConfigList;
import org.apache.sqoop.model.MJob;
import org.apache.sqoop.model.MLink;
import org.apache.sqoop.test.infrastructure.Infrastructure;
import org.apache.sqoop.test.infrastructure.SqoopTestCase;
import org.apache.sqoop.test.infrastructure.providers.HadoopInfrastructureProvider;
import org.apache.sqoop.test.infrastructure.providers.SqoopInfrastructureProvider;
import org.apache.sqoop.test.utils.ParametrizedUtils;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import java.io.DataOutputStream;
@ -44,9 +42,9 @@
import static org.testng.Assert.assertTrue;
@Infrastructure(dependencies = {HadoopInfrastructureProvider.class, SqoopInfrastructureProvider.class})
public class InvalidRESTCallsTest extends SqoopTestCase {
public abstract class RestTest extends SqoopTestCase {
private static final Logger LOG = Logger.getLogger(InvalidRESTCallsTest.class);
private static final Logger LOG = Logger.getLogger(RestTest.class);
// Validate returned response from server
public static abstract class Validator {
@ -65,7 +63,7 @@ public void setConnection(HttpURLConnection connection) throws Exception {
try {
this.input = (connection.getInputStream() != null) ? IOUtils.toString(connection.getInputStream()) : "";
} catch(Exception e) {
// We're ignoring exception here because that means that request wasn't successful and data are in "error" stream
// We're ignoring exception here because that means that request wasn't successful and response is in "error" stream instead
}
this.error = connection.getErrorStream() != null ? IOUtils.toString(connection.getErrorStream()) : "";
}
@ -119,10 +117,9 @@ public TestDescription(String name, String rest, String method, String data, Val
}
/**
* Data preparation for links, jobs and other objects that we might need.
* Various objects that can be pre-created by child test cases
*/
@BeforeMethod
public void loadTestData() {
public void createFirstLink() {
// Link: first-link
MLink genericJDBCLink = getClient().createLink("generic-jdbc-connector");
genericJDBCLink.setName("first-link");
@ -136,114 +133,23 @@ public void loadTestData() {
@AfterMethod
public void dropTestData() {
getClient().deleteLink("first-link");
}
/**
* Correct and poisoned requests that we'll be running with expected responses from the server
*/
public static TestDescription []PROVDER_DATA = new TestDescription[] {
// End point /version/
new TestDescription("Valid", "version", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(200);
}}),
new TestDescription("Invalid post request", "version", "POST", "Random text", new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(500);
assertServerException("org.apache.sqoop.server.common.ServerError", "SERVER_0002");
}}),
// End point /v1/connector
new TestDescription("Get all connectors", "v1/connector/all", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(200);
}}),
new TestDescription("Get connector by name", "v1/connector/generic-jdbc-connector", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(200);
}}),
new TestDescription("Get connector by ID", "v1/connector/1", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(200);
}}),
new TestDescription("Get connector by non-existing ID", "v1/connector/666", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(500);
assertServerException("org.apache.sqoop.server.common.ServerError", "SERVER_0006");
}}),
new TestDescription("Get connector by non-existing name", "v1/connector/jarcecs-cool-connector", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(500);
assertServerException("org.apache.sqoop.server.common.ServerError", "SERVER_0006");
}}),
new TestDescription("Invalid post request", "v1/connector", "POST", "Random data", new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(500);
assertServerException("org.apache.sqoop.server.common.ServerError", "SERVER_0002");
}}),
// End point /v1/link
// Get
new TestDescription("Get all links", "v1/link/all", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(200);
assertContains("first-link");
}}),
new TestDescription("Get link by name", "v1/link/first-link", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(200);
assertContains("first-link");
}}),
new TestDescription("Get all links for connector", "v1/link/all?cname=generic-jdbc-connector", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(200);
assertContains("first-link");
}}),
new TestDescription("Get non existing link", "v1/link/i-dont-exists", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(500);
assertServerException("org.apache.sqoop.server.common.ServerError", "SERVER_0006");
assertContains("Invalid link name: i-dont-exists");
}}),
new TestDescription("Get links for non existing connector", "v1/link/all?cname=i-dont-exists", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(500);
assertServerException("org.apache.sqoop.server.common.ServerError", "SERVER_0005");
assertContains("Invalid connector: i-dont-exists");
}}),
};
@DataProvider(name="invalid-rest-calls-test", parallel=false)
public static Object[][] data() {
return Iterables.toArray(ParametrizedUtils.toArrayOfArrays(PROVDER_DATA), Object[].class);
for(MJob job : getClient().getJobs()) {
getClient().deleteJob(job.getName());
}
for(MLink link : getClient().getLinks()) {
getClient().deleteLink(link.getName());
}
}
private TestDescription desc;
@Factory(dataProvider = "invalid-rest-calls-test")
public InvalidRESTCallsTest(TestDescription desc) {
public RestTest(TestDescription desc) {
this.desc = desc;
}
@Override
public String getTestName() {
return InvalidRESTCallsTest.class.getName() + " " + desc.rest + "[" + desc.method + "]: " + desc.name;
return getClass().getName() + " " + desc.rest + "[" + desc.method + "]: " + desc.name;
}
@Test
@ -257,10 +163,10 @@ public void test() throws Exception {
if(desc.data != null) {
connection.setDoOutput(true);
byte []byteData = desc.data.getBytes(Charset.forName("UTF-8"));
byte[] byteData = desc.data.getBytes(Charset.forName("UTF-8"));
connection.setRequestProperty("Content-Length", Integer.toString(byteData.length));
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.write(byteData, byteData.length, 0);
wr.write(byteData, 0, byteData.length);
wr.flush();
wr.close();
}

View File

@ -0,0 +1,51 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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 org.apache.sqoop.integration.server.rest;
import org.apache.sqoop.test.utils.ParametrizedUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import java.util.Iterator;
public class VersionRestTest extends RestTest {
public static TestDescription[] PROVIDER_DATA = new TestDescription[]{
new TestDescription("Valid", "version", "GET", null, new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(200);
}}),
new TestDescription("Invalid post request", "version", "POST", "Random text", new Validator() {
@Override
void validate() throws Exception {
assertResponseCode(500);
assertServerException("org.apache.sqoop.server.common.ServerError", "SERVER_0002");
}}),
};
@DataProvider(name="version-rest-test", parallel=false)
public static Iterator<Object[]> data() {
return ParametrizedUtils.toArrayOfArrays(PROVIDER_DATA).iterator();
}
@Factory(dataProvider = "version-rest-test")
public VersionRestTest(TestDescription desc) {
super(desc);
}
}