GitConfigResourceTest: Realizes missing asserts

This commit is contained in:
Johannes Schnatterer
2018-08-01 15:34:17 +02:00
parent fc77ae3a47
commit 86af96bd83

View File

@@ -1,5 +1,7 @@
package sonia.scm.api.v2.resources;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.sdorra.shiro.ShiroRule;
import com.github.sdorra.shiro.SubjectAware;
import org.jboss.resteasy.core.Dispatcher;
@@ -16,10 +18,10 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import sonia.scm.repository.GitConfig;
import sonia.scm.repository.GitRepositoryHandler;
import sonia.scm.store.ConfigurationStoreFactory;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
@@ -52,38 +54,39 @@ public class GitConfigResourceTest {
private GitConfigToGitConfigDtoMapperImpl configToDtoMapper;
@Mock
ConfigurationStoreFactory storeFactory;
@InjectMocks
private GitRepositoryHandler repositoryHandler;
@Before
public void prepareEnvironment() {
GitConfig gitConfig = createConfiguration();
repositoryHandler.setConfig(gitConfig);
when(repositoryHandler.getConfig()).thenReturn(gitConfig);
GitConfigResource gitConfigResource = new GitConfigResource(dtoToConfigMapper, configToDtoMapper, repositoryHandler);
dispatcher.getRegistry().addSingletonResource(gitConfigResource);
when(uriInfoStore.get().getBaseUri()).thenReturn(baseUri);
}
@Test
public void shouldGetGitConfig() throws URISyntaxException {
public void shouldGetGitConfig() throws URISyntaxException, IOException {
MockHttpRequest request = MockHttpRequest.get("/" + GitConfigResource.GIT_CONFIG_PATH_V2);
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertTrue(response.getContentAsString().contains("\"disabled\":false"));
//assertTrue(response.getContentAsString().contains("\"repository-directory\":\"repository/directory\""));
//assertTrue(response.getContentAsString().contains("\"gc-expression\":\"valid Git GC Cron Expression\""));
assertTrue(response.getContentAsString().contains("\"self\":{\"href\":\"/v2/config/git"));
assertTrue(response.getContentAsString().contains("\"update\":{\"href\":\"/v2/config/git"));
String responseString = response.getContentAsString();
ObjectNode responseJson = new ObjectMapper().readValue(responseString, ObjectNode.class);
assertTrue(responseString.contains("\"disabled\":false"));
assertTrue(responseJson.get("repositoryDirectory").asText().endsWith("repository/directory"));
assertTrue(responseString.contains("\"gcExpression\":\"valid Git GC Cron Expression\""));
assertTrue(responseString.contains("\"self\":{\"href\":\"/v2/config/git"));
assertTrue(responseString.contains("\"update\":{\"href\":\"/v2/config/git"));
}
// TODO negative tests
// TODO update & negative tests
private GitConfig createConfiguration() {
GitConfig config = new GitConfig();
//config.setGcExpression("valid Git GC Cron Expression");
config.setGcExpression("valid Git GC Cron Expression");
config.setDisabled(false);
config.setRepositoryDirectory(new File("repository/directory"));
return config;