Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Discovery Service and Https support #27

Merged
merged 2 commits into from
Sep 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yahoo.pulsar.discovery.service;
package com.yahoo.pulsar.discovery.service.web;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.CompletableFuture;

import javax.ws.rs.HttpMethod;
import javax.ws.rs.client.Client;
Expand All @@ -30,6 +32,7 @@
import javax.ws.rs.core.Response;

import org.apache.bookkeeper.test.PortManager;
import org.apache.zookeeper.ZooKeeper;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.filter.LoggingFilter;
import org.json.JSONException;
Expand All @@ -43,8 +46,10 @@
import com.yahoo.pulsar.common.policies.data.BundlesData;
import com.yahoo.pulsar.discovery.service.server.ServerManager;
import com.yahoo.pulsar.discovery.service.server.ServiceConfig;
import com.yahoo.pulsar.discovery.service.web.DiscoveryServiceServlet;
import com.yahoo.pulsar.zookeeper.ZooKeeperClientFactory;

public class DiscoveryServiceTest extends ProducerConsumerBase {
public class DiscoveryServiceWebTest extends ProducerConsumerBase {

private Client client = ClientBuilder.newClient(new ClientConfig().register(LoggingFilter.class));

Expand All @@ -64,56 +69,52 @@ protected void cleanup() throws Exception {
/**
* 1. Start : Broker and Discovery service. 2. Provide started broker server as active broker to Discovery service
* 3. Call GET, PUT, POST request to discovery service that redirects to Broker service and receives response
*
*
* @throws Exception
*/
@Test
public void testRiderectUrlWithServerStarted() throws Exception {

// 1. start server
List<String> resources = Lists.newArrayList(DiscoveryService.class.getPackage().getName());
System.setProperty("zookeeperServers", "dummy-value");
System.setProperty("zooKeeperSessionTimeoutMillis", "1000");

int port = PortManager.nextFreePort();
ServiceConfig config = new ServiceConfig();
config.setWebServicePort(port);
ServerManager server = new ServerManager(config);
server.start(resources);

ZookeeperCacheLoader.availableActiveBrokers.add(super.brokerUrl.getHost() + ":" + super.brokerUrl.getPort());

Thread.sleep(200);
DiscoveryZooKeeperClientFactoryImpl.zk = mockZookKeeper;
Map<String, String> params = new TreeMap<>();
params.put("zookeeperServers", "");
params.put("zookeeperClientFactoryClass", DiscoveryZooKeeperClientFactoryImpl.class.getName());
server.addServlet("/", DiscoveryServiceServlet.class, params);
server.start();

String serviceUrl = server.getServiceUri().toString();
String putRequestUrl = serviceUrl + "admin/namespaces/p1/c1/n1";
String postRequestUrl = serviceUrl + "admin/namespaces/p1/c1/n1/permissions/test-role";
String postRequestUrl = serviceUrl + "admin/namespaces/p1/c1/n1/replication";
String getRequestUrl = serviceUrl + "admin/namespaces/p1";

/**
* verify : every time when vip receives a request: it redirects to above brokers sequentially and broker
* returns appropriate response which must not be null.
**/
assertNotNull(hitBrokerService(HttpMethod.POST, postRequestUrl, null));
assertNotNull(hitBrokerService(HttpMethod.PUT, putRequestUrl, new BundlesData(1)));
assertNotNull(hitBrokerService(HttpMethod.GET, getRequestUrl, null));

assertEquals("Cannot get the replication clusters for a non-global namespace", hitBrokerService(HttpMethod.POST, postRequestUrl, Lists.newArrayList("use")));
assertEquals("Property does not exist", hitBrokerService(HttpMethod.PUT, putRequestUrl, new BundlesData(1)));
assertEquals("Property does not exist", hitBrokerService(HttpMethod.GET, getRequestUrl, null));
server.stop();

}

public String hitBrokerService(String method, String url, BundlesData bundle) throws JSONException {
public String hitBrokerService(String method, String url, Object data) throws JSONException {

Response response = null;
try {
WebTarget webTarget = client.target(url);
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
if (HttpMethod.PUT.equals(method)) {
response = (Response) invocationBuilder.put(Entity.entity(bundle, MediaType.APPLICATION_JSON));
response = (Response) invocationBuilder.put(Entity.entity(data, MediaType.APPLICATION_JSON));
} else if (HttpMethod.GET.equals(method)) {
response = (Response) invocationBuilder.get();
} else if (HttpMethod.POST.equals(method)) {
response = (Response) invocationBuilder.post(Entity.entity(bundle, MediaType.APPLICATION_JSON));
response = (Response) invocationBuilder.post(Entity.entity(data, MediaType.APPLICATION_JSON));
} else {
fail("Unsupported http method");
}
Expand All @@ -126,5 +127,15 @@ public String hitBrokerService(String method, String url, BundlesData bundle) th
String serviceResponse = jsonObject.getString("reason");
return serviceResponse;
}

static class DiscoveryZooKeeperClientFactoryImpl implements ZooKeeperClientFactory {
static ZooKeeper zk;

@Override
public CompletableFuture<ZooKeeper> create(String serverList, SessionType sessionType,
int zkSessionTimeoutMillis) {
return CompletableFuture.completedFuture(zk);
}
}

}
8 changes: 8 additions & 0 deletions pulsar-discovery-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@
<artifactId>powermock-module-testng</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>pulsar-zookeeper-utils</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>

</dependencies>

Expand Down

This file was deleted.

Loading