diff --git a/src/main/java/tech/jhipster/lite/generator/client/angular/common/domain/AngularCommon.java b/src/main/java/tech/jhipster/lite/generator/client/angular/common/domain/AngularCommon.java index 7b17ad5bc72..1463f6b2e4c 100644 --- a/src/main/java/tech/jhipster/lite/generator/client/angular/common/domain/AngularCommon.java +++ b/src/main/java/tech/jhipster/lite/generator/client/angular/common/domain/AngularCommon.java @@ -8,7 +8,7 @@ public class AngularCommon { protected static final List DECORATOR_REGEX_LIST = List.of("(^@[A-Z]{1}[\\w]+\\(\\{)"); - protected static final String DECLARATIONS_REGEX = "^([\\s]+declarations:[\\s]+\\[[^\\]]+\\])"; + protected static final String DECLARATIONS_REGEX = "^([\\s]+declarations:[\\s]+\\[[^\\]]+\\][\\s,]*)"; protected static final List DECLARATIONS_WITH_ARRAY_VALUES_REGEX_LIST = List.of( "^[\\s]+(declarations:[\\s]+\\[[^\\]]+)\\]", @@ -25,8 +25,8 @@ public class AngularCommon { ); protected static final String COMA = ","; - - protected static final String OPENING_BRACKET = "["; + protected static final String O_BRACKET = "["; + protected static final String C_BRACKET = "]"; private AngularCommon() { // Cannot be instantiated diff --git a/src/main/java/tech/jhipster/lite/generator/client/angular/common/domain/AngularCommonDomainService.java b/src/main/java/tech/jhipster/lite/generator/client/angular/common/domain/AngularCommonDomainService.java index 481fb155204..70fea8c47d1 100644 --- a/src/main/java/tech/jhipster/lite/generator/client/angular/common/domain/AngularCommonDomainService.java +++ b/src/main/java/tech/jhipster/lite/generator/client/angular/common/domain/AngularCommonDomainService.java @@ -2,11 +2,12 @@ import static tech.jhipster.lite.common.domain.WordUtils.indent; import static tech.jhipster.lite.generator.client.angular.common.domain.AngularCommon.COMA; +import static tech.jhipster.lite.generator.client.angular.common.domain.AngularCommon.C_BRACKET; import static tech.jhipster.lite.generator.client.angular.common.domain.AngularCommon.DECLARATIONS_REGEX; import static tech.jhipster.lite.generator.client.angular.common.domain.AngularCommon.DECLARATIONS_WITH_ARRAY_VALUES_REGEX_LIST; import static tech.jhipster.lite.generator.client.angular.common.domain.AngularCommon.DECORATOR_REGEX_LIST; import static tech.jhipster.lite.generator.client.angular.common.domain.AngularCommon.ENV_VARIABLES_WITH_VALUES_REGEX_LIST; -import static tech.jhipster.lite.generator.client.angular.common.domain.AngularCommon.OPENING_BRACKET; +import static tech.jhipster.lite.generator.client.angular.common.domain.AngularCommon.O_BRACKET; import static tech.jhipster.lite.generator.client.angular.common.domain.AngularCommon.PROVIDERS_WITH_ARRAY_VALUES_REGEX_LIST; import java.io.IOException; @@ -139,7 +140,7 @@ private Optional getFirstMatchInFile(List regexList, String file private String appendValuesInArray(String fileContent, String prefixWithArrayValuesStr, String arrayValuesToAdd, String endOfLine) { StringBuilder updatedArrayValuesStr = new StringBuilder(prefixWithArrayValuesStr.stripTrailing()); - if (!prefixWithArrayValuesStr.trim().endsWith(OPENING_BRACKET) && !updatedArrayValuesStr.toString().endsWith(COMA)) { + if (!prefixWithArrayValuesStr.trim().endsWith(O_BRACKET) && !updatedArrayValuesStr.toString().endsWith(COMA)) { updatedArrayValuesStr.append(COMA); } updatedArrayValuesStr.append(endOfLine).append(arrayValuesToAdd.stripTrailing()).append(endOfLine).append(indent(1)); @@ -148,9 +149,18 @@ private String appendValuesInArray(String fileContent, String prefixWithArrayVal private String appendProvidersAfterDeclarations(String fileContent, String fullFilePath, String providers, Project project) { String declarationsStr = getFirstMatchInFile(List.of(DECLARATIONS_REGEX), fileContent) + .map(String::stripTrailing) .orElseThrow(() -> new GeneratorException("Missing declarations in file: " + fullFilePath)); String newProvidersArrayStr = - indent(1) + "providers: [" + project.getEndOfLine() + providers.stripTrailing() + project.getEndOfLine() + indent(1) + "]"; + indent(1) + + "providers: " + + O_BRACKET + + project.getEndOfLine() + + providers.stripTrailing() + + project.getEndOfLine() + + indent(1) + + C_BRACKET + + COMA; String declarationsAndProvidersStr = declarationsStr.trim().endsWith(COMA) ? declarationsStr : declarationsStr + COMA; declarationsAndProvidersStr += project.getEndOfLine() + newProvidersArrayStr; return fileContent.replace(declarationsStr, declarationsAndProvidersStr); diff --git a/src/test/java/tech/jhipster/lite/generator/client/angular/common/domain/AngularCommonDomainServiceTest.java b/src/test/java/tech/jhipster/lite/generator/client/angular/common/domain/AngularCommonDomainServiceTest.java index 2819d152887..0c65ca662af 100644 --- a/src/test/java/tech/jhipster/lite/generator/client/angular/common/domain/AngularCommonDomainServiceTest.java +++ b/src/test/java/tech/jhipster/lite/generator/client/angular/common/domain/AngularCommonDomainServiceTest.java @@ -459,7 +459,7 @@ private static Stream contentFileInputsProvider() { declarations: [AppComponent], providers: [ { provide: HTTP_INTERCEPTORS, useClass: HttpAuthInterceptor, multi: true, }, - ] + ], }) """, "New providers array after declarations (without comma)" @@ -532,6 +532,24 @@ void shouldAddProviders(String inputFile, String expectedContentFile, @SuppressW } } + @Test + void shouldNotAddWhenProvidersAndDeclarationsNotFoundInFile() { + // Given + Project project = Project.builder().folder("/project/path").build(); + String filePath = "file/path"; + String providers = "{ provide: HTTP_INTERCEPTORS, useClass: HttpAuthInterceptor, multi: true, }"; + try (MockedStatic fileUtils = mockStatic(FileUtils.class)) { + String fullFilePath = "/project/path/file/path"; + fileUtils.when(() -> FileUtils.getPath("/project/path", filePath)).thenReturn(fullFilePath); + fileUtils.when(() -> FileUtils.read(fullFilePath)).thenReturn(""); + + // When + Then + assertThatThrownBy(() -> angularCommonDomainService.addProviders(project, filePath, providers)) + .isInstanceOf(GeneratorException.class) + .hasMessageContaining(fullFilePath); + } + } + @Test void shouldNotAddWhenFileCannotBeRead() { // Given @@ -573,7 +591,7 @@ void shouldNotAddWhenFileCannotBeWritten() { fileUtils.when(() -> FileUtils.write(anyString(), anyString(), anyString())).thenThrow(IOException.class); // When + Then - assertThatThrownBy(() -> angularCommonDomainService.addDeclarations(project, filePath, providers)) + assertThatThrownBy(() -> angularCommonDomainService.addProviders(project, filePath, providers)) .isInstanceOf(GeneratorException.class) .hasMessageContaining(fullFilePath); }