Search in sources :

Example 21 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project hadoop by apache.

the class TestHttpExceptionUtils method testValidateResponseJsonErrorUnknownException.

@Test
public void testValidateResponseJsonErrorUnknownException() throws IOException {
    Map<String, Object> json = new HashMap<String, Object>();
    json.put(HttpExceptionUtils.ERROR_EXCEPTION_JSON, "FooException");
    json.put(HttpExceptionUtils.ERROR_CLASSNAME_JSON, "foo.FooException");
    json.put(HttpExceptionUtils.ERROR_MESSAGE_JSON, "EX");
    Map<String, Object> response = new HashMap<String, Object>();
    response.put(HttpExceptionUtils.ERROR_JSON, json);
    ObjectMapper jsonMapper = new ObjectMapper();
    String msg = jsonMapper.writeValueAsString(response);
    InputStream is = new ByteArrayInputStream(msg.getBytes());
    HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
    Mockito.when(conn.getErrorStream()).thenReturn(is);
    Mockito.when(conn.getResponseMessage()).thenReturn("msg");
    Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_BAD_REQUEST);
    try {
        HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED);
        Assert.fail();
    } catch (IOException ex) {
        Assert.assertTrue(ex.getMessage().contains("EX"));
        Assert.assertTrue(ex.getMessage().contains("foo.FooException"));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 22 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project hadoop by apache.

the class TestWebHDFSOAuth2 method getOAuthServerMockRequest.

private HttpRequest getOAuthServerMockRequest(MockServerClient mockServerClient) throws IOException {
    HttpRequest expectedRequest = request().withMethod("POST").withPath("/refresh").withBody("client_secret=credential&grant_type=client_credentials&client_id=MY_CLIENTID");
    Map<String, Object> map = new TreeMap<>();
    map.put(EXPIRES_IN, "0987654321");
    map.put(TOKEN_TYPE, "bearer");
    map.put(ACCESS_TOKEN, AUTH_TOKEN);
    ObjectMapper mapper = new ObjectMapper();
    HttpResponse resp = response().withStatusCode(HttpStatus.SC_OK).withHeaders(CONTENT_TYPE_APPLICATION_JSON).withBody(mapper.writeValueAsString(map));
    mockServerClient.when(expectedRequest, exactly(1)).respond(resp);
    return expectedRequest;
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) HttpResponse(org.mockserver.model.HttpResponse) TreeMap(java.util.TreeMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 23 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project hadoop by apache.

the class TestRefreshTokenTimeBasedTokenRefresher method refreshUrlIsCorrect.

@Test
public void refreshUrlIsCorrect() throws IOException {
    final int PORT = 7552;
    final String REFRESH_ADDRESS = "http://localhost:" + PORT + "/refresh";
    long tokenExpires = 0;
    Configuration conf = buildConf("refresh token key", Long.toString(tokenExpires), "joebob", REFRESH_ADDRESS);
    Timer mockTimer = mock(Timer.class);
    when(mockTimer.now()).thenReturn(tokenExpires + 1000l);
    AccessTokenProvider tokenProvider = new ConfRefreshTokenBasedAccessTokenProvider(mockTimer);
    tokenProvider.setConf(conf);
    // Build mock server to receive refresh request
    ClientAndServer mockServer = startClientAndServer(PORT);
    HttpRequest expectedRequest = request().withMethod("POST").withPath("/refresh").withBody(ParameterBody.params(Parameter.param(CLIENT_ID, "joebob"), Parameter.param(GRANT_TYPE, REFRESH_TOKEN), Parameter.param(REFRESH_TOKEN, "refresh token key")));
    MockServerClient mockServerClient = new MockServerClient("localhost", PORT);
    // https://tools.ietf.org/html/rfc6749#section-5.1
    Map<String, Object> map = new TreeMap<>();
    map.put(EXPIRES_IN, "0987654321");
    map.put(TOKEN_TYPE, BEARER);
    map.put(ACCESS_TOKEN, "new access token");
    ObjectMapper mapper = new ObjectMapper();
    HttpResponse resp = response().withStatusCode(HttpStatus.SC_OK).withHeaders(CONTENT_TYPE_APPLICATION_JSON).withBody(mapper.writeValueAsString(map));
    mockServerClient.when(expectedRequest, exactly(1)).respond(resp);
    assertEquals("new access token", tokenProvider.getAccessToken());
    mockServerClient.verify(expectedRequest);
    mockServerClient.clear(expectedRequest);
    mockServer.stop();
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) Configuration(org.apache.hadoop.conf.Configuration) HttpResponse(org.mockserver.model.HttpResponse) MockServerClient(org.mockserver.client.server.MockServerClient) TreeMap(java.util.TreeMap) Timer(org.apache.hadoop.util.Timer) ClientAndServer(org.mockserver.integration.ClientAndServer) ClientAndServer.startClientAndServer(org.mockserver.integration.ClientAndServer.startClientAndServer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 24 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project hadoop by apache.

the class TestDiskBalancerRPC method testGetDiskBalancerVolumeMapping.

@Test
public void testGetDiskBalancerVolumeMapping() throws Exception {
    final int dnIndex = 0;
    DataNode dataNode = cluster.getDataNodes().get(dnIndex);
    String volumeNameJson = dataNode.getDiskBalancerSetting(DiskBalancerConstants.DISKBALANCER_VOLUME_NAME);
    Assert.assertNotNull(volumeNameJson);
    ObjectMapper mapper = new ObjectMapper();
    @SuppressWarnings("unchecked") Map<String, String> volumemap = mapper.readValue(volumeNameJson, HashMap.class);
    Assert.assertEquals(2, volumemap.size());
}
Also used : DataNode(org.apache.hadoop.hdfs.server.datanode.DataNode) DiskBalancerDataNode(org.apache.hadoop.hdfs.server.diskbalancer.datamodel.DiskBalancerDataNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 25 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project hadoop by apache.

the class TestNameNodeMXBean method testTopUsers.

@Test(timeout = 120000)
@SuppressWarnings("unchecked")
public void testTopUsers() throws Exception {
    final Configuration conf = new Configuration();
    MiniDFSCluster cluster = null;
    try {
        cluster = new MiniDFSCluster.Builder(conf).numDataNodes(0).build();
        cluster.waitActive();
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName mxbeanNameFsns = new ObjectName("Hadoop:service=NameNode,name=FSNamesystemState");
        FileSystem fs = cluster.getFileSystem();
        final Path path = new Path("/");
        final int NUM_OPS = 10;
        for (int i = 0; i < NUM_OPS; i++) {
            fs.listStatus(path);
            fs.setTimes(path, 0, 1);
        }
        String topUsers = (String) (mbs.getAttribute(mxbeanNameFsns, "TopUserOpCounts"));
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = mapper.readValue(topUsers, Map.class);
        assertTrue("Could not find map key timestamp", map.containsKey("timestamp"));
        assertTrue("Could not find map key windows", map.containsKey("windows"));
        List<Map<String, List<Map<String, Object>>>> windows = (List<Map<String, List<Map<String, Object>>>>) map.get("windows");
        assertEquals("Unexpected num windows", 3, windows.size());
        for (Map<String, List<Map<String, Object>>> window : windows) {
            final List<Map<String, Object>> ops = window.get("ops");
            assertEquals("Unexpected num ops", 3, ops.size());
            for (Map<String, Object> op : ops) {
                final long count = Long.parseLong(op.get("totalCount").toString());
                final String opType = op.get("opType").toString();
                final int expected;
                if (opType.equals(TopConf.ALL_CMDS)) {
                    expected = 2 * NUM_OPS;
                } else {
                    expected = NUM_OPS;
                }
                assertEquals("Unexpected total count", expected, count);
            }
        }
    } finally {
        if (cluster != null) {
            cluster.shutdown();
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) MiniDFSCluster(org.apache.hadoop.hdfs.MiniDFSCluster) Configuration(org.apache.hadoop.conf.Configuration) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) ObjectName(javax.management.ObjectName) FileSystem(org.apache.hadoop.fs.FileSystem) DistributedFileSystem(org.apache.hadoop.hdfs.DistributedFileSystem) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1523 Test (org.junit.Test)608 JsonNode (com.fasterxml.jackson.databind.JsonNode)217 IOException (java.io.IOException)191 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)172 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)127 Map (java.util.Map)122 HashMap (java.util.HashMap)111 ArrayList (java.util.ArrayList)71 File (java.io.File)56 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)53 JCodeModel (com.sun.codemodel.JCodeModel)52 InputStream (java.io.InputStream)50 JPackage (com.sun.codemodel.JPackage)44 List (java.util.List)42 JsonException (jmri.server.json.JsonException)41 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)39 JType (com.sun.codemodel.JType)38 Test (org.testng.annotations.Test)36 JsonFactory (com.fasterxml.jackson.core.JsonFactory)34