Skip to content

Enhancement: Add support to configure if the method suffix should be used for created JSON file name #96

Description

@skrilagg

Hi all,

I am using version 1.1.0 and therefore had to add some functionality, that in parts is already added for version 2.x.

Part of this is the ability to configure a base repository for stored files, which I just noticed you already implemented for the next version.
Another addition I added is the ability to configure if the name of the testMethod should be used in the generated json file name.

Let me explain, why I added this:

  • When creating deepsampler tests I start with a SaveSamples-Test with the name of the service method for example.
  • After that I create another LoadSamples-Test which and comment out the Test Annotation on the LoadSamples method.
  • The default behaviour now would be that I have to either give both methods the same name or set the source name on the LoadSamples-Test everytime.

For convenience I added another configuration value to the Annotation UseSamplerFixture, which is then used inside the JUnitPluginUtils. Let me show you with the following:

public static void saveSamples(final Method testMethod) {
	final SaveSamples saveSamples = testMethod.getAnnotation(SaveSamples.class);

	if (saveSamples == null) {
		return;
	}

	final JsonSourceManager.Builder persistentSampleManagerBuilder = loadBuilder(
			saveSamples.persistenceManagerProvider());

	final String fileName = getFilePath(saveSamples, getFixture(testMethod), testMethod);

	PersistentSamplerHolder.source(persistentSampleManagerBuilder.buildWithFile(fileName));

	addExtensions(testMethod);

	PersistentSamplerHolder.getSampler().record();
}

private static String getFilePath(LoadSamples loadSamples, UseSamplerFixture fixture, Method testMethod) {
	return getFilePath(loadSamples.file(), fixture, testMethod);
}

private static String getFilePath(SaveSamples saveSamples, UseSamplerFixture fixture, Method testMethod) {
	return getFilePath(saveSamples.file(), fixture, testMethod);
}

private static String getFilePath(String path, UseSamplerFixture fixture, Method testMethod) {
	String basePath = fixture != null ? fixture.repositoryBase() : "";
	String file = StringUtils.defaultIfEmpty(path,
			getDefaultJsonFileNameWithFolder(testMethod, withMethod(fixture)));

	return Paths.get(basePath, file).toString();
}

private static boolean withMethod(final UseSamplerFixture fixture) {
	return Optional.ofNullable(fixture).map(anno -> anno.withMethod()).orElse(false);
}

private static String getDefaultJsonFileNameWithFolder(final Method testMethod, boolean withMethod) {
	return testMethod.getDeclaringClass().getName().replace(".", "/")
			+ (withMethod ? ("_" + testMethod.getName()) : "") + ".json";
}

Let me hear your thoughts on this.

Cheers,
Michel

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions