Skip to content

Commit

Permalink
Add angular common service / fix regex - add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bolo89 committed May 13, 2022
1 parent 084cccd commit 80ad802
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class AngularCommon {

protected static final List<String> 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<String> DECLARATIONS_WITH_ARRAY_VALUES_REGEX_LIST = List.of(
"^[\\s]+(declarations:[\\s]+\\[[^\\]]+)\\]",
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -139,7 +140,7 @@ private Optional<String> getFirstMatchInFile(List<String> 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));
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ private static Stream<Arguments> contentFileInputsProvider() {
declarations: [AppComponent],
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: HttpAuthInterceptor, multi: true, },
]
],
})
""",
"New providers array after declarations (without comma)"
Expand Down Expand Up @@ -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> 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
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 80ad802

Please sign in to comment.