new GetCreateIssueMetadataOptionsBuilder().withProjectKeys("TST").withExpandedIssueTypesFields().build()).claim();
// select project and issue
assertEquals(1, Iterables.size(metadataProjects));
final CimProject project = metadataProjects.iterator().next();
final CimIssueType issueType = findEntityByName(project.getIssueTypes(), "Sub-task");
// grab the first component
final Iterable<Object> allowedValuesForComponents = issueType.getField(IssueFieldId.COMPONENTS_FIELD).getAllowedValues();
assertNotNull(allowedValuesForComponents);
assertTrue(allowedValuesForComponents.iterator().hasNext());
final BasicComponent component = (BasicComponent) allowedValuesForComponents.iterator().next();
// grab the first priority
final Iterable<Object> allowedValuesForPriority = issueType.getField(IssueFieldId.PRIORITY_FIELD).getAllowedValues();
assertNotNull(allowedValuesForPriority);
assertTrue(allowedValuesForPriority.iterator().hasNext());
final BasicPriority priority = (BasicPriority) allowedValuesForPriority.iterator().next();
// build issue input
final String description = "Some description for substask";
final BasicUser assignee = IntegrationTestUtil.USER1;
final List<String> affectedVersionsNames = Collections.emptyList();
final DateTime dueDate = new DateTime(new Date().getTime());
final ArrayList<String> fixVersionsNames = Lists.newArrayList("1.1");
final Set<String> summaries = ImmutableSet.of("Summary 1","Summary 2","Summary 3","Summary 4","Summary 5");
final Set<String> summariesWithError = ImmutableSet.of("Summary 1", "Summary 4");
final Set<String> expectedSummariesOrder = Sets.difference(summaries, summariesWithError);
final int issuecToCreateCount = summaries.size() - summariesWithError.size();
final int issuesInErrorCount = summariesWithError.size();
final List<IssueInput> issuesToCreate = Lists.newArrayList();
// prepare IssueInput
for (final String summary : summaries) {
String currentProjectKey = project.getKey();
//last issue to create will have a non existing project - to simulate creation error
if (summariesWithError.contains(summary)) {
currentProjectKey = "FAKE_KEY";
}
final IssueInputBuilder issueInputBuilder =
new IssueInputBuilder(currentProjectKey, issueType.getId(), summary)
.setDescription(description)
.setAssignee(assignee)
.setAffectedVersionsNames(affectedVersionsNames)
.setFixVersionsNames(fixVersionsNames)
.setComponents(component)
.setDueDate(dueDate)
.setPriority(priority)
.setFieldValue("parent", ComplexIssueInputFieldValue.with("key", "TST-1"));
issuesToCreate.add(issueInputBuilder.build());
}
assertEquals(summaries.size(), issuesToCreate.size());
// create
final BulkOperationResult<BasicIssue> createdIssues = issueClient.createIssues(issuesToCreate).claim();
assertEquals(issuecToCreateCount, Iterables.size(createdIssues.getIssues()));
assertEquals(issuesInErrorCount, Iterables.size(createdIssues.getErrors()));
//check order
final Set<String> createdSummariesOrder = ImmutableSet.copyOf(Iterables.transform(createdIssues
.getIssues(), new Function<BasicIssue, String>() {
@Override
public String apply(final BasicIssue basicIssue) {
return issueClient.getIssue(basicIssue.getKey()).claim().getSummary();
}
}));
assertEquals(expectedSummariesOrder, createdSummariesOrder);
final Issue parentIssue = issueClient.getIssue("TST-1").claim();
final Set<String> subtaskKeys = ImmutableSet.copyOf(Iterables.transform(parentIssue
.getSubtasks(), new Function<Subtask, String>() {
@Override
public String apply(Subtask subtask) {
return subtask.getIssueKey();
}
}));
for (final BasicIssue basicIssue : createdIssues.getIssues()) {
// get issue and check if everything was set as we expected
final Issue createdIssue = issueClient.getIssue(basicIssue.getKey()).claim();
assertNotNull(createdIssue);
assertEquals(basicIssue.getKey(), createdIssue.getKey());
assertEquals(project.getKey(), createdIssue.getProject().getKey());
assertEquals(issueType.getId(), createdIssue.getIssueType().getId());
assertEquals( description, createdIssue.getDescription());
final BasicUser actualAssignee = createdIssue.getAssignee();
assertNotNull(actualAssignee);
assertEquals(assignee.getSelf(), actualAssignee.getSelf());