I have a class that is responsible for fetching raw data from gitlab using REST.
package biz.tugay.gitlab2web2.core.dao;
import biz.tugay.gitlab2web2.core.util.RootApplicationConstants;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.nio.charset.Charset;
/**
* Should be used for fetching raw content from GitLab.
* @see <a href="https://docs.gitlab.com/ee/api/repository_files.html#get-raw-file-from-repository">GitLab Documentation</a>
*/
@Setter
@Service
@PropertySource("classpath:application.properties")
public class ArticleFromGitLabFetcherImpl implements ArticleFetcher {
@Value("${project.id}")
private String project;
@Override
public String fetchArticle(final String path) {
final String articleAbsolutePath = buildFullPath(path);
final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(articleAbsolutePath);
final URI uri = builder.build(true).toUri();
final RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
final ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);
return response.getBody();
}
private String buildFullPath(String filePath) {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(RootApplicationConstants.GITLAB_ROOT);
stringBuilder.append(project);
stringBuilder.append(RootApplicationConstants.GITLAB_FILES_PATH);
stringBuilder.append(filePath.replaceAll("/", "%2F"));
stringBuilder.append(RootApplicationConstants.GITLAB_BRANCH_MASTER);
return stringBuilder.toString();
}
}
The helper class is as follows if you are interested:
package biz.tugay.gitlab2web2.core.util;
public class RootApplicationConstants {
public static final String GITLAB_ROOT = "https://gitlab.com/api/v4/projects/";
public static final String GITLAB_TREE_PATH = "/repository/tree?path=";
public static final String GITLAB_FILES_PATH = "/repository/files/";
public static final String GITLAB_BRANCH_MASTER = "/raw?ref=master";
}
Now I want to test this class where the test class fetches itself and asserts it is equal to itself. Here is my take on it:
package biz.tugay.gitlab2web2.core.dao;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class ArticleFromGitLabFetcherImplTest {
private final ArticleFromGitLabFetcherImpl articleFromGitLabFetcher = new ArticleFromGitLabFetcherImpl();
final String expected = "package biz.tugay.gitlab2web2.core.dao;\n" +
"\n" +
"import org.junit.jupiter.api.Assertions;\n" +
"import org.junit.jupiter.api.Test;\n" +
"\n" +
"public class ArticleFromGitLabFetcherImplTest {\n" +
"\n" +
" private final ArticleFromGitLabFetcherImpl articleFromGitLabFetcher = new ArticleFromGitLabFetcherImpl();\n" +
"\n" +
" final String expected = \"\";\n" +
" \n" +
" @Test\n" +
" public void fetchSampleArticle() {\n" +
" articleFromGitLabFetcher.setProject(\"8847297\");\n" +
" final String actual = articleFromGitLabFetcher.fetchArticle(\"src/test/java/biz/tugay/gitlab2web2/core/dao/ArticleFromGitLabFetcherImplTest.java\");\n" +
" Assertions.assertEquals(expected, actual);\n" +
" }\n" +
"}";
@Test
public void fetchSampleArticle() {
articleFromGitLabFetcher.setProject("8847297");
final String actual = articleFromGitLabFetcher.fetchArticle("src/test/java/biz/tugay/gitlab2web2/core/dao/ArticleFromGitLabFetcherImplTest.java");
Assertions.assertEquals(expected, actual);
}
}
Obviously this does not work because everytime I paste the class itself to expected, the class itself also changes.
But I do not want to test this class against any other document or any other class since changing some other arbitrary class should not break my test.
Is there any way for me to test my rest fetcher to assert it is working by making use of itself?
Currently when I run my test I get:
org.opentest4j.AssertionFailedError: <Click to see difference>
Expected
package biz.tugay.gitlab2web2.core.dao;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class ArticleFromGitLabFetcherImplTest {
private final ArticleFromGitLabFetcherImpl articleFromGitLabFetcher = new ArticleFromGitLabFetcherImpl();
final String expected = "";
@Test
public void fetchSampleArticle() {
articleFromGitLabFetcher.setProject("8847297");
final String actual = articleFromGitLabFetcher.fetchArticle("src/test/java/biz/tugay/gitlab2web2/core/dao/ArticleFromGitLabFetcherImplTest.java");
Assertions.assertEquals(expected, actual);
}
}
Actual
package biz.tugay.gitlab2web2.core.dao;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class ArticleFromGitLabFetcherImplTest {
private final ArticleFromGitLabFetcherImpl articleFromGitLabFetcher = new ArticleFromGitLabFetcherImpl();
final String expected = "package biz.tugay.gitlab2web2.core.dao;\n" +
"\n" +
"import org.junit.jupiter.api.Assertions;\n" +
"import org.junit.jupiter.api.Test;\n" +
"\n" +
"public class ArticleFromGitLabFetcherImplTest {\n" +
"\n" +
" private final ArticleFromGitLabFetcherImpl articleFromGitLabFetcher = new ArticleFromGitLabFetcherImpl();\n" +
"\n" +
" final String expected = \"\";\n" +
" \n" +
" @Test\n" +
" public void fetchSampleArticle() {\n" +
" articleFromGitLabFetcher.setProject(\"8847297\");\n" +
" final String actual = articleFromGitLabFetcher.fetchArticle(\"src/test/java/biz/tugay/gitlab2web2/core/dao/ArticleFromGitLabFetcherImplTest.java\");\n" +
" Assertions.assertEquals(expected, actual);\n" +
" }\n" +
"}";
@Test
public void fetchSampleArticle() {
articleFromGitLabFetcher.setProject("8847297");
final String actual = articleFromGitLabFetcher.fetchArticle("src/test/java/biz/tugay/gitlab2web2/core/dao/ArticleFromGitLabFetcherImplTest.java");
Assertions.assertEquals(expected, actual);
}
}
I am thinking this is impossible but there are so many smart people out here so I assume no harm in asking? Is there a trick I can use?
Aucun commentaire:
Enregistrer un commentaire