protected Response getStubbedResponse ( Object requestObject ) { // compatibility String resp = getStubResponse ( externalRequestToString ( requestObject ) ) ; if ( resp != null ) { Response oldStubbed = new Response ( ) ; oldStubbed . setObject ( resp ) ; oldStubbed . setContent ( externalResponseToString ( resp ) ) ; return oldStubbed ; } List < SimulationResponse > responses = new ArrayList <> ( ) ; for ( Attribute attr : this . getAttributes ( ) ) { if ( attr . getAttributeName ( ) . startsWith ( WorkAttributeConstant . SIMULATION_RESPONSE ) ) { SimulationResponse r = new SimulationResponse ( attr . getAttributeValue ( ) ) ; responses . add ( r ) ; } } String unfilteredResponse = null ; String returnCode = null ; if ( responses . size ( ) == 0 ) { unfilteredResponse = null ; } else if ( responses . size ( ) == 1 ) { unfilteredResponse = responses . get ( 0 ) . getResponse ( ) ; returnCode = responses . get ( 0 ) . getReturnCode ( ) ; } else { // randomly pick a response based on chances int total_chances = 0 ; for ( SimulationResponse r : responses ) { total_chances += r . getChance ( ) . intValue ( ) ; } if ( random == null ) random = new Random ( ) ; int ran = random . nextInt ( total_chances ) ; int k = 0 ; for ( SimulationResponse r : responses ) { if ( ran >= k && ran < k + r . getChance ( ) . intValue ( ) ) { unfilteredResponse = r . getResponse ( ) ; break ; } k += r . getChance ( ) . intValue ( ) ; } } Response response = new Response ( ) ; String filtered = filter ( unfilteredResponse , requestObject ) ; response . setObject ( filtered ) ; response . setContent ( filtered ) ; if ( returnCode != null ) { try { response . setStatusCode ( Integer . parseInt ( returnCode ) ) ; } catch ( NumberFormatException ex ) { } } return response ; }
Return stubbed response from external system .