Skip to content

Commit

Permalink
Use getVersion for spring-doc-openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielFran committed Feb 3, 2022
1 parent be0b2c0 commit c3383ee
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import tech.jhipster.lite.error.domain.GeneratorException;
import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;
Expand Down Expand Up @@ -49,8 +50,16 @@ public void initWithSecurityJWT(Project project) {

@Override
public void addSpringDocDependency(Project project) {
buildToolService.addProperty(project, "springdoc-openapi-ui.version", SpringDoc.springDocVersion());
buildToolService.addDependency(project, SpringDoc.springDocDependency());
this.buildToolService.getVersion(project, "springdoc-openapi")
.ifPresentOrElse(
version -> {
buildToolService.addProperty(project, "springdoc-openapi-ui.version", version);
buildToolService.addDependency(project, SpringDoc.springDocDependency());
},
() -> {
throw new GeneratorException("Springdoc Openapi version not found");
}
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package tech.jhipster.lite.generator.server.springboot.mvc.springdoc.domain;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;
import static tech.jhipster.lite.TestUtils.tmpProject;
import static tech.jhipster.lite.generator.server.springboot.mvc.springdoc.domain.SpringDocConstants.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import tech.jhipster.lite.UnitTest;
import tech.jhipster.lite.error.domain.GeneratorException;
import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.buildtool.generic.domain.Dependency;
import tech.jhipster.lite.generator.project.domain.Project;
Expand Down Expand Up @@ -44,10 +46,11 @@ void shouldInitWithDefaultValues() {
Project project = tmpProject();

// When
when(buildToolService.getVersion(project, "springdoc-openapi")).thenReturn(Optional.of("0.0.0"));
springDocDomainService.init(project);

// Then
verify(buildToolService).addProperty(project, "springdoc-openapi-ui.version", "1.6.4");
verify(buildToolService).addProperty(project, "springdoc-openapi-ui.version", "0.0.0");
ArgumentCaptor<Dependency> dependencyArgCaptor = ArgumentCaptor.forClass(Dependency.class);
verify(buildToolService).addDependency(eq(project), dependencyArgCaptor.capture());
assertThat(dependencyArgCaptor.getValue()).usingRecursiveComparison().isEqualTo(getExpectedDependency());
Expand Down Expand Up @@ -89,10 +92,11 @@ void shouldInitWithAdditionalConfigFromProject() {
project.getConfig().putAll(projectConfig);

// When
when(buildToolService.getVersion(project, "springdoc-openapi")).thenReturn(Optional.of("0.0.0"));
springDocDomainService.init(project);

// Then
verify(buildToolService).addProperty(project, "springdoc-openapi-ui.version", "1.6.4");
verify(buildToolService).addProperty(project, "springdoc-openapi-ui.version", "0.0.0");
ArgumentCaptor<Dependency> dependencyArgCaptor = ArgumentCaptor.forClass(Dependency.class);
verify(buildToolService).addDependency(eq(project), dependencyArgCaptor.capture());
assertThat(dependencyArgCaptor.getValue()).usingRecursiveComparison().isEqualTo(getExpectedDependency());
Expand All @@ -113,6 +117,13 @@ void shouldInitWithAdditionalConfigFromProject() {
assertThat(project.getConfig()).containsAllEntriesOf(projectConfig);
}

@Test
void shouldNotAddSpringDocDependency() {
Project project = tmpProject();

assertThatThrownBy(() -> springDocDomainService.addSpringDocDependency(project)).isExactlyInstanceOf(GeneratorException.class);
}

private static Dependency getExpectedDependency() {
return Dependency
.builder()
Expand Down

0 comments on commit c3383ee

Please sign in to comment.