List of usage examples for org.springframework.http MediaType parseMediaType
public static MediaType parseMediaType(String mediaType)
From source file:org.apache.metron.rest.controller.GrokControllerIntegrationTest.java
@Test public void test() throws Exception { this.mockMvc/*from w w w .j a v a 2 s. co m*/ .perform(post(grokUrl + "/validate").with(httpBasic(user, password)).with(csrf()) .contentType(MediaType.parseMediaType("application/json;charset=UTF-8")) .content(grokValidationJson)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.results.action").value("TCP_MISS")) .andExpect(jsonPath("$.results.bytes").value(337891)) .andExpect(jsonPath("$.results.code").value(200)) .andExpect(jsonPath("$.results.elapsed").value(415)) .andExpect(jsonPath("$.results.ip_dst_addr").value("207.109.73.154")) .andExpect(jsonPath("$.results.ip_src_addr").value("127.0.0.1")) .andExpect(jsonPath("$.results.method").value("GET")) .andExpect(jsonPath("$.results.timestamp").value("1467011157.401")) .andExpect(jsonPath("$.results.url").value("http://www.aliexpress.com/af/shoes.html?")); this.mockMvc .perform(post(grokUrl + "/validate").with(httpBasic(user, password)).with(csrf()) .contentType(MediaType.parseMediaType("application/json;charset=UTF-8")) .content(missingPatternLabelGrokValidationJson)) .andExpect(status().isInternalServerError()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.responseCode").value(500)) .andExpect(jsonPath("$.message").value("Pattern label is required")); this.mockMvc .perform(post(grokUrl + "/validate").with(httpBasic(user, password)).with(csrf()) .contentType(MediaType.parseMediaType("application/json;charset=UTF-8")) .content(missingStatementGrokValidationJson)) .andExpect(status().isInternalServerError()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.responseCode").value(500)) .andExpect(jsonPath("$.message").value("Grok statement is required")); this.mockMvc.perform(get(grokUrl + "/list").with(httpBasic(user, password))).andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$").isNotEmpty()); String statement = FileUtils.readFileToString( new File("../../metron-platform/metron-parsers/src/main/resources/patterns/squid")); this.mockMvc.perform(get(grokUrl + "/get/statement?path=/patterns/squid").with(httpBasic(user, password))) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("text/plain;charset=UTF-8"))) .andExpect(content().bytes(statement.getBytes())); this.mockMvc.perform(get(grokUrl + "/get/statement?path=/bad/path").with(httpBasic(user, password))) .andExpect(status().isInternalServerError()).andExpect(jsonPath("$.responseCode").value(500)) .andExpect(jsonPath("$.message").value("Could not find a statement at path /bad/path")); }
From source file:org.apache.metron.rest.controller.PcapControllerIntegrationTest.java
@Test public void testSecurity() throws Exception { this.mockMvc/*from www.j a va 2 s . c o m*/ .perform(post(pcapUrl + "/fixed").with(csrf()) .contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(fixedJson)) .andExpect(status().isUnauthorized()); this.mockMvc .perform(post(pcapUrl + "/query").with(csrf()) .contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(queryJson)) .andExpect(status().isUnauthorized()); }
From source file:org.apache.metron.rest.controller.PcapControllerIntegrationTest.java
@Test public void testFixedRequest() throws Exception { MockPcapJob mockPcapJob = (MockPcapJob) wac.getBean("mockPcapJob"); mockPcapJob.setStatus(new JobStatus().withState(JobStatus.State.RUNNING)); this.mockMvc/*from w w w. ja v a 2 s . com*/ .perform(post(pcapUrl + "/fixed").with(httpBasic(user, password)).with(csrf()) .contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(fixedJson)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.jobStatus").value("RUNNING")); Assert.assertEquals("/base/path", mockPcapJob.getBasePath()); Assert.assertEquals("/base/interim/result/path", mockPcapJob.getBaseInterrimResultPath()); Assert.assertEquals("/final/output/path", mockPcapJob.getFinalOutputPath()); Assert.assertEquals(10000000, mockPcapJob.getStartTimeNs()); Assert.assertEquals(20000000, mockPcapJob.getEndTimeNs()); Assert.assertEquals(2, mockPcapJob.getNumReducers()); Assert.assertTrue(mockPcapJob.getFilterImpl() instanceof FixedPcapFilter.Configurator); Map<String, String> actualFixedFields = mockPcapJob.getFixedFields(); Assert.assertEquals("192.168.1.2", actualFixedFields.get(Constants.Fields.SRC_ADDR.getName())); Assert.assertEquals("2000", actualFixedFields.get(Constants.Fields.SRC_PORT.getName())); Assert.assertEquals("192.168.1.1", actualFixedFields.get(Constants.Fields.DST_ADDR.getName())); Assert.assertEquals("1000", actualFixedFields.get(Constants.Fields.DST_PORT.getName())); Assert.assertEquals("true", actualFixedFields.get(Constants.Fields.INCLUDES_REVERSE_TRAFFIC.getName())); Assert.assertEquals("TCP", actualFixedFields.get(Constants.Fields.PROTOCOL.getName())); Assert.assertEquals("filter", actualFixedFields.get(PcapHelper.PacketFields.PACKET_FILTER.getName())); }
From source file:org.apache.metron.rest.controller.PcapControllerIntegrationTest.java
@Test public void testFixedRequestDefaults() throws Exception { MockPcapJob mockPcapJob = (MockPcapJob) wac.getBean("mockPcapJob"); mockPcapJob.setStatus(new JobStatus().withState(JobStatus.State.RUNNING)); long beforeJobTime = System.currentTimeMillis(); this.mockMvc// ww w. j av a 2s . c o m .perform(post(pcapUrl + "/fixed").with(httpBasic(user, password)).with(csrf()) .contentType(MediaType.parseMediaType("application/json;charset=UTF-8")) .content(fixedWithDefaultsJson)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.jobStatus").value("RUNNING")); Assert.assertEquals("/apps/metron/pcap/input", mockPcapJob.getBasePath()); Assert.assertEquals("/apps/metron/pcap/interim", mockPcapJob.getBaseInterrimResultPath()); Assert.assertEquals("/apps/metron/pcap/output", mockPcapJob.getFinalOutputPath()); Assert.assertEquals(0, mockPcapJob.getStartTimeNs()); Assert.assertTrue(beforeJobTime < mockPcapJob.getEndTimeNs() / 1000000); Assert.assertTrue(System.currentTimeMillis() > mockPcapJob.getEndTimeNs() / 1000000); Assert.assertEquals(10, mockPcapJob.getNumReducers()); Assert.assertTrue(mockPcapJob.getFilterImpl() instanceof FixedPcapFilter.Configurator); Map<String, String> actualFixedFields = mockPcapJob.getFixedFields(); Assert.assertEquals("192.168.1.2", actualFixedFields.get(Constants.Fields.SRC_ADDR.getName())); Assert.assertEquals("2000", actualFixedFields.get(Constants.Fields.SRC_PORT.getName())); Assert.assertEquals("192.168.1.1", actualFixedFields.get(Constants.Fields.DST_ADDR.getName())); Assert.assertEquals("1000", actualFixedFields.get(Constants.Fields.DST_PORT.getName())); Assert.assertEquals("true", actualFixedFields.get(Constants.Fields.INCLUDES_REVERSE_TRAFFIC.getName())); Assert.assertEquals("TCP", actualFixedFields.get(Constants.Fields.PROTOCOL.getName())); Assert.assertEquals("filter", actualFixedFields.get(PcapHelper.PacketFields.PACKET_FILTER.getName())); }
From source file:org.apache.metron.rest.controller.PcapControllerIntegrationTest.java
@Test public void testQueryRequest() throws Exception { MockPcapJob mockPcapJob = (MockPcapJob) wac.getBean("mockPcapJob"); mockPcapJob.setStatus(new JobStatus().withState(JobStatus.State.RUNNING)); this.mockMvc//ww w .j a v a 2s .c o m .perform(post(pcapUrl + "/query").with(httpBasic(user, password)).with(csrf()) .contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(queryJson)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.jobStatus").value("RUNNING")); Assert.assertEquals("/base/path", mockPcapJob.getBasePath()); Assert.assertEquals("/base/interim/result/path", mockPcapJob.getBaseInterrimResultPath()); Assert.assertEquals("/final/output/path", mockPcapJob.getFinalOutputPath()); Assert.assertEquals(10000000, mockPcapJob.getStartTimeNs()); Assert.assertEquals(20000000, mockPcapJob.getEndTimeNs()); Assert.assertEquals(2, mockPcapJob.getNumReducers()); Assert.assertTrue(mockPcapJob.getFilterImpl() instanceof QueryPcapFilter.Configurator); Assert.assertEquals("query", mockPcapJob.getQuery()); }
From source file:org.apache.metron.rest.controller.PcapControllerIntegrationTest.java
@Test public void testTooManyJobs() throws Exception { MockPcapJob mockPcapJob = (MockPcapJob) wac.getBean("mockPcapJob"); mockPcapJob.setStatus(new JobStatus().withJobId("jobId").withState(JobStatus.State.RUNNING)); this.mockMvc//from w w w . j av a 2 s . c o m .perform(post(pcapUrl + "/fixed").with(httpBasic(user, password)).with(csrf()) .contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(fixedJson)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.jobId").value("jobId")).andExpect(jsonPath("$.jobStatus").value("RUNNING")); this.mockMvc .perform(post(pcapUrl + "/fixed").with(httpBasic(user, password)).with(csrf()) .contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(fixedJson)) .andExpect(status().isInternalServerError()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.message").value( "Cannot submit job because a job is already running. Please contact the administrator to cancel job(s) with id(s) jobId")); }
From source file:org.apache.metron.rest.controller.PcapControllerIntegrationTest.java
@Test public void testGetStatus() throws Exception { MockPcapJob mockPcapJob = (MockPcapJob) wac.getBean("mockPcapJob"); mockPcapJob.setStatus(new JobStatus().withJobId("jobId").withState(JobStatus.State.RUNNING)); this.mockMvc//from w w w . j a v a 2 s . co m .perform(post(pcapUrl + "/fixed").with(httpBasic(user, password)).with(csrf()) .contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(fixedJson)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.jobId").value("jobId")).andExpect(jsonPath("$.jobStatus").value("RUNNING")); mockPcapJob.setStatus(new JobStatus().withJobId("jobId").withState(JobStatus.State.SUCCEEDED)); Pageable<Path> pageable = new PcapPages(Arrays.asList(new Path("path1"), new Path("path1"))); mockPcapJob.setIsDone(true); mockPcapJob.setPageable(pageable); this.mockMvc.perform(get(pcapUrl + "/jobId").with(httpBasic(user, password))).andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.jobStatus").value("SUCCEEDED")).andExpect(jsonPath("$.pageTotal").value(2)); mockPcapJob.setStatus(new JobStatus().withJobId("jobId").withState(JobStatus.State.FINALIZING)); this.mockMvc.perform(get(pcapUrl + "/jobId").with(httpBasic(user, password))).andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.jobStatus").value("FINALIZING")); mockPcapJob.setStatus(new JobStatus().withJobId("jobId").withState(JobStatus.State.FAILED)); this.mockMvc.perform(get(pcapUrl + "/jobId").with(httpBasic(user, password))).andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.jobStatus").value("FAILED")); mockPcapJob.setStatus(new JobStatus().withJobId("jobId").withState(JobStatus.State.KILLED)); this.mockMvc.perform(get(pcapUrl + "/jobId").with(httpBasic(user, password))).andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.jobStatus").value("KILLED")); this.mockMvc.perform(get(pcapUrl + "/someJobId").with(httpBasic(user, password))) .andExpect(status().isNotFound()); }
From source file:org.apache.metron.rest.controller.PcapControllerIntegrationTest.java
@Test public void testGetStatusList() throws Exception { MockPcapJob mockPcapJob = (MockPcapJob) wac.getBean("mockPcapJob"); mockPcapJob.setStatus(new JobStatus().withJobId("jobId").withState(JobStatus.State.RUNNING)); this.mockMvc/*from w w w . j a v a 2 s. c om*/ .perform(post(pcapUrl + "/fixed").with(httpBasic(user, password)).with(csrf()) .contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(fixedJson)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.jobId").value("jobId")).andExpect(jsonPath("$.jobStatus").value("RUNNING")); this.mockMvc.perform(get(pcapUrl + "?state=RUNNING").with(httpBasic(user, password))) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$[0].jobId").value("jobId")) .andExpect(jsonPath("$[0].jobStatus").value("RUNNING")); this.mockMvc.perform(get(pcapUrl + "?state=SUCCEEDED").with(httpBasic(user, password))) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(content().json("[]")); }
From source file:org.apache.metron.rest.controller.PcapControllerIntegrationTest.java
@Test public void testKillJob() throws Exception { MockPcapJob mockPcapJob = (MockPcapJob) wac.getBean("mockPcapJob"); this.mockMvc.perform(get(pcapUrl + "/jobId123").with(httpBasic(user, password))) .andExpect(status().isNotFound()); mockPcapJob.setStatus(new JobStatus().withJobId("jobId123").withState(JobStatus.State.RUNNING)); this.mockMvc// w ww. ja v a2 s . c o m .perform(post(pcapUrl + "/fixed").with(httpBasic(user, password)).with(csrf()) .contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(fixedJson)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.jobId").value("jobId123")) .andExpect(jsonPath("$.jobStatus").value("RUNNING")); mockPcapJob.setStatus(new JobStatus().withJobId("jobId123").withState(JobStatus.State.KILLED)); this.mockMvc.perform(delete(pcapUrl + "/kill/{id}", "jobId123").with(httpBasic(user, password))) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.jobId").value("jobId123")) .andExpect(jsonPath("$.jobStatus").value("KILLED")); mockPcapJob.setStatus(new JobStatus().withJobId("jobId").withState(JobStatus.State.KILLED)); }
From source file:org.apache.metron.rest.controller.PcapControllerIntegrationTest.java
@Test public void testGetPdml() throws Exception { MockPcapJob mockPcapJob = (MockPcapJob) wac.getBean("mockPcapJob"); mockPcapJob.setStatus(new JobStatus().withJobId("jobId").withState(JobStatus.State.RUNNING)); this.mockMvc//from w w w . j ava2s . c om .perform(post(pcapUrl + "/fixed").with(httpBasic(user, password)).with(csrf()) .contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(fixedJson)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.jobId").value("jobId")).andExpect(jsonPath("$.jobStatus").value("RUNNING")); Pageable<Path> pageable = new PcapPages(Arrays.asList(new Path("./target"))); mockPcapJob.setIsDone(true); mockPcapJob.setPageable(pageable); this.mockMvc.perform(get(pcapUrl + "/jobId/pdml?page=1").with(httpBasic(user, password))) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(jsonPath("$.version").value("0")) .andExpect(jsonPath("$.creator").value("wireshark/2.6.1")) .andExpect(jsonPath("$.time").value("Thu Jun 28 14:14:38 2018")) .andExpect(jsonPath("$.captureFile") .value("/tmp/pcap-data-201806272004-289365c53112438ca55ea047e13a12a5+0001.pcap")) .andExpect(jsonPath("$.packets[0].protos[0].name").value("geninfo")) .andExpect(jsonPath("$.packets[0].protos[0].fields[0].name").value("num")) .andExpect(jsonPath("$.packets[0].protos[1].name").value("ip")) .andExpect(jsonPath("$.packets[0].protos[1].fields[0].name").value("ip.addr")); this.mockMvc.perform(get(pcapUrl + "/jobId/pdml?page=0").with(httpBasic(user, password))) .andExpect(status().isNotFound()); this.mockMvc.perform(get(pcapUrl + "/jobId/pdml?page=2").with(httpBasic(user, password))) .andExpect(status().isNotFound()); }