Skip to content

Commit

Permalink
chore: add test for clearCache
Browse files Browse the repository at this point in the history
  • Loading branch information
derkoe committed Jan 23, 2022
1 parent 09f1d94 commit e29e095
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
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;
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.withStatus;

public class WeblateMessageSourceTest {
class WeblateMessageSourceTest {

private RestTemplate restTemplate = new RestTemplate();

Expand All @@ -28,15 +30,17 @@ public class WeblateMessageSourceTest {
private MockRestServiceServer mockServer;

@BeforeEach
public void init() {
void init() {
messageSource = new WeblateMessageSource();
messageSource.setRestTemplate(restTemplate);
messageSource.setProject("test-project");
messageSource.setComponent("test-comp");
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(
Expand All @@ -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");

Expand All @@ -63,18 +78,28 @@ 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");

assertNull(key1Value);
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"));
}

}

0 comments on commit e29e095

Please sign in to comment.