@Before public void setup() { google = new GoogleTemplate("ACCESS_TOKEN"); mockServer = MockRestServiceServer.createServer(google.getRestTemplate()); appAuthGoogle = new GoogleTemplate("APP_ACCESS_TOKEN"); appAuthMockServer = MockRestServiceServer.createServer(appAuthGoogle.getRestTemplate()); }
@Test(expected = ExpiredAuthorizationException.class) public void tokenInvalid_tokenExpired() { MockRestServiceServer mockServer = MockRestServiceServer.createServer(facebook.getRestTemplate()); mockServer .expect(requestTo("https://graph.facebook.com/me")) .andExpect(method(GET)) .andRespond( withBadRequest() .body(jsonResource("testdata/error-expired-token")) .contentType(MediaType.APPLICATION_JSON)); facebook.userOperations().getUserProfile(); }
@Test public void testWhenTheCodeIsDenied() throws Exception { mockUaaServer .expect(requestTo("http://uaa.example.com/uaa/password_resets")) .andExpect(method(POST)) .andRespond(withBadRequest()); emailResetPasswordService.forgotPassword(uriComponentsBuilder, "*****@*****.**"); mockUaaServer.verify(); Assert.assertEquals(0, smtpServer.getReceivedEmailSize()); }
@Test(expected = MissingAuthorizationException.class) public void currentUser_noAccessToken() { FacebookTemplate facebook = new FacebookTemplate(); // use anonymous FacebookTemplate in this test MockRestServiceServer mockServer = MockRestServiceServer.createServer(facebook.getRestTemplate()); mockServer .expect(requestTo("https://graph.facebook.com/me")) .andExpect(method(GET)) .andRespond( withBadRequest() .body(jsonResource("testdata/error-current-user-no-token")) .contentType(MediaType.APPLICATION_JSON)); facebook.userOperations().getUserProfile(); }
@Test public void testChangingAPassword() throws Exception { mockUaaServer .expect(requestTo("http://uaa.example.com/uaa/password_change")) .andExpect(method(POST)) .andExpect(jsonPath("$.code").value("secret_code")) .andExpect(jsonPath("$.new_password").value("new_secret")) .andRespond(withSuccess("userman", APPLICATION_JSON)); String username = emailResetPasswordService.resetPassword("secret_code", "new_secret"); mockUaaServer.verify(); Assert.assertEquals("userman", username); }
@Test public void testCreateCustomer() throws Exception { long now = System.currentTimeMillis(); String f = "Joe", l = "Doe"; String jsonOfJoeDoe = "{ \"signupDate\":" + now + ",\"firstName\":\"" + f + "\",\"lastName\":\"" + l + "\"}"; MvcResult mvcResult = mockMvc .perform( post("/users/{userId}/customers", userId) .accept(applicationJsonMediaType) .content(jsonOfJoeDoe) .contentType(this.applicationJsonMediaType)) .andExpect(status().isCreated()) .andExpect(content().contentType(this.applicationJsonMediaType)) .andReturn(); mockServer.verify(); String locationUri = mvcResult.getResponse().getHeader("Location"); Assert.assertTrue(locationUri.contains("/users/" + userId + "/customers/")); }
@Before public void setup() { ServiceProperties properties = new ServiceProperties(); properties.setVehicleServiceRootUrl("http://example.com/"); this.service = new RemoteVehicleDetailsService(properties); this.server = MockRestServiceServer.createServer(this.service.getRestTemplate()); }
@Before public void setup() { linkedIn = new LinkedInTemplate("API_KEY", "API_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET"); mockServer = MockRestServiceServer.createServer(linkedIn.getRestTemplate()); responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.APPLICATION_JSON); }
@Test public void tokenInvalid_signedOutOfFacebook_unauthorized() { MockRestServiceServer mockServer = MockRestServiceServer.createServer(facebook.getRestTemplate()); mockServer .expect(requestTo("https://graph.facebook.com/me")) .andExpect(method(GET)) .andRespond( withStatus(HttpStatus.UNAUTHORIZED) .body(jsonResource("testdata/error-invalid-token-signout")) .contentType(MediaType.APPLICATION_JSON)); try { facebook.userOperations().getUserProfile(); fail("Expected RevokedAuthorizationException"); } catch (RevokedAuthorizationException e) { assertEquals(LOGGED_OUT_REVOKATION, e.getMessage()); } }
@Test public void tokenInvalid_passwordChanged_unauthorized() { MockRestServiceServer mockServer = MockRestServiceServer.createServer(facebook.getRestTemplate()); mockServer .expect(requestTo("https://graph.facebook.com/me")) .andExpect(method(GET)) .andRespond( withBadRequest() .body(jsonResource("testdata/error-invalid-token-password")) .contentType(MediaType.APPLICATION_JSON)); try { facebook.userOperations().getUserProfile(); fail("Expected RevokedAuthorizationException"); } catch (RevokedAuthorizationException e) { assertEquals(CHANGED_PASSWORD_REVOKATION, e.getMessage()); } }
@Test public void testWhenAResetCodeIsReturnedByTheUaa() throws Exception { mockUaaServer .expect(requestTo("http://uaa.example.com/uaa/password_resets")) .andExpect(method(POST)) .andRespond(withSuccess("the_secret_code", APPLICATION_JSON)); emailResetPasswordService.forgotPassword(uriComponentsBuilder, "*****@*****.**"); mockUaaServer.verify(); Assert.assertEquals(1, smtpServer.getReceivedEmailSize()); SmtpMessage message = (SmtpMessage) smtpServer.getReceivedEmail().next(); Assert.assertEquals("*****@*****.**", message.getHeaderValue("To")); Assert.assertEquals( "Click the link to reset your password <a href=\"http://login.example.com/login/reset_password?code=the_secret_code&[email protected]\">Reset Password</a>", message.getBody()); }
@Test public void shouldGetVersion() { // Given MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate); String body = "1.2.0.SNAPSHOT"; String expectedUri = "http://localhost:8080/test/version"; mockServer .expect(requestTo(expectedUri)) .andExpect(method(HttpMethod.GET)) .andRespond(withSuccess(body, MediaType.TEXT_PLAIN)); willReturn(restTemplate).given(restClient).getRestTemplate(); // When String version = restClient.getVersionFrom(expectedUri); // Then Assert.assertEquals("Version should be", body, version); }
@Test public void testFeed6() throws Exception { mockServer.expect(requestTo("url")).andRespond(withBadRequest()); FetchResult irrelevant = parser.parse("url", "lastModified"); assertThat(irrelevant.getUrl(), is("url")); assertThat(irrelevant.getLastModified(), nullValue()); assertThat(irrelevant.getEntries(), hasSize(0)); }
@Test(expected = HttpServerErrorException.class) public void subscribeContextRequestWith500() throws Exception { mockServer .expect(requestTo(baseUrl + "/ngsi10/subscribeContext")) .andExpect(method(HttpMethod.POST)) .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR)); ngsiClient.subscribeContext(baseUrl, null, createSubscribeContextTemperature()).get(); }
@Before public void setUp() throws Exception { smtpServer = SimpleSmtpServer.start(2525); RestTemplate uaaTemplate = new RestTemplate(); mockUaaServer = MockRestServiceServer.createServer(uaaTemplate); uriComponentsBuilder = UriComponentsBuilder.fromUriString("http://login.example.com/login"); emailResetPasswordService = new EmailResetPasswordService( uaaTemplate, "http://uaa.example.com/uaa", "localhost", 2525, "", ""); }
@Test public void testFeed9() throws Exception { mockServer .expect(requestTo("irrelevant")) .andExpect(method(GET)) .andExpect(header("User-Agent", startsWith("Mozilla"))) .andRespond(withStatus(HttpStatus.NOT_MODIFIED)); parser.parse("irrelevant", "lastModified"); }
@Test public void testFeed1() throws Exception { mockServer .expect(requestTo(HTTP_EXAMPLE_COM)) .andExpect(method(GET)) .andRespond(withSuccess(new ClassPathResource("rss/feed1.xml"), TEXT_XML)); FetchResult result = parser.parse(HTTP_EXAMPLE_COM); assertThat(result.getEntries(), hasSize(2)); assertThat(result.getResultSizePerFetch(), is(2)); }
@Test public void testInvalidCharacter() throws Exception { // test [^\u0020-\uD7FF]+ mockServer .expect(requestTo(HTTP_EXAMPLE_COM)) .andExpect(method(GET)) .andRespond(withSuccess(new ClassPathResource("rss/feed7.xml"), TEXT_XML)); FetchResult result = parser.parse(HTTP_EXAMPLE_COM); assertThat(result.getEntries(), hasSize(6)); }
@Test public void shouldGetEnvProps() throws IOException, URISyntaxException { // Given String url = "http://localhost:8080/%s/version"; MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate); String body = loadJsonFile(); mockServer .expect(requestTo("http://localhost:8080/test/version")) .andExpect(method(HttpMethod.GET)) .andRespond(withSuccess(body, MediaType.APPLICATION_JSON)); willReturn(restTemplate).given(restClient).getRestTemplate(); restClient.setUrl(url); // When EnvironmentProperties environmentProperties = restClient.getEnvironmentProperties("test"); // Then Assert.assertEquals( "Test expectedName should be", "test", environmentProperties.getTest().getExpectedName()); }
@Test public void testFeed8() throws Exception { mockServer .expect(requestTo("irrelevant")) .andExpect(method(GET)) .andExpect(header("If-Modified-Since", is("lastModified"))) .andRespond(withStatus(HttpStatus.NOT_MODIFIED)); FetchResult result = parser.parse("irrelevant", "lastModified"); assertThat(result.getEntries(), empty()); }
@Before public void setup() { List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); converters.add(new StringHttpMessageConverter()); converters.add(new MappingJackson2HttpMessageConverter()); this.restTemplate = new RestTemplate(); this.restTemplate.setMessageConverters(converters); this.mockServer = MockRestServiceServer.createServer(this.restTemplate); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); }
@Test public void testFeed7() throws Exception { mockServer .expect(requestTo(HTTP_EXAMPLE_COM)) .andExpect(method(GET)) .andRespond(withSuccess(new ClassPathResource("rss/feed2.xml"), TEXT_XML)); FetchResult result = parser.parse(HTTP_EXAMPLE_COM); assertThat( result.getEntries(), hasItems(hasProperty("url", is("http://www.javaspecialists.eu/archive/Issue220b.html")))); }
@Test public void testFeed4() throws Exception { mockServer .expect(requestTo("https://github.com/ksokol.private.atom")) .andExpect(method(GET)) .andRespond(withSuccess(new ClassPathResource("rss/feed4.xml"), TEXT_XML)); FetchResult result = parser.parse("https://github.com/ksokol.private.atom"); assertThat( result.getEntries(), hasItems(hasProperty("content", startsWith("<!-- issue_comment -->")))); }
@Test public void testUpdateProducerForm() throws Exception { mockServer .expect(requestTo(restServicesPrefix + "/producer/1")) .andExpect(method(HttpMethod.GET)) .andRespond( withSuccess( "{\"producerId\":null,\"producerName\":null," + "\"country\":null}", MediaType.APPLICATION_JSON)); mockServer .expect(requestTo(restServicesPrefix + "/producer/dto")) .andExpect(method(HttpMethod.GET)) .andRespond(withSuccess("{\"producers\":null,\"total\":null}", MediaType.APPLICATION_JSON)); mockMvc .perform(get("/admin/producer/update/1").accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().isOk()) .andExpect(forwardedUrl("/WEB-INF/view/producerform.jsp")); }
@Test(expected = UncategorizedApiException.class) public void htmlErrorResponse() { try { FacebookTemplate facebook = new FacebookTemplate(); // use anonymous FacebookTemplate in this test MockRestServiceServer mockServer = MockRestServiceServer.createServer(facebook.getRestTemplate()); mockServer .expect(requestTo("https://graph.facebook.com/123456/picture?type=normal")) .andExpect(method(GET)) .andRespond( withBadRequest() .body(new ClassPathResource("testdata/error-not-json.html", getClass())) .contentType(MediaType.TEXT_HTML)); facebook.userOperations().getUserProfileImage("123456"); fail("Expected UncategorizedApiException"); } catch (UncategorizedApiException e) { assertTrue(e.getCause() instanceof HttpClientErrorException); throw e; } }
@Test public void testDeleteProducer() throws Exception { mockServer .expect(requestTo(restServicesPrefix + "/producer/1")) .andExpect(method(HttpMethod.DELETE)) .andRespond(withSuccess("redirect:/admin/producer", MediaType.APPLICATION_JSON)); mockMvc .perform(post("/admin/producer/delete/1")) .andDo(print()) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/admin/producer")); }
@Test public void testFeed3() throws Exception { mockServer .expect(requestTo(HTTP_EXAMPLE_COM)) .andExpect(method(GET)) .andRespond(withSuccess(new ClassPathResource("rss/feed3.xml"), TEXT_XML)); FetchResult result = parser.parse(HTTP_EXAMPLE_COM); assertThat( result.getEntries(), hasItems( hasProperty("url", is(HTTP_EXAMPLE_COM + "/12539.htm")), hasProperty("url", is(HTTP_EXAMPLE_COM + "/12673.htm")))); }
@Test public void testFeed5() throws Exception { mockServer .expect(requestTo("http://neusprech.org/feed/")) .andExpect(method(GET)) .andRespond(withSuccess(new ClassPathResource("rss/feed5.xml"), TEXT_XML)); FetchResult result = parser.parse("http://neusprech.org/feed/"); assertThat( result.getEntries(), hasItems( hasProperty( "content", startsWith("Ein Gastbeitrag von Erik W. Ende Juni 2014 sagte")))); }
@Before public void setUp() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/view/"); viewResolver.setSuffix(".jsp"); mockMvc = standaloneSetup(producerWebController) .setMessageConverters(new MappingJackson2HttpMessageConverter()) .setViewResolvers(viewResolver) .build(); mockServer = MockRestServiceServer.createServer(restTemplate); }
@Test public void testAdjustedResponseContentType() { String url = "https://feeds.feedwrench.com/AdventuresInAngular.rss"; mockServer .expect(requestTo(url)) .andExpect(method(GET)) .andRespond( withSuccess(new ClassPathResource("rss/feed7.xml"), TEXT_XML) .contentType(MediaType.TEXT_PLAIN)); FetchResult result = parser.parse(url); assertThat(result.getEntries(), hasSize(greaterThan(0))); }