Skip to content

Commit

Permalink
HADOOP-15984. Improve Some Code.
Browse files Browse the repository at this point in the history
  • Loading branch information
slfan1989 committed Jan 5, 2025
1 parent fff1b6a commit 3237736
Show file tree
Hide file tree
Showing 31 changed files with 932 additions and 1,200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.io.StringReader;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.ServiceUnavailableException;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
Expand All @@ -48,15 +50,16 @@
import org.apache.hadoop.yarn.webapp.WebServicesTestUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.glassfish.jersey.internal.inject.AbstractBinder;
import org.glassfish.jersey.jettison.JettisonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import org.glassfish.jersey.internal.inject.AbstractBinder;
import org.glassfish.jersey.jettison.JettisonFeature;
import org.glassfish.jersey.server.ResourceConfig;

/**
* Test the History Server info web services api's. Also test non-existent urls.
*
Expand Down Expand Up @@ -96,46 +99,43 @@ protected void configure() {
}

@Test
public void testHS() throws Exception {
WebTarget r = target();
public void testHS() throws JSONException, Exception {
WebTarget r = targetWithJsonObject();
Response response = r.path("ws").path("v1").path("history")
.request(MediaType.APPLICATION_JSON).get(Response.class);
assertEquals(MediaType.APPLICATION_JSON + ";" + JettyUtils.UTF_8,
response.getMediaType().toString());
String entity = response.readEntity(String.class);
JSONObject json = new JSONObject(entity);
JSONObject json = response.readEntity(JSONObject.class);
assertEquals("incorrect number of elements", 1, json.length());
verifyHSInfo(json.getJSONObject("historyInfo"));
}

@Test
public void testHSSlash() throws Exception {
WebTarget r = target();
public void testHSSlash() throws JSONException, Exception {
WebTarget r = targetWithJsonObject();
Response response = r.path("ws").path("v1").path("history/")
.request(MediaType.APPLICATION_JSON).get(Response.class);
assertEquals(MediaType.APPLICATION_JSON + ";" + JettyUtils.UTF_8,
response.getMediaType().toString());
String entity = response.readEntity(String.class);
JSONObject json = new JSONObject(entity);
JSONObject json = response.readEntity(JSONObject.class);
assertEquals("incorrect number of elements", 1, json.length());
verifyHSInfo(json.getJSONObject("historyInfo"));
}

@Test
public void testHSDefault() throws Exception {
WebTarget r = target();
public void testHSDefault() throws JSONException, Exception {
WebTarget r = targetWithJsonObject();
Response response = r.path("ws").path("v1").path("history/")
.request(MediaType.APPLICATION_JSON).get(Response.class);
assertEquals(MediaType.APPLICATION_JSON + ";" + JettyUtils.UTF_8,
response.getMediaType().toString());
String entity = response.readEntity(String.class);
JSONObject json = new JSONObject(entity);
JSONObject json = response.readEntity(JSONObject.class);
assertEquals("incorrect number of elements", 1, json.length());
verifyHSInfo(json.getJSONObject("historyInfo"));
}

@Test
public void testHSXML() throws Exception {
public void testHSXML() throws JSONException, Exception {
WebTarget r = target();
Response response = r.path("ws").path("v1").path("history")
.request(MediaType.APPLICATION_XML).get(Response.class);
Expand All @@ -146,48 +146,45 @@ public void testHSXML() throws Exception {
}

@Test
public void testInfo() throws Exception {
WebTarget r = target();
public void testInfo() throws JSONException, Exception {
WebTarget r = targetWithJsonObject();
Response response = r.path("ws").path("v1").path("history")
.path("info").request(MediaType.APPLICATION_JSON)
.get(Response.class);
assertEquals(MediaType.APPLICATION_JSON + ";" + JettyUtils.UTF_8,
response.getMediaType().toString());
String entity = response.readEntity(String.class);
JSONObject json = new JSONObject(entity);
JSONObject json = response.readEntity(JSONObject.class);
assertEquals("incorrect number of elements", 1, json.length());
verifyHSInfo(json.getJSONObject("historyInfo"));
}

@Test
public void testInfoSlash() throws Exception {
WebTarget r = target();
public void testInfoSlash() throws JSONException, Exception {
WebTarget r = targetWithJsonObject();
Response response = r.path("ws").path("v1").path("history")
.path("info/").request(MediaType.APPLICATION_JSON)
.get(Response.class);
assertEquals(MediaType.APPLICATION_JSON + ";" + JettyUtils.UTF_8,
response.getMediaType().toString());
String entity = response.readEntity(String.class);
JSONObject json = new JSONObject(entity);
JSONObject json = response.readEntity(JSONObject.class);
assertEquals("incorrect number of elements", 1, json.length());
verifyHSInfo(json.getJSONObject("historyInfo"));
}

@Test
public void testInfoDefault() throws Exception {
WebTarget r = target();
public void testInfoDefault() throws JSONException, Exception {
WebTarget r = targetWithJsonObject();
Response response = r.path("ws").path("v1").path("history")
.path("info/").request().get(Response.class);
assertEquals(MediaType.APPLICATION_JSON + ";" + JettyUtils.UTF_8,
response.getMediaType().toString());
String entity = response.readEntity(String.class);
JSONObject json = new JSONObject(entity);
JSONObject json = response.readEntity(JSONObject.class);
assertEquals("incorrect number of elements", 1, json.length());
verifyHSInfo(json.getJSONObject("historyInfo"));
}

@Test
public void testInfoXML() throws Exception {
public void testInfoXML() throws JSONException, Exception {
WebTarget r = target();
Response response = r.path("ws").path("v1").path("history")
.path("info/").request(MediaType.APPLICATION_XML)
Expand All @@ -199,37 +196,52 @@ public void testInfoXML() throws Exception {
}

@Test
public void testInvalidUri() {
public void testInvalidUri() throws JSONException, Exception {
WebTarget r = target();
String responseStr = "";
Response response = r.path("ws").path("v1").path("history").path("bogus")
.request(MediaType.APPLICATION_JSON).get();
assertResponseStatusCode(Response.Status.NOT_FOUND, response.getStatusInfo());
WebServicesTestUtils.checkStringMatch(
"error string exists and shouldn't", "", responseStr);
try {
Response response = r.path("ws").path("v1").path("history").path("bogus")
.request(MediaType.APPLICATION_JSON).get();
throw new NotFoundException(response);
} catch (NotFoundException ue) {
Response response = ue.getResponse();
assertResponseStatusCode(Response.Status.NOT_FOUND, response.getStatusInfo());
WebServicesTestUtils.checkStringMatch(
"error string exists and shouldn't", "", responseStr);
}
}

@Test
public void testInvalidUri2() {
public void testInvalidUri2() throws JSONException, Exception {
WebTarget r = target();
String responseStr = "";
Response response = r.path("ws").path("v1").path("invalid")
.request(MediaType.APPLICATION_JSON).get();
assertResponseStatusCode(Response.Status.NOT_FOUND, response.getStatusInfo());
WebServicesTestUtils.checkStringMatch(
"error string exists and shouldn't", "", responseStr);
try {
Response response = r.path("ws").path("v1").path("invalid")
.request(MediaType.APPLICATION_JSON).get();
throw new NotFoundException(response);
} catch (NotFoundException ue) {
Response response = ue.getResponse();
assertResponseStatusCode(Response.Status.NOT_FOUND, response.getStatusInfo());
WebServicesTestUtils.checkStringMatch(
"error string exists and shouldn't", "", responseStr);
}
}

@Test
public void testInvalidAccept() {
public void testInvalidAccept() throws JSONException, Exception {
WebTarget r = target();
String responseStr = "";
Response response =
r.path("ws").path("v1").path("history").request(MediaType.TEXT_PLAIN).get();
assertResponseStatusCode(Response.Status.SERVICE_UNAVAILABLE,
response.getStatusInfo());
WebServicesTestUtils.checkStringMatch(
"error string exists and shouldn't", "", responseStr);
try {
Response response =
r.path("ws").path("v1").path("history").request(MediaType.TEXT_PLAIN).get();
throw new ServiceUnavailableException(response);
} catch (ServiceUnavailableException ue) {
Response response = ue.getResponse();
assertResponseStatusCode(Response.Status.SERVICE_UNAVAILABLE,
response.getStatusInfo());
WebServicesTestUtils.checkStringMatch(
"error string exists and shouldn't", "", responseStr);
}
}

public void verifyHsInfoGeneric(String hadoopVersionBuiltOn,
Expand Down Expand Up @@ -271,4 +283,5 @@ public void verifyHSInfoXML(String xml) throws Exception {
WebServicesTestUtils.getXmlLong(element, "startedOn"));
}
}

}
Loading

0 comments on commit 3237736

Please sign in to comment.