Skip to content

Commit

Permalink
feat: improve test coverage - GeneratorService
Browse files Browse the repository at this point in the history
  • Loading branch information
roexber authored Oct 21, 2020
1 parent ec49d16 commit 8fe29ce
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.slf4j.Logger;
Expand All @@ -51,8 +52,7 @@ public GeneratorService(
ApplicationProperties applicationProperties,
GitService gitService,
JHipsterService jHipsterService,
LogsService logsService,
YoRCService yoRCService
LogsService logsService
) {
this.applicationProperties = applicationProperties;
this.gitService = gitService;
Expand Down Expand Up @@ -98,10 +98,8 @@ private File generateApplication(String applicationId, String applicationConfigu

private void generateYoRc(String applicationId, File workingDir, String applicationConfiguration) throws IOException {
this.logsService.addLog(applicationId, "Creating `.yo-rc.json` file");
try {
PrintWriter writer = new PrintWriter(workingDir + "/.yo-rc.json", "UTF-8");
try (PrintWriter writer = new PrintWriter(workingDir + "/.yo-rc.json", StandardCharsets.UTF_8)) {
writer.print(applicationConfiguration);
writer.close();
} catch (IOException ioe) {
log.error("Error creating file .yo-rc.json", ioe);
throw ioe;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.github.jhipster.online.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.willReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import io.github.jhipster.online.config.ApplicationProperties;
import io.github.jhipster.online.domain.User;
import io.github.jhipster.online.domain.enums.GitProvider;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@EnableConfigurationProperties(value = ApplicationProperties.class)
@TestPropertySource("classpath:config/application.yml")
class GeneratorServiceTest {

private final String applicationId = "app-id";
private final String applicationConfiguration = "app-config";

@Autowired
private ApplicationProperties applicationProperties;

@Mock
private GitService gitService;

@Mock
private JHipsterService jHipsterService;

@Mock
private LogsService logsService;

private GeneratorService generatorService;

@BeforeEach
void shouldConstructGeneratorService() {
generatorService = new GeneratorService(applicationProperties, gitService, jHipsterService, logsService);
}

@Test
void generateZippedApplication() throws IOException {
String result = generatorService.generateZippedApplication(applicationId, applicationConfiguration);

assertThat(result).isEqualTo("/tmp/jhipster/applications/app-id.zip");
verify(logsService).addLog(applicationId, "Creating `.yo-rc.json` file");
verify(jHipsterService).generateApplication(applicationId, new File("/tmp/jhipster/applications/app-id"));
assertThat(new File("/tmp/jhipster/applications/app-id/.yo-rc.json")).isFile().hasContent(applicationConfiguration);
}

@Test
void generateGitApplication() throws GitAPIException, IOException, URISyntaxException {
User user = mock(User.class);
File workingDir = new File("/tmp/jhipster/applications/app-id");

generatorService.generateGitApplication(user, applicationId, applicationConfiguration, "gh-org", "repo", GitProvider.GITHUB);

verify(logsService).addLog(applicationId, "Creating `.yo-rc.json` file");
verify(jHipsterService).generateApplication(applicationId, new File("/tmp/jhipster/applications/app-id"));
assertThat(new File("/tmp/jhipster/applications/app-id/.yo-rc.json")).isFile().hasContent(applicationConfiguration);
verify(logsService).addLog(applicationId, "Pushing the application to the Git remote repository");
verify(gitService).pushNewApplicationToGit(user, workingDir, "gh-org", "repo", GitProvider.GITHUB);
verify(logsService).addLog(applicationId, "Application successfully pushed!");
verify(gitService).cleanUpDirectory(workingDir);
}
}

0 comments on commit 8fe29ce

Please sign in to comment.