diff --git a/src/test/java/at/porscheinformatik/weblate/spring/WeblateCodesTest.java b/src/test/java/at/porscheinformatik/weblate/spring/WeblateCodesTest.java index c6d5290..0250b03 100644 --- a/src/test/java/at/porscheinformatik/weblate/spring/WeblateCodesTest.java +++ b/src/test/java/at/porscheinformatik/weblate/spring/WeblateCodesTest.java @@ -10,7 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.params.provider.Arguments.arguments; -public class WeblateCodesTest { +class WeblateCodesTest { @ParameterizedTest @MethodSource("validCodesProvider") diff --git a/src/test/java/at/porscheinformatik/weblate/spring/WeblateMessageSourceTest.java b/src/test/java/at/porscheinformatik/weblate/spring/WeblateMessageSourceTest.java index 35676b0..e67bd19 100644 --- a/src/test/java/at/porscheinformatik/weblate/spring/WeblateMessageSourceTest.java +++ b/src/test/java/at/porscheinformatik/weblate/spring/WeblateMessageSourceTest.java @@ -7,11 +7,13 @@ import org.springframework.http.MediaType; import org.springframework.test.web.client.ExpectedCount; import org.springframework.test.web.client.MockRestServiceServer; +import org.springframework.test.web.client.response.DefaultResponseCreator; import org.springframework.web.client.RestTemplate; import java.net.URI; import java.net.URISyntaxException; import java.util.Locale; +import java.util.Properties; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -19,7 +21,7 @@ import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus; -public class WeblateMessageSourceTest { +class WeblateMessageSourceTest { private RestTemplate restTemplate = new RestTemplate(); @@ -28,7 +30,7 @@ public class WeblateMessageSourceTest { private MockRestServiceServer mockServer; @BeforeEach - public void init() { + void init() { messageSource = new WeblateMessageSource(); messageSource.setRestTemplate(restTemplate); messageSource.setProject("test-project"); @@ -36,7 +38,9 @@ public void init() { messageSource.setBaseUrl("http://localhost:8080"); mockServer = MockRestServiceServer.createServer(restTemplate); + } + private void mockGetLocales() { mockServer.expect(ExpectedCount.once(), requestTo("http://localhost:8080/api/projects/test-project/languages/") ).andRespond( @@ -46,15 +50,26 @@ public void init() { ); } + private void mockResponse(String body) { + try { + URI uri = new URI("http://localhost:8080/api/translations/test-project/test-comp/en/file/?q=state%3A%3E%3Dtranslated"); + DefaultResponseCreator response = withStatus(HttpStatus.OK).contentType(MediaType.TEXT_PLAIN); + if (body != null) { + response.body(body); + } + mockServer.expect(ExpectedCount.once(), + requestTo(uri)) + .andExpect(method(HttpMethod.GET)) + .andRespond(response); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Test - public void simpleCase() throws URISyntaxException { - mockServer.expect(ExpectedCount.once(), - requestTo(new URI("http://localhost:8080/api/translations/test-project/test-comp/en/file/?q=state%3A%3E%3Dtranslated"))) - .andExpect(method(HttpMethod.GET)) - .andRespond(withStatus(HttpStatus.OK) - .contentType(MediaType.TEXT_PLAIN) - .body("key1=Hello, World!\nkey2=Wow this works") - ); + void simpleCase() { + mockGetLocales(); + mockResponse("key1=Hello, World!\nkey2=Wow this works"); String key1Value = messageSource.getAllProperties(Locale.ENGLISH).getProperty("key1"); @@ -63,13 +78,9 @@ public void simpleCase() throws URISyntaxException { } @Test - public void emptyResponse() throws URISyntaxException { - mockServer.expect(ExpectedCount.once(), - requestTo(new URI("http://localhost:8080/api/translations/test-project/test-comp/en/file/?q=state%3A%3E%3Dtranslated"))) - .andExpect(method(HttpMethod.GET)) - .andRespond(withStatus(HttpStatus.OK) - .contentType(MediaType.TEXT_PLAIN) - ); + void emptyResponse() throws URISyntaxException { + mockGetLocales(); + mockResponse(null); String key1Value = messageSource.getAllProperties(Locale.ENGLISH).getProperty("key1"); @@ -77,4 +88,18 @@ public void emptyResponse() throws URISyntaxException { mockServer.verify(); } + @Test + void clearCache() { + mockGetLocales(); + mockResponse("key1=Hello, World!\nkey2=Wow this works"); + mockGetLocales(); + mockResponse("key1=Another one\nkey2=Wow this works"); + + Properties allProperties = messageSource.getAllProperties(Locale.ENGLISH); + assertEquals("Hello, World!", allProperties.get("key1")); + messageSource.clearCache(); + allProperties = messageSource.getAllProperties(Locale.ENGLISH); + assertEquals("Another one", allProperties.get("key1")); + } + }