Skip to content

Commit

Permalink
Adding unit test for the druid request headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
codingwhatever committed Oct 17, 2016
1 parent b632d50 commit bfebe62
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.yahoo.bard.webservice.druid.client.impl

import com.yahoo.bard.webservice.druid.client.DruidClientConfigHelper
import com.yahoo.bard.webservice.druid.model.query.QueryContext
import com.yahoo.bard.webservice.druid.model.query.WeightEvaluationQuery

import com.fasterxml.jackson.databind.ObjectMapper

import io.netty.handler.codec.http.HttpHeaders
import spock.lang.Specification

import java.util.function.Supplier

class AsyncDruidWebServiceImplSpec extends Specification {
def "Ensure that headersToAppend are added"() {
setup:
WeightEvaluationQuery weightEvaluationQuery = Mock(WeightEvaluationQuery)
QueryContext queryContext = Mock(QueryContext)
weightEvaluationQuery.getContext() >> { queryContext }
queryContext.numberOfQueries() >> { 1 }
queryContext.getSequenceNumber() >> { 1 }

when:
Map<String, String> expectedHeaders = new HashMap<>()
expectedHeaders.put("k1", "v1")
expectedHeaders.put("k2", "v2")
Supplier<Map<String, String>> supplier = new Supplier<Map<String, String>>() {
@Override
Map<String, String> get() {
return expectedHeaders
}
}

AsyncDruidWebServiceImplWrapper webServiceImplWrapper = new AsyncDruidWebServiceImplWrapper(
DruidClientConfigHelper.getNonUiServiceConfig(),
new ObjectMapper(),
supplier
)
webServiceImplWrapper.postDruidQuery(
null,
null,
null,
null,
weightEvaluationQuery
);

then:
HttpHeaders actualHeaders = webServiceImplWrapper.getHeaders()
for (Map.Entry<String, String> header : expectedHeaders) {
actualHeaders.get(header.getKey()) == header.getValue()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms.
package com.yahoo.bard.webservice.druid.client.impl;

import com.yahoo.bard.webservice.druid.client.DruidServiceConfig;
import com.yahoo.bard.webservice.druid.client.FailureCallback;
import com.yahoo.bard.webservice.druid.client.HttpErrorCallback;
import com.yahoo.bard.webservice.druid.client.SuccessCallback;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.asynchttpclient.BoundRequestBuilder;
import org.asynchttpclient.Request;

import io.netty.handler.codec.http.HttpHeaders;

import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;

/**
* Used for testing AsyncDruidWebServiceImpl.
*/
public class AsyncDruidWebServiceImplWrapper extends AsyncDruidWebServiceImpl {
public Request request;

/**
* Constructor wrapper.
*
* @param serviceConfig Service config
* @param mapper Mapper
* @param headersToAppend Headers
*/
public AsyncDruidWebServiceImplWrapper(
DruidServiceConfig serviceConfig,
ObjectMapper mapper,
Supplier<Map<String, String>> headersToAppend
) {
super(serviceConfig, mapper, headersToAppend);
}

/**
* Capture arguments to test for expected values.
*
* @param success callback for handling successful requests.
* @param error callback for handling http errors.
* @param failure callback for handling exception failures.
* @param requestBuilder The bound request builder for the request to be sent.
* @param timerName The name that distinguishes this request as part of a druid query or segment metadata request
* @param outstanding The counter that keeps track of the outstanding (in flight) requests for the top level query
*/
@Override
protected void sendRequest(
final SuccessCallback success,
final HttpErrorCallback error,
final FailureCallback failure,
final BoundRequestBuilder requestBuilder,
final String timerName,
final AtomicLong outstanding
) {
this.request = requestBuilder.build();
}

public HttpHeaders getHeaders() {
return request.getHeaders();
}
}

0 comments on commit bfebe62

Please sign in to comment.