Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.pepaproch.gtswsdlclient; import com.pepaproch.gtswsdlclient.auth.AuthTokenProvider; import com.pepaproch.gtswsdlclient.auth.AuthTokenProviderImpl; import com.pepaproch.gtswsdlclient.auth.AuthTokenRevokedListener; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.web.client.RestTemplate; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotSame; import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; /** * * @author pepa */ public class AuthTokenProviderImplTest { public static final String AUTH_PATH = "/v1/auth"; MockRestServiceServer mockServer; RestTemplate restTemplate = new RestTemplate(); private final String BASE_URL = "http://test"; private AuthTokenRevokedListener revokedListener; public AuthTokenProviderImplTest() { } @AfterClass public static void tearDownClass() { } @Before public void setUp() { } /** * Test of getAuthorisationToken method, of class AuthTokenProviderImpl. */ @Test public void testGetAuthorisationToken() { AuthTokenProvider instance = new AuthTokenProviderImpl(BASE_URL, "test", "test", restTemplate, 1); mockServer = MockRestServiceServer.createServer(restTemplate); String authResponseBody = "{\"accessToken\" : \"testtoken\" }"; this.mockServer.expect(requestTo(BASE_URL + AUTH_PATH)).andExpect(method(HttpMethod.POST)) .andRespond(withSuccess(authResponseBody, MediaType.APPLICATION_JSON)); String authorisationToken = instance.getAuthorisationToken().getToken(); String authorisationToken2 = instance.getAuthorisationToken().getToken(); this.mockServer.verify(); assertEquals(authorisationToken, authorisationToken2); } /** * Test of getAuthorisationToken method, of class AuthTokenProviderImpl. */ @Test public void testGetAuthorisationTokenExpired() { AuthTokenProvider instance = new AuthTokenProviderImpl(BASE_URL, "test", "test", restTemplate, 0); mockServer = MockRestServiceServer.createServer(restTemplate); this.mockServer.expect(requestTo(BASE_URL + AUTH_PATH)).andExpect(method(HttpMethod.POST)) .andRespond(withSuccess("{\"accessToken\" : \"testtoken\" }", MediaType.APPLICATION_JSON)); this.mockServer.expect(requestTo(BASE_URL + AUTH_PATH)).andExpect(method(HttpMethod.POST)) .andRespond(withSuccess("{\"accessToken\" : \"testtoken-1\" }", MediaType.APPLICATION_JSON)); String authorisationToken = instance.getAuthorisationToken().getToken(); String authorisationToken2 = instance.getAuthorisationToken().getToken(); this.mockServer.verify(); assertNotSame(authorisationToken, authorisationToken2); } /** * Test of revoked method, of class AuthTokenProviderImpl. */ @Test public void testRevoked() { AuthTokenProvider instance = new AuthTokenProviderImpl(BASE_URL, "test", "test", restTemplate, 10); mockServer = MockRestServiceServer.createServer(restTemplate); this.mockServer.expect(requestTo(BASE_URL + AUTH_PATH)).andExpect(method(HttpMethod.POST)) .andRespond(withSuccess("{\"accessToken\" : \"testtoken\" }", MediaType.APPLICATION_JSON)); this.mockServer.expect(requestTo(BASE_URL + AUTH_PATH)).andExpect(method(HttpMethod.POST)).andRespond( withSuccess("{\"accessToken\" : \"test token after revoke\" }", MediaType.APPLICATION_JSON)); String authorisationToken = instance.getAuthorisationToken().getToken(); this.revokedListener = (AuthTokenRevokedListener) instance; this.revokedListener.revoked(instance.getAuthorisationToken()); String authorisationToken2 = instance.getAuthorisationToken().getToken(); this.mockServer.verify(); assertNotSame(authorisationToken, authorisationToken2); } }