使用WireMock访问异步响应

时间:2019-06-29 14:39:49

标签: java spring wiremock

我提供了课程,

@Service
public class EllaService {

    private static final Logger LOG = LoggerFactory.getLogger( EllaService.class );

    @Autowired
    @Qualifier( ELLA_CONNECTOR_BEAN_NAME )
    private EntityServiceConnectable<EllaResponseDto> connector;

    @Autowired
    private EllaFilterConfigHolder configHolder;

    @Autowired
    private EllaServiceConfiguration config;

    @Autowired
    private Environment env;

    /**
     * Initialize the service.
     */
    @PostConstruct
    public void initialize() {
        this.configHolder = config.ellaFilterConfigHolder( env );
    }

    // ========================================================================

    /**
     * Asynchronously call Ella. Determine if traffic is applicable for Ella and if yes forward to Ella.
     *
     * @param irisBo IrisBo object
     */
    @Async( ELLA_THREAD_POOL_EXECUTOR_NAME )
    public void invokeEllaAsync( final IrisBo irisBo ) {

        if( isTrafficAllowed( irisBo ) ) {
            callEllaService( irisBo );
        }
    }

    // ========================================================================

    /**
     * Call ella service without processing service response.
     *
     * @param irisBo IrisBo object
     */
    public void callEllaService( final IrisBo irisBo ) {

        HttpHeaders ellaHeaders = createRequestHeaders( irisBo );

        ServiceResponse<EllaResponseDto> response = connector.call( EllaDtoConverter.convertToRequest( irisBo ), ellaHeaders );

        if( !response.isSuccess() ) {
            LOG.error( "ERROR occured while calling ella async [ irisBo: {} - response: {}]", response, irisBo );
        }
    }

    // ========================================================================

    private boolean isTrafficAllowed( IrisBo irisBo ) {

        if( configHolder.isExternalBuyerFilterEnabled() && irisBo.getBuyer().isExternalKnownCustomer() ) {
            return false;
        }

        if( configHolder.isInternalBuyerFilterEnabled() && irisBo.getBuyer().isInternalKnownCustomer() ) {
            return false;
        }

        return isShopNotConfigured( irisBo );
    }

    // ========================================================================

    private boolean isShopNotConfigured( IrisBo irisBo ) {
        return !configHolder.getShopIdsToFilterSet().contains( irisBo.getOrder().getShopId() );
    }

    // ========================================================================

    private HttpHeaders createRequestHeaders( final IrisBo irisBo ) {

        HttpHeaders ellaHeaders = new HttpHeaders();

        ellaHeaders.add( ACCEPT, APPLICATION_JSON_UTF8_VALUE );
        ellaHeaders.add( CONTENT_TYPE, APPLICATION_JSON_UTF8_VALUE );

        RatepayHeaders.append( ellaHeaders, irisBo.getRequestInfo() );

        return ellaHeaders;
    }

}

我为该类编写了一个集成测试,

public class EllaServiceIT {

    private final static String ELLA_ENDPOINT = "/ella";

    private EllaService ellaService;
    private IrisBo irisBo;

    @Rule
    public WireMockRule wireMockRule = new WireMockRule( wireMockConfig().dynamicPort() );

    @Before
    @SuppressWarnings( "resource" )
    public void setUp() {

        int port = wireMockRule.port();

        System.setProperty( "ella.uri", "http://localhost:" + port + ELLA_ENDPOINT );
        System.setProperty( "ella.internal.buyer.filter", "false" );
        System.setProperty( "ella.external.buyer.filter", "false" );

        System.setProperty( "ella.shop.id.filter", "123456" );
        System.setProperty( "ella.connection_timeout", "10000" );
        System.setProperty( "ella.read_timeout", "10000" );

        System.setProperty( "kafka.bootstrap_servers", "localhost" );
        System.setProperty( "kafka.topic", "ella-test-topic" );

        ApplicationContext context = new AnnotationConfigApplicationContext( EllaServiceConfiguration.class );
        ellaService = context.getBean( EllaService.class );

        irisBo = EllaTestDataProvider.createValidIrisBoWithoutRequest();
    }

    @Test
    public void testCallEllaServiceIsSuccessful() {

        wireMockRule.stubFor( post( urlEqualTo( ELLA_ENDPOINT ) ) );
        ellaService.invokeEllaAsync( irisBo );

        verify( exactly( 1 ), postRequestedFor( urlEqualTo( ELLA_ENDPOINT ) ) );
    }  




@Test
    public void testCallEllaServiceIsUnsuccessful() {

        wireMockRule.stubFor( post( urlEqualTo( ELLA_ENDPOINT ) ).willReturn( serverError() ) );
        ellaService.invokeEllaAsync( irisBo );

        verify( exactly( 0 ), postRequestedFor( urlEqualTo( ELLA_ENDPOINT ) ) );
    }
}

IT测试运行良好,但我也想访问LOC返回的异步值并对其进行验证。

ServiceResponse<EllaResponseDto> response = connector.call( EllaDtoConverter.convertToRequest( irisBo ), ellaHeaders );

怎么可能?

0 个答案:

没有答案