Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set preset in Spring Config #10610

Merged

Conversation

pascalgrimaud
Copy link
Member

@renanfranca
Copy link
Contributor

@pascalgrimaud : A error was detected with custom-jhlite module, what do you think about this fix?
#10609 (comment)

This must be done in this same PR.

@pascalgrimaud
Copy link
Member Author

@renanfranca : I tested locally jhlite custom with the following code here and it works, as I used default override mechanism by Spring, so the property will be set with the default value preset.json

@pascalgrimaud pascalgrimaud force-pushed the 10553/preset-in-spring-config branch from b9a5881 to 21da0d8 Compare August 19, 2024 15:35
@renanfranca
Copy link
Contributor

@renanfranca : I tested locally jhlite custom with the following code here and it works, as I used default override mechanism by Spring, so the property will be set with the default value preset.json

Nice! Thanks!

@pascalgrimaud : If you change the property at application.yml the cucumber test will fail. To avoid that the class FakedFileSystemProjectFilesConfiguration.java should be change:

the line to change:

from:
Mockito.doReturn(presetJsonContent.getBytes(StandardCharsets.UTF_8)).when(fileSystemProjectFiles).readBytes("/preset.json");

to:
Mockito.doReturn(presetJsonContent.getBytes(StandardCharsets.UTF_8)).when(fileSystemProjectFiles).readBytes(anyString());

The class without and with the change:

from:

ackage tech.jhipster.lite.project.infrastructure.secondary;

import java.nio.charset.StandardCharsets;
import org.mockito.Mockito;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import tech.jhipster.lite.module.infrastructure.secondary.FileSystemProjectFiles;

@TestConfiguration
public class FakedFileSystemProjectFilesConfiguration {

  @Bean
  @Primary
  FileSystemProjectFiles fileSystemProjectFiles() {
    FileSystemProjectFiles fileSystemProjectFiles = Mockito.spy(new FileSystemProjectFiles());

    mockPresetJson(fileSystemProjectFiles);

    return fileSystemProjectFiles;
  }

  private static void mockPresetJson(FileSystemProjectFiles fileSystemProjectFiles) {
    String presetJsonContent =
      """
      {
        "presets": [
          {
            "name": "test preset one",
            "modules": ["test-module-one", "test-module-two"]
          },
          {
            "name": "test preset two",
            "modules": ["test-module-three", "test-module-four"]
          }
        ]
      }
      """;

    Mockito.doReturn(presetJsonContent.getBytes(StandardCharsets.UTF_8)).when(fileSystemProjectFiles).readBytes("/preset.json");
  }
}

to:

package tech.jhipster.lite.project.infrastructure.secondary;

import static org.mockito.ArgumentMatchers.*;

import java.nio.charset.StandardCharsets;
import org.mockito.Mockito;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import tech.jhipster.lite.module.infrastructure.secondary.FileSystemProjectFiles;

@TestConfiguration
public class FakedFileSystemProjectFilesConfiguration {

  @Bean
  @Primary
  FileSystemProjectFiles fileSystemProjectFiles() {
    FileSystemProjectFiles fileSystemProjectFiles = Mockito.spy(new FileSystemProjectFiles());

    mockPresetJson(fileSystemProjectFiles);

    return fileSystemProjectFiles;
  }

  private static void mockPresetJson(FileSystemProjectFiles fileSystemProjectFiles) {
    String presetJsonContent =
      """
      {
        "presets": [
          {
            "name": "test preset one",
            "modules": ["test-module-one", "test-module-two"]
          },
          {
            "name": "test preset two",
            "modules": ["test-module-three", "test-module-four"]
          }
        ]
      }
      """;

    Mockito.doReturn(presetJsonContent.getBytes(StandardCharsets.UTF_8)).when(fileSystemProjectFiles).readBytes(anyString());
  }
}

@pascalgrimaud
Copy link
Member Author

@renanfranca : nice catch and thanks. See my last commit, we should not need to apply this additional code about Cucumber

@renanfranca
Copy link
Contributor

@renanfranca : I tested locally jhlite custom with the following code here and it works, as I used default override mechanism by Spring, so the property will be set with the default value preset.json

Nice! Thanks!

@pascalgrimaud : If you change the property at application.yml the cucumber test will fail. To avoid that the class FakedFileSystemProjectFilesConfiguration.java should be change:

the line to change:

from: Mockito.doReturn(presetJsonContent.getBytes(StandardCharsets.UTF_8)).when(fileSystemProjectFiles).readBytes("/preset.json");

to: Mockito.doReturn(presetJsonContent.getBytes(StandardCharsets.UTF_8)).when(fileSystemProjectFiles).readBytes(anyString());

The class without and with the change:

from:

ackage tech.jhipster.lite.project.infrastructure.secondary;

import java.nio.charset.StandardCharsets;
import org.mockito.Mockito;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import tech.jhipster.lite.module.infrastructure.secondary.FileSystemProjectFiles;

@TestConfiguration
public class FakedFileSystemProjectFilesConfiguration {

  @Bean
  @Primary
  FileSystemProjectFiles fileSystemProjectFiles() {
    FileSystemProjectFiles fileSystemProjectFiles = Mockito.spy(new FileSystemProjectFiles());

    mockPresetJson(fileSystemProjectFiles);

    return fileSystemProjectFiles;
  }

  private static void mockPresetJson(FileSystemProjectFiles fileSystemProjectFiles) {
    String presetJsonContent =
      """
      {
        "presets": [
          {
            "name": "test preset one",
            "modules": ["test-module-one", "test-module-two"]
          },
          {
            "name": "test preset two",
            "modules": ["test-module-three", "test-module-four"]
          }
        ]
      }
      """;

    Mockito.doReturn(presetJsonContent.getBytes(StandardCharsets.UTF_8)).when(fileSystemProjectFiles).readBytes("/preset.json");
  }
}

to:

package tech.jhipster.lite.project.infrastructure.secondary;

import static org.mockito.ArgumentMatchers.*;

import java.nio.charset.StandardCharsets;
import org.mockito.Mockito;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import tech.jhipster.lite.module.infrastructure.secondary.FileSystemProjectFiles;

@TestConfiguration
public class FakedFileSystemProjectFilesConfiguration {

  @Bean
  @Primary
  FileSystemProjectFiles fileSystemProjectFiles() {
    FileSystemProjectFiles fileSystemProjectFiles = Mockito.spy(new FileSystemProjectFiles());

    mockPresetJson(fileSystemProjectFiles);

    return fileSystemProjectFiles;
  }

  private static void mockPresetJson(FileSystemProjectFiles fileSystemProjectFiles) {
    String presetJsonContent =
      """
      {
        "presets": [
          {
            "name": "test preset one",
            "modules": ["test-module-one", "test-module-two"]
          },
          {
            "name": "test preset two",
            "modules": ["test-module-three", "test-module-four"]
          }
        ]
      }
      """;

    Mockito.doReturn(presetJsonContent.getBytes(StandardCharsets.UTF_8)).when(fileSystemProjectFiles).readBytes(anyString());
  }
}

@pascalgrimaud

If you change the property at application.yml the cucumber test will fail. To avoid that the class FakedFileSystemProjectFilesConfiguration.java should be change:

the line to change:

from: Mockito.doReturn(presetJsonContent.getBytes(StandardCharsets.UTF_8)).when(fileSystemProjectFiles).readBytes("/preset.json");

to: Mockito.doReturn(presetJsonContent.getBytes(StandardCharsets.UTF_8)).when(fileSystemProjectFiles).readBytes(anyString());

@pascalgrimaud : please, hold on. Don't implement that change, it will break a lot of other tests and won't fix the problem when the preset-filename.name is changed!

I don't have a solution right now from the top of my head.

@renanfranca
Copy link
Contributor

@renanfranca : nice catch and thanks. See my last commit, we should not need to apply this additional code about Cucumber

@pascalgrimaud : Greate! Thanks for the solution, I didn't see that option implement the code in my head 😅

Copy link
Contributor

@renanfranca renanfranca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pascalgrimaud for your work! 😃

@pascalgrimaud pascalgrimaud merged commit 55aaffe into jhipster:main Aug 19, 2024
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

preset.json in spring config
2 participants