[ZEPPELIN-3627] Remove starting server option for some functional tests
### What is this PR for?
Removing `AbstractTestRestApi` dependency to reduce testing time
### What type of PR is it?
[Bug Fix | Improvement | Feature | Documentation | Hot Fix | Refactoring]
### Todos
* [x] - Remove `AbstractTestRestApi`
### What is the Jira issue?
* https://issues.apache.org/jira/browse/ZEPPELIN-3627
### Questions:
* Does the licenses files need update? No
* Is there breaking changes for older versions? No
* Does this needs documentation? No
Author: Jongyoul Lee <jongyoul@gmail.com>
Closes #3076 from jongyoul/ZEPPELIN-3627 and squashes the following commits:
505580062 [Jongyoul Lee] Revert commented flaky tests
ac7d827b4 [Jongyoul Lee] Change flaky test
0ab6d66b0 [Jongyoul Lee] Disable flaky tests
e99522dde [Jongyoul Lee] Remove `AbstractRestApiTest` from `CredentialRestApiTest`
diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/rest/CredentialRestApi.java b/zeppelin-server/src/main/java/org/apache/zeppelin/rest/CredentialRestApi.java
index 10ca50f..9765b8f 100755
--- a/zeppelin-server/src/main/java/org/apache/zeppelin/rest/CredentialRestApi.java
+++ b/zeppelin-server/src/main/java/org/apache/zeppelin/rest/CredentialRestApi.java
@@ -14,38 +14,31 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.zeppelin.rest;
import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import java.io.IOException;
import java.util.Map;
-
-import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
-import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
-
import org.apache.zeppelin.server.JsonResponse;
import org.apache.zeppelin.user.Credentials;
import org.apache.zeppelin.user.UserCredentials;
import org.apache.zeppelin.user.UsernamePassword;
import org.apache.zeppelin.utils.SecurityUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-/**
- * Credential Rest API.
- */
+/** Credential Rest API. */
@Path("/credential")
@Produces("application/json")
public class CredentialRestApi {
@@ -53,12 +46,6 @@
private Credentials credentials;
private Gson gson = new Gson();
- @Context
- private HttpServletRequest servReq;
-
- public CredentialRestApi() {
- }
-
public CredentialRestApi(Credentials credentials) {
this.credentials = credentials;
}
@@ -73,14 +60,15 @@
*/
@PUT
public Response putCredentials(String message) throws IOException, IllegalArgumentException {
- Map<String, String> messageMap = gson.fromJson(message,
- new TypeToken<Map<String, String>>(){}.getType());
+ Map<String, String> messageMap =
+ gson.fromJson(message, new TypeToken<Map<String, String>>() {}.getType());
String entity = messageMap.get("entity");
String username = messageMap.get("username");
String password = messageMap.get("password");
if (Strings.isNullOrEmpty(entity)
- || Strings.isNullOrEmpty(username) || Strings.isNullOrEmpty(password)) {
+ || Strings.isNullOrEmpty(username)
+ || Strings.isNullOrEmpty(password)) {
return new JsonResponse(Status.BAD_REQUEST).build();
}
@@ -95,31 +83,26 @@
/**
* Get User Credentials list REST API.
*
- * @param
* @return JSON with status.OK
- * @throws IOException
* @throws IllegalArgumentException
*/
@GET
- public Response getCredentials(String message) throws
- IOException, IllegalArgumentException {
+ public Response getCredentials() throws IllegalArgumentException {
String user = SecurityUtils.getPrincipal();
logger.info("getCredentials credentials for user {} ", user);
UserCredentials uc = credentials.getUserCredentials(user);
- return new JsonResponse(Status.OK, uc).build();
+ return new JsonResponse<>(Status.OK, uc).build();
}
/**
* Remove User Credentials REST API.
*
- * @param
* @return JSON with status.OK
* @throws IOException
* @throws IllegalArgumentException
*/
@DELETE
- public Response removeCredentials(String message) throws
- IOException, IllegalArgumentException {
+ public Response removeCredentials() throws IOException, IllegalArgumentException {
String user = SecurityUtils.getPrincipal();
logger.info("removeCredentials credentials for user {} ", user);
UserCredentials uc = credentials.removeUserCredentials(user);
@@ -139,11 +122,11 @@
*/
@DELETE
@Path("{entity}")
- public Response removeCredentialEntity(@PathParam("entity") String entity) throws
- IOException, IllegalArgumentException {
+ public Response removeCredentialEntity(@PathParam("entity") String entity)
+ throws IOException, IllegalArgumentException {
String user = SecurityUtils.getPrincipal();
logger.info("removeCredentialEntity for user {} entity {}", user, entity);
- if (credentials.removeCredentialEntity(user, entity) == false) {
+ if (!credentials.removeCredentialEntity(user, entity)) {
return new JsonResponse(Status.NOT_FOUND).build();
}
return new JsonResponse(Status.OK).build();
diff --git a/zeppelin-server/src/test/java/org/apache/zeppelin/rest/CredentialsRestApiTest.java b/zeppelin-server/src/test/java/org/apache/zeppelin/rest/CredentialsRestApiTest.java
index 2225c39..3e61d11 100644
--- a/zeppelin-server/src/test/java/org/apache/zeppelin/rest/CredentialsRestApiTest.java
+++ b/zeppelin-server/src/test/java/org/apache/zeppelin/rest/CredentialsRestApiTest.java
@@ -19,119 +19,80 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
-
-import org.apache.commons.httpclient.methods.DeleteMethod;
-import org.apache.commons.httpclient.methods.GetMethod;
-import org.apache.commons.httpclient.methods.PutMethod;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import java.io.IOException;
+import java.nio.file.Files;
import java.util.Map;
-
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import org.apache.zeppelin.user.Credentials;
import org.apache.zeppelin.user.UserCredentials;
+import org.junit.Before;
+import org.junit.Test;
-public class CredentialsRestApiTest extends AbstractTestRestApi {
- protected static final Logger LOG = LoggerFactory.getLogger(CredentialsRestApiTest.class);
- Gson gson = new Gson();
+public class CredentialsRestApiTest {
+ private final Gson gson = new Gson();
- @BeforeClass
- public static void init() throws Exception {
- AbstractTestRestApi.startUp(CredentialsRestApiTest.class.getSimpleName());
- }
+ private CredentialRestApi credentialRestApi;
+ private Credentials credentials;
- @AfterClass
- public static void destroy() throws Exception {
- AbstractTestRestApi.shutDown();
+ @Before
+ public void setUp() throws IOException {
+ credentials =
+ new Credentials(false, Files.createTempFile("credentials", "test").toString(), null);
+ credentialRestApi = new CredentialRestApi(credentials);
}
@Test
public void testInvalidRequest() throws IOException {
- String jsonInvalidRequestEntityNull = "{\"entity\" : null, \"username\" : \"test\", " +
- "\"password\" : \"testpass\"}";
- String jsonInvalidRequestNameNull = "{\"entity\" : \"test\", \"username\" : null, " +
- "\"password\" : \"testpass\"}";
- String jsonInvalidRequestPasswordNull = "{\"entity\" : \"test\", \"username\" : \"test\", " +
- "\"password\" : null}";
- String jsonInvalidRequestAllNull = "{\"entity\" : null, \"username\" : null, " +
- "\"password\" : null}";
+ String jsonInvalidRequestEntityNull =
+ "{\"entity\" : null, \"username\" : \"test\", " + "\"password\" : \"testpass\"}";
+ String jsonInvalidRequestNameNull =
+ "{\"entity\" : \"test\", \"username\" : null, " + "\"password\" : \"testpass\"}";
+ String jsonInvalidRequestPasswordNull =
+ "{\"entity\" : \"test\", \"username\" : \"test\", " + "\"password\" : null}";
+ String jsonInvalidRequestAllNull =
+ "{\"entity\" : null, \"username\" : null, " + "\"password\" : null}";
- PutMethod entityNullPut = httpPut("/credential", jsonInvalidRequestEntityNull);
- entityNullPut.addRequestHeader("Origin", "http://localhost");
- assertThat(entityNullPut, isBadRequest());
- entityNullPut.releaseConnection();
+ Response response = credentialRestApi.putCredentials(jsonInvalidRequestEntityNull);
+ assertEquals(Status.BAD_REQUEST, response.getStatusInfo().toEnum());
- PutMethod nameNullPut = httpPut("/credential", jsonInvalidRequestNameNull);
- nameNullPut.addRequestHeader("Origin", "http://localhost");
- assertThat(nameNullPut, isBadRequest());
- nameNullPut.releaseConnection();
+ response = credentialRestApi.putCredentials(jsonInvalidRequestNameNull);
+ assertEquals(Status.BAD_REQUEST, response.getStatusInfo().toEnum());
- PutMethod passwordNullPut = httpPut("/credential", jsonInvalidRequestPasswordNull);
- passwordNullPut.addRequestHeader("Origin", "http://localhost");
- assertThat(passwordNullPut, isBadRequest());
- passwordNullPut.releaseConnection();
+ response = credentialRestApi.putCredentials(jsonInvalidRequestPasswordNull);
+ assertEquals(Status.BAD_REQUEST, response.getStatusInfo().toEnum());
- PutMethod allNullPut = httpPut("/credential", jsonInvalidRequestAllNull);
- allNullPut.addRequestHeader("Origin", "http://localhost");
- assertThat(allNullPut, isBadRequest());
- allNullPut.releaseConnection();
+ response = credentialRestApi.putCredentials(jsonInvalidRequestAllNull);
+ assertEquals(Status.BAD_REQUEST, response.getStatusInfo().toEnum());
}
public Map<String, UserCredentials> testGetUserCredentials() throws IOException {
- GetMethod getMethod = httpGet("/credential");
- getMethod.addRequestHeader("Origin", "http://localhost");
- Map<String, Object> resp = gson.fromJson(getMethod.getResponseBodyAsString(),
- new TypeToken<Map<String, Object>>(){}.getType());
+ Response response = credentialRestApi.getCredentials();
+ Map<String, Object> resp =
+ gson.fromJson(
+ response.getEntity().toString(), new TypeToken<Map<String, Object>>() {}.getType());
Map<String, Object> body = (Map<String, Object>) resp.get("body");
Map<String, UserCredentials> credentialMap =
- (Map<String, UserCredentials>) body.get("userCredentials");
- getMethod.releaseConnection();
+ (Map<String, UserCredentials>) body.get("userCredentials");
return credentialMap;
}
- public void testPutUserCredentials(String requestData) throws IOException {
- PutMethod putMethod = httpPut("/credential", requestData);
- putMethod.addRequestHeader("Origin", "http://localhost");
- assertThat(putMethod, isAllowed());
- putMethod.releaseConnection();
- }
-
- public void testRemoveUserCredentials() throws IOException {
- DeleteMethod deleteMethod = httpDelete("/credential/");
- assertThat("Test delete method:", deleteMethod, isAllowed());
- deleteMethod.releaseConnection();
- }
-
- public void testRemoveCredentialEntity(String entity) throws IOException {
- DeleteMethod deleteMethod = httpDelete("/credential/" + entity);
- assertThat("Test delete method:", deleteMethod, isAllowed());
- deleteMethod.releaseConnection();
- }
-
@Test
public void testCredentialsAPIs() throws IOException {
- String requestData1 = "{\"entity\" : \"entityname\", \"username\" : \"myuser\", \"password\" " +
- ": \"mypass\"}";
+ String requestData1 =
+ "{\"entity\" : \"entityname\", \"username\" : \"myuser\", \"password\" " + ": \"mypass\"}";
String entity = "entityname";
- Map<String, UserCredentials> credentialMap;
- testPutUserCredentials(requestData1);
- credentialMap = testGetUserCredentials();
- assertNotNull("CredentialMap should be null", credentialMap);
+ credentialRestApi.putCredentials(requestData1);
+ assertNotNull("CredentialMap should be null", testGetUserCredentials());
- testRemoveCredentialEntity(entity);
- credentialMap = testGetUserCredentials();
- assertNull("CredentialMap should be null", credentialMap.get("entity1"));
+ credentialRestApi.removeCredentialEntity(entity);
+ assertNull("CredentialMap should be null", testGetUserCredentials().get("entity1"));
- testRemoveUserCredentials();
- credentialMap = testGetUserCredentials();
- assertEquals("Compare CredentialMap", credentialMap.toString(), "{}");
+ credentialRestApi.removeCredentials();
+ assertEquals("Compare CredentialMap", testGetUserCredentials().toString(), "{}");
}
}