Search in sources :

Example 1 with CustomFieldElement

use of com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldElement in project synopsys-detect by blackducksoftware.

the class UpdateCustomFieldsOperation method pairOperationFromViews.

private List<CustomFieldOperation> pairOperationFromViews(List<CustomFieldElement> elements, List<CustomFieldView> views, String targetName, BlackDuckApiClient blackDuckService) throws DetectUserFriendlyException {
    List<CustomFieldOperation> operations = new ArrayList<>();
    for (CustomFieldElement element : elements) {
        Optional<CustomFieldView> fieldView = views.stream().filter(view -> view.getLabel().equals(element.getLabel())).findFirst();
        if (!fieldView.isPresent()) {
            throw new DetectUserFriendlyException(String.format("Unable to find custom field view with label '%s' on the %s. Ensure it exists.", element.getLabel(), targetName), ExitCodeType.FAILURE_BLACKDUCK_FEATURE_ERROR);
        }
        List<String> values = new ArrayList<>();
        List<CustomFieldOptionView> options = retrieveCustomFieldOptions(fieldView.get(), blackDuckService);
        if (options.isEmpty()) {
            logger.debug("Did not find any associated options for this field, will use raw values.");
            values = element.getValue();
        } else {
            logger.debug("Found one or more options for this field. Will attempt to map given values to fields..");
            for (String value : element.getValue()) {
                Optional<CustomFieldOptionView> option = options.stream().filter(it -> it.getLabel().equals(value)).findFirst();
                if (option.isPresent()) {
                    values.add(option.get().getHref().string());
                } else {
                    throw new DetectUserFriendlyException(String.format("Unable to update custom field '%s', unable to find option for value '%s'", element.getLabel(), value), ExitCodeType.FAILURE_BLACKDUCK_FEATURE_ERROR);
                }
            }
        }
        operations.add(new CustomFieldOperation(fieldView.get(), values));
    }
    return operations;
}
Also used : DetectUserFriendlyException(com.synopsys.integration.detect.configuration.DetectUserFriendlyException) CustomFieldElement(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldElement) ExitCodeType(com.synopsys.integration.detect.configuration.enumeration.ExitCodeType) IntegrationException(com.synopsys.integration.exception.IntegrationException) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) CustomFieldOperation(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldOperation) ProjectVersionWrapper(com.synopsys.integration.blackduck.service.model.ProjectVersionWrapper) ArrayList(java.util.ArrayList) LinkMultipleResponses(com.synopsys.integration.blackduck.api.core.response.LinkMultipleResponses) CustomFieldDocument(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldDocument) CustomFieldOptionView(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldOptionView) List(java.util.List) CustomFieldView(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldView) BlackDuckView(com.synopsys.integration.blackduck.api.core.BlackDuckView) Optional(java.util.Optional) NoSuchElementException(java.util.NoSuchElementException) Collections(java.util.Collections) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) CustomFieldView(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldView) ArrayList(java.util.ArrayList) CustomFieldOperation(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldOperation) CustomFieldOptionView(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldOptionView) CustomFieldElement(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldElement) DetectUserFriendlyException(com.synopsys.integration.detect.configuration.DetectUserFriendlyException)

Example 2 with CustomFieldElement

use of com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldElement in project synopsys-detect by blackducksoftware.

the class DetectCustomFieldParserTest method parsedProject.

@Test
public void parsedProject() throws DetectUserFriendlyException {
    Map<String, String> props = new HashMap<>();
    props.put("detect.custom.fields.project[0].label", "label");
    props.put("detect.custom.fields.project[0].value", "value1, value2");
    DetectCustomFieldParser parser = new DetectCustomFieldParser();
    CustomFieldDocument document = parser.parseCustomFieldDocument(props);
    Assertions.assertEquals(1, document.getProject().size());
    CustomFieldElement element = document.getProject().get(0);
    Assertions.assertEquals("label", element.getLabel());
    Assertions.assertEquals(2, element.getValue().size());
    Assertions.assertTrue(element.getValue().contains("value1"));
    Assertions.assertTrue(element.getValue().contains("value2"));
}
Also used : CustomFieldDocument(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldDocument) CustomFieldElement(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldElement) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 3 with CustomFieldElement

use of com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldElement in project synopsys-detect by blackducksoftware.

the class DetectCustomFieldParserTest method parsedMissingValueStillList.

@Test
public void parsedMissingValueStillList() throws DetectUserFriendlyException {
    Map<String, String> props = new HashMap<>();
    props.put("detect.custom.fields.project[0].label", "label");
    DetectCustomFieldParser parser = new DetectCustomFieldParser();
    CustomFieldDocument document = parser.parseCustomFieldDocument(props);
    CustomFieldElement element = document.getProject().get(0);
    Assertions.assertEquals(0, element.getValue().size());
}
Also used : CustomFieldDocument(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldDocument) CustomFieldElement(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldElement) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 4 with CustomFieldElement

use of com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldElement in project synopsys-detect by blackducksoftware.

the class DetectCustomFieldParserTest method parsedEmptySingleQuotesAsEmptyArray.

@Test
public void parsedEmptySingleQuotesAsEmptyArray() throws DetectUserFriendlyException {
    Map<String, String> props = new HashMap<>();
    props.put("detect.custom.fields.project[0].name", "example");
    props.put("detect.custom.fields.project[0].value", "''");
    DetectCustomFieldParser parser = new DetectCustomFieldParser();
    CustomFieldDocument document = parser.parseCustomFieldDocument(props);
    CustomFieldElement element = document.getProject().get(0);
    Assertions.assertEquals(0, element.getValue().size());
}
Also used : CustomFieldDocument(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldDocument) CustomFieldElement(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldElement) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 5 with CustomFieldElement

use of com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldElement in project synopsys-detect by blackducksoftware.

the class DetectCustomFieldParserTest method parsedEmptyStringAsEmptyArray.

@Test
public void parsedEmptyStringAsEmptyArray() throws DetectUserFriendlyException {
    Map<String, String> props = new HashMap<>();
    props.put("detect.custom.fields.project[0].name", "example");
    props.put("detect.custom.fields.project[0].value", "");
    DetectCustomFieldParser parser = new DetectCustomFieldParser();
    CustomFieldDocument document = parser.parseCustomFieldDocument(props);
    CustomFieldElement element = document.getProject().get(0);
    Assertions.assertEquals(0, element.getValue().size());
}
Also used : CustomFieldDocument(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldDocument) CustomFieldElement(com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldElement) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Aggregations

CustomFieldElement (com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldElement)9 CustomFieldDocument (com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldDocument)8 HashMap (java.util.HashMap)7 Test (org.junit.jupiter.api.Test)7 CustomFieldOperation (com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldOperation)2 BlackDuckView (com.synopsys.integration.blackduck.api.core.BlackDuckView)1 LinkMultipleResponses (com.synopsys.integration.blackduck.api.core.response.LinkMultipleResponses)1 BlackDuckApiClient (com.synopsys.integration.blackduck.service.BlackDuckApiClient)1 ProjectVersionWrapper (com.synopsys.integration.blackduck.service.model.ProjectVersionWrapper)1 DetectUserFriendlyException (com.synopsys.integration.detect.configuration.DetectUserFriendlyException)1 ExitCodeType (com.synopsys.integration.detect.configuration.enumeration.ExitCodeType)1 CustomFieldOptionView (com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldOptionView)1 CustomFieldView (com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldView)1 IntegrationException (com.synopsys.integration.exception.IntegrationException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 Optional (java.util.Optional)1 Logger (org.slf4j.Logger)1