Search in sources :

Example 1 with AdapterException

use of com.mercedesbenz.sechub.adapter.AdapterException in project sechub by mercedes-benz.

the class CheckmarxProductExecutorMockTest method when_adapter_throws_first_time_adapter_exception_with_exceed_a_retry_is_done.

@Test
public void when_adapter_throws_first_time_adapter_exception_with_exceed_a_retry_is_done() throws Exception {
    /* prepare */
    SecHubExecutionContext context = createExecutionContextForPseudoCodeScan();
    ProductExecutorCallback callback = mock(ProductExecutorCallback.class);
    ProductExecutorConfigSetup setup = createCheckmarxSetupWithAllMandotoryPartsSet();
    ProductExecutorConfig executorConfig = new ProductExecutorConfig(ProductIdentifier.CHECKMARX, 1, setup);
    ProductExecutorContext executorContext = mock(ProductExecutorContext.class);
    when(executorContext.getCallback()).thenReturn(callback);
    when(executorContext.getExecutorConfig()).thenReturn(executorConfig);
    ProductResult currentResult = new ProductResult(JOB_UUID, PROJECT_EXAMPLE, executorConfig, "pseudo-result");
    when(executorContext.getCurrentProductResult()).thenReturn(currentResult);
    /* @formatter:off */
    when(checkmarxAdapter.start(any(), any())).thenThrow(// first fails
    new AdapterException(new AdapterLogId("1", "traceId"), "bla bla - Changes exceeded the threshold limit - bla bla")).thenReturn(// second: access
    "result2");
    /* @formatter:on */
    /* execute */
    executorToTest.execute(context, executorContext);
    /* test */
    // the adapter must be called twice
    verify(checkmarxAdapter, times(2)).start(any(), any());
}
Also used : ProductResult(com.mercedesbenz.sechub.domain.scan.product.ProductResult) SecHubExecutionContext(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionContext) ProductExecutorConfig(com.mercedesbenz.sechub.domain.scan.product.config.ProductExecutorConfig) ProductExecutorContext(com.mercedesbenz.sechub.domain.scan.product.ProductExecutorContext) AdapterException(com.mercedesbenz.sechub.adapter.AdapterException) ProductExecutorCallback(com.mercedesbenz.sechub.domain.scan.product.ProductExecutorCallback) AdapterLogId(com.mercedesbenz.sechub.adapter.AdapterLogId) ProductExecutorConfigSetup(com.mercedesbenz.sechub.domain.scan.product.config.ProductExecutorConfigSetup) Test(org.junit.Test)

Example 2 with AdapterException

use of com.mercedesbenz.sechub.adapter.AdapterException in project sechub by mercedes-benz.

the class CheckmarxProductExecutorMockTest method when_adapter_throws_two_time_adapter_exception_with_exceed_a_retry_is_done_only_one_time.

@Test
public void when_adapter_throws_two_time_adapter_exception_with_exceed_a_retry_is_done_only_one_time() throws Exception {
    /* prepare */
    SecHubExecutionContext context = createExecutionContextForPseudoCodeScan();
    ProductExecutorCallback callback = mock(ProductExecutorCallback.class);
    ProductExecutorConfigSetup setup = createCheckmarxSetupWithAllMandotoryPartsSet();
    ProductExecutorConfig executorConfig = new ProductExecutorConfig(ProductIdentifier.CHECKMARX, 1, setup);
    ProductExecutorContext executorContext = mock(ProductExecutorContext.class);
    when(executorContext.getCallback()).thenReturn(callback);
    when(executorContext.getExecutorConfig()).thenReturn(executorConfig);
    ProductResult currentResult = new ProductResult(JOB_UUID, PROJECT_EXAMPLE, executorConfig, "pseudo-result");
    when(executorContext.getCurrentProductResult()).thenReturn(currentResult);
    /* @formatter:off */
    when(checkmarxAdapter.start(any(), any())).thenThrow(// first fails
    new AdapterException(new AdapterLogId("1", "traceId"), "bla bla - Changes exceeded the threshold limit - bla bla")).thenThrow(// second fails
    new AdapterException(new AdapterLogId("2", "traceId"), "bla bla - Changes exceeded the threshold limit - bla bla")).thenReturn(// third: would be access but should not happen! resilience shall here only work one time!
    "result2");
    /* @formatter:on */
    SecHubExecutionException expected = null;
    /* execute */
    try {
        executorToTest.execute(context, executorContext);
    } catch (SecHubExecutionException e) {
        expected = e;
    }
    /* test */
    assertNotNull("No SecHubExecutionException happened, but must!", expected);
    // the adapter must be called twice - first errr, than one retry, third error
    verify(checkmarxAdapter, times(2)).start(any(), any());
    // means no retry again
    Throwable cause = expected.getCause();
    String message = cause.getMessage();
    assertTrue(message.contains("Changes exceeded the threshold limit"));
}
Also used : SecHubExecutionException(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException) ProductResult(com.mercedesbenz.sechub.domain.scan.product.ProductResult) SecHubExecutionContext(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionContext) ProductExecutorConfig(com.mercedesbenz.sechub.domain.scan.product.config.ProductExecutorConfig) ProductExecutorContext(com.mercedesbenz.sechub.domain.scan.product.ProductExecutorContext) AdapterException(com.mercedesbenz.sechub.adapter.AdapterException) ProductExecutorCallback(com.mercedesbenz.sechub.domain.scan.product.ProductExecutorCallback) AdapterLogId(com.mercedesbenz.sechub.adapter.AdapterLogId) ProductExecutorConfigSetup(com.mercedesbenz.sechub.domain.scan.product.config.ProductExecutorConfigSetup) Test(org.junit.Test)

Example 3 with AdapterException

use of com.mercedesbenz.sechub.adapter.AdapterException in project sechub by mercedes-benz.

the class AdapterExceptionTest method throwAsAdapterException_rethrows_adapter_exception_when_exception_is_adapter_exception.

@Test
public void throwAsAdapterException_rethrows_adapter_exception_when_exception_is_adapter_exception() throws Exception {
    /* prepare */
    AdapterException cause = new AdapterException(new AdapterLogId("y", null), "originMessage");
    /* test */
    expected.expect(AdapterException.class);
    expected.expectMessage("y:originMessage");
    /* execute */
    throwAsAdapterException(new AdapterLogId("z", null), "otherMessage", cause);
}
Also used : AdapterException(com.mercedesbenz.sechub.adapter.AdapterException) Test(org.junit.Test)

Example 4 with AdapterException

use of com.mercedesbenz.sechub.adapter.AdapterException in project sechub by mercedes-benz.

the class PDSAdapterV1 method waitForJobDone.

private void waitForJobDone(PDSContext context) throws AdapterException {
    PDSAdapterConfig config = context.getConfig();
    PDSAdapterConfigData data = config.getPDSAdapterConfigData();
    UUID secHubJobUUID = data.getSecHubJobUUID();
    UUID pdsJobUUID = context.getPdsJobUUID();
    int count = 0;
    boolean jobEnded = false;
    PDSJobStatus jobstatus = null;
    long started = getCurrentTimeMilliseconds();
    int timeToWaitForNextCheckOperationInMilliseconds = config.getTimeToWaitForNextCheckOperationInMilliseconds();
    LOG.info("Start waiting for PDS-job:{} to be done. Related SecHub-Job is:{} . Will check every {} ms. Adapter will wait maximum {} ms before timeout.", pdsJobUUID, secHubJobUUID, timeToWaitForNextCheckOperationInMilliseconds, config.getTimeOutInMilliseconds());
    while (!jobEnded && isNotTimeout(config, started)) {
        count++;
        LOG.debug("Fetch job status for PDS-job:{}. Elapsed time for {} retries:{} ms", pdsJobUUID, count, calculateElapsedTime(started));
        /* see PDSJobStatusState.java */
        jobstatus = getJobStatus(context);
        PDSAdapterJobStatusState state = jobstatus.state;
        switch(state) {
            case DONE:
                jobEnded = true;
                break;
            case FAILED:
                throw asAdapterException("PDS job execution failed", config);
            case CANCELED:
                throw asAdapterCanceledByUserException(config);
            default:
        }
        if (jobEnded) {
            // break while...
            break;
        }
        assertNotInterrupted();
        try {
            Thread.sleep(timeToWaitForNextCheckOperationInMilliseconds);
        } catch (InterruptedException e) {
            throw new AdapterException(getAdapterLogId(null), "Execution thread was interrupted");
        }
    }
    if (!jobEnded) {
        long elapsedTimeInMilliseconds = calculateElapsedTime(started);
        throw new IllegalStateException("Even after " + count + " retries, every waiting " + timeToWaitForNextCheckOperationInMilliseconds + " ms, no job report state acceppted as END was found.!\nElapsed time were" + elapsedTimeInMilliseconds + " ms.\nLAST fetched jobstatus for " + secHubJobUUID + ", PDS job uuid: " + pdsJobUUID + " was:\n" + jobstatus);
    }
}
Also used : PDSJobStatus(com.mercedesbenz.sechub.adapter.pds.data.PDSJobStatus) AdapterException(com.mercedesbenz.sechub.adapter.AdapterException) UUID(java.util.UUID) PDSAdapterJobStatusState(com.mercedesbenz.sechub.adapter.pds.data.PDSJobStatus.PDSAdapterJobStatusState)

Example 5 with AdapterException

use of com.mercedesbenz.sechub.adapter.AdapterException in project sechub by mercedes-benz.

the class AssertProject method hasMetaData.

public AssertProject hasMetaData(Map<String, String> expectedMap) {
    String content = fetchProjectDetails();
    Map<String, String> found = new HashMap<>();
    JsonNode metaDataNode;
    try {
        metaDataNode = JSONAdapterSupport.FOR_UNKNOWN_ADAPTER.fetch("metaData", content).asNode();
        metaDataNode.fieldNames().forEachRemaining(key -> {
            JsonNode element = metaDataNode.get(key);
            if (element.isTextual()) {
                String value = element.textValue();
                found.put(key, value);
            }
        });
    } catch (AdapterException e) {
        e.printStackTrace();
        fail("adapter jso failure:" + e.getMessage());
    }
    assertEquals(expectedMap, found);
    return this;
}
Also used : HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) AdapterException(com.mercedesbenz.sechub.adapter.AdapterException)

Aggregations

AdapterException (com.mercedesbenz.sechub.adapter.AdapterException)9 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 Test (org.junit.Test)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 AdapterLogId (com.mercedesbenz.sechub.adapter.AdapterLogId)2 ProductExecutorCallback (com.mercedesbenz.sechub.domain.scan.product.ProductExecutorCallback)2 ProductExecutorContext (com.mercedesbenz.sechub.domain.scan.product.ProductExecutorContext)2 ProductResult (com.mercedesbenz.sechub.domain.scan.product.ProductResult)2 ProductExecutorConfig (com.mercedesbenz.sechub.domain.scan.product.config.ProductExecutorConfig)2 ProductExecutorConfigSetup (com.mercedesbenz.sechub.domain.scan.product.config.ProductExecutorConfigSetup)2 SecHubExecutionContext (com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionContext)2 CheckmarxFullScanNecessaryException (com.mercedesbenz.sechub.adapter.checkmarx.support.CheckmarxFullScanNecessaryException)1 CheckmarxOAuthSupport (com.mercedesbenz.sechub.adapter.checkmarx.support.CheckmarxOAuthSupport)1 CheckmarxProjectSupport (com.mercedesbenz.sechub.adapter.checkmarx.support.CheckmarxProjectSupport)1 CheckmarxScanReportSupport (com.mercedesbenz.sechub.adapter.checkmarx.support.CheckmarxScanReportSupport)1 PDSJobStatus (com.mercedesbenz.sechub.adapter.pds.data.PDSJobStatus)1 PDSAdapterJobStatusState (com.mercedesbenz.sechub.adapter.pds.data.PDSJobStatus.PDSAdapterJobStatusState)1 SecHubExecutionException (com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1