Usage Examples

OBJECTO

Declaration

import com.cariochi.objecto.TypeFactory;
import com.cariochi.objecto.FieldFactory;
import com.cariochi.objecto.Constructor;
import com.cariochi.objecto.Modifier;
import com.cariochi.objecto.DatafakerMethod;
import com.cariochi.objecto.Settings;
import com.cariochi.objecto.model.Issue;
import com.cariochi.objecto.model.Issue.Status;
import com.cariochi.objecto.model.Issue.Type;
import com.cariochi.objecto.model.User;
import java.util.List;
import java.util.Random;
import net.datafaker.Faker;

@Settings.MaxRecursionDepth(3)
public interface IssueFactory extends BaseUserGenerators {

    @TypeFactory
    @References("subtasks[*].parent")
    Issue createIssue();

    Issue createIssue(@Modifier("type=?") Type type);
    
    List<Issue> createIssues();
    
    List<Issue> createIssues(@Modifier("type=?") Type type);
    
    @Constructor
    private Attachment<?> newAttachment() {
        return Attachment.builder().fileContent(new byte[0]).build();
    }
    
    @FieldFactory(type = Issue.class, field = "key")
    private String issueKeyGenerator(Random random) {
        return "ID-" + random.nextInt(1000, 10000);
    }
    
    @Modifier("key=?")
    IssueFactory withKey(String key);

    @Modifier("setType(?)")
    IssueFactory withType(Type type);

    @Modifier("setStatus(?)")
    IssueFactory withStatus(Status status);

    @Modifier("assignee=?")
    IssueFactory withAssignee(User assignee);
    
    @Modifier("subtasks[*].setStatus(?)") 
    IssueFactory withAllSubtaskStatuses(Status status);

    @Modifier("comments[0].commenter=?")
    IssueFactory withFirstCommenter(User commenter);
    
    @Modifier("dependencies.put(?, ?)")
    IssueFactory withDependency(DependencyType type, Issue issue);

}
import com.cariochi.issuestest.model.User;
import com.cariochi.objecto.PostProcessor;
import net.datafaker.Faker;

import static org.apache.commons.lang3.StringUtils.lowerCase;
import static org.apache.commons.lang3.StringUtils.replace;

public interface BaseUserGenerators {

    @PostProcessor
    private void userPostProcessor(User user) {
        final String username = replace(replace(lowerCase(user.getFullName()), ".", ""), " ", ".");
        user.setUsername(username);
        user.setEmail(username + "@" + new Faker().internet().domainName());
    }

}

Usage

// Creating a factory for generating Issue objects
IssueFactory issueFactory = Objecto.create(IssueFactory.class);

// Generating a random Issue object
Issue randomIssue = issueFactory.createIssue();

// Generating an Issue object with specific parameters
Issue randomBug = issueFactory.createIssue(Type.BUG);

// Using modifier methods for easy modification
Issue randomOpenBug = issueFactory
                          .withType(Type.BUG)
                          .withStatus(Status.OPEN)
                          .withAssignee(user)
                          .withAllSubtaskStatuses(Status.IN_PROGRESS)
                          .createIssue();

// Generating list of random issues with specific parameters
List<Issue> bugList = issueFactory.createIssues(Type.BUG);

// Using modifier methods
List<Issue> openBugList = issueFactory
                            .withType(Type.BUG)
                            .withStatus(Status.OPEN)
                            .withwithDependency(DependencyType.BLOCK, randomBug)
                            .createIssues();

Last updated