Skip to content

Commit

Permalink
testing: consolidate builder classes
Browse files Browse the repository at this point in the history
Motivation:

To avoid repetition (make code DRY) we should have a single
FileRequestsBuilder class and a single StorageInfoBuilder class.

Modification:

Combine the different FileAttributesBuilder classes into a single class.

Rename some methods to following the normal builder pattern.

Remove unused StorageInfoBuilder class.

Results:

No user or admin observable changes.  Slightly less code.

Target: master
Requires-notes: no
Requires-book: no
Patch: https://rb.dcache.org/r/13143/
Acked-by: Tigran Mkrtchyan
  • Loading branch information
paulmillar committed Aug 25, 2021
1 parent 184c826 commit 6f69d12
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,78 @@
*/
package org.dcache.util;

import java.util.Collections;
import java.util.Set;

import diskCacheV111.util.PnfsId;
import diskCacheV111.vehicles.StorageInfo;

import org.dcache.namespace.FileType;
import org.dcache.vehicles.FileAttributes;

/**
* A fluent class to build a FileAttribute.
* A fluent class to build a FileAttributes object.
*/
public class FileAttributesBuilder
{
private final FileAttributes _attributes = new FileAttributes();

public static FileAttributesBuilder attributes()
public static FileAttributesBuilder fileAttributes()
{
return new FileAttributesBuilder();
}

public FileAttributesBuilder size(long size)
public FileAttributesBuilder withSize(long size)
{
_attributes.setSize(size);
return this;
}

public FileAttributesBuilder size(long size, ByteUnit units)
public FileAttributesBuilder withSize(long size, ByteUnit units)
{
return withSize(units.toBytes(size));
}

public FileAttributesBuilder withLabel(String name)
{
return withLabels(Collections.singleton(name));
}

public FileAttributesBuilder withLabels(Set<String> names)
{
return size(units.toBytes(size));
_attributes.setLabels(names);
return this;
}

public FileAttributesBuilder type(FileType type)
public FileAttributesBuilder withType(FileType type)
{
_attributes.setFileType(type);
return this;
}

public FileAttributesBuilder id(PnfsId id)
public FileAttributesBuilder withId(PnfsId id)
{
_attributes.setPnfsId(id);
return this;
}

public FileAttributesBuilder storageInfo(StorageInfoBuilder builder)
public FileAttributesBuilder withStorageInfo(StorageInfoBuilder builder)
{
return storageInfo(builder.build());
return withStorageInfo(builder.build());
}

public FileAttributesBuilder storageInfo(StorageInfo info)
public FileAttributesBuilder withStorageInfo(StorageInfo info)
{
_attributes.setStorageInfo(info);
return this;
}

public FileAttributesBuilder withXattr(String name, String value)
{
_attributes.updateXattr(name, value);
return this;
}

public FileAttributes build()
{
return _attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import java.util.Map;
import java.util.Optional;

import org.dcache.util.FileAttributesBuilder;

import static org.dcache.namespace.FileAttribute.XATTR;
import static org.dcache.util.FileAttributesBuilder.fileAttributes;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
Expand Down Expand Up @@ -92,25 +95,4 @@ private void given(FileAttributesBuilder builder)
{
fileAttributes = builder.build();
}

private FileAttributesBuilder fileAttributes()
{
return new FileAttributesBuilder();
}

private static class FileAttributesBuilder
{
FileAttributes attributes = new FileAttributes();

public FileAttributesBuilder withXattr(String name, String value)
{
attributes.updateXattr(name, value);
return this;
}

public FileAttributes build()
{
return attributes;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@

import org.junit.Test;

import java.util.Collections;
import java.util.Set;

import org.dcache.util.FileAttributesBuilder;

import static org.dcache.namespace.FileAttribute.LABELS;
import static org.dcache.util.FileAttributesBuilder.fileAttributes;
import static org.junit.Assert.*;

public class FileLabelsTest
Expand Down Expand Up @@ -68,33 +70,4 @@ private void given(FileAttributesBuilder builder)
{
fileAttributes = builder.build();
}


private FileAttributesBuilder fileAttributes()
{
return new FileAttributesBuilder();

}

private static class FileAttributesBuilder
{
FileAttributes attributes = new FileAttributes();
public FileAttributesBuilder withLabel(String name)
{
attributes.setLabels(Collections.singleton(name));
return this;
}


public FileAttributesBuilder withLabels(Set<String> names)
{
attributes.setLabels(names);
return this;
}

public FileAttributes build()
{
return attributes;
}
}
}
Loading

0 comments on commit 6f69d12

Please sign in to comment.