Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Commit

Permalink
introduce TestHttpClient
Browse files Browse the repository at this point in the history
used in DiagStatusServletTest, also upcoming restconf support
  • Loading branch information
vorburger committed Oct 11, 2018
1 parent 01213f8 commit 32dc3c8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
package org.opendaylight.infrautils.diagstatus.web.test;

import static com.google.common.truth.Truth.assertThat;
import static org.opendaylight.infrautils.diagstatus.web.WebInitializer.DIAGSTATUS_URL;
import static org.opendaylight.infrautils.testutils.TestHttpClient.Method.GET;
import static org.opendaylight.infrautils.testutils.TestHttpClient.Method.HEAD;

import com.google.inject.AbstractModule;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import javax.inject.Inject;
Expand All @@ -26,6 +27,7 @@
import org.opendaylight.infrautils.inject.guice.testutils.AnnotationsModule;
import org.opendaylight.infrautils.inject.guice.testutils.GuiceRule2;
import org.opendaylight.infrautils.testutils.Partials;
import org.opendaylight.infrautils.testutils.TestHttpClient;
import org.opendaylight.infrautils.web.WebWiring;

/**
Expand All @@ -49,36 +51,30 @@ protected void configure() {
WebWiring.class, DiagStatusServletTestWiring.class, AnnotationsModule.class);

@Inject WebServer webServer;
@Inject TestHttpClient http;

@Test
public void testGetWhenOk() throws IOException {
SRVC.isOperational = true;
assertThat(getDiagStatusResponseCode("GET")).isEqualTo(200);
assertThat(http.responseCode(GET, DIAGSTATUS_URL)).isEqualTo(200);
}

@Test
public void testHeadWhenOk() throws IOException {
SRVC.isOperational = true;
assertThat(getDiagStatusResponseCode("HEAD")).isEqualTo(200);
assertThat(http.responseCode(HEAD, DIAGSTATUS_URL)).isEqualTo(200);
}

@Test
public void testGetWhenNok() throws IOException {
SRVC.isOperational = false;
assertThat(getDiagStatusResponseCode("GET")).isEqualTo(503);
assertThat(http.responseCode(GET, DIAGSTATUS_URL)).isEqualTo(503);
}

@Test
public void testHeadWhenNok() throws IOException {
SRVC.isOperational = false;
assertThat(getDiagStatusResponseCode("HEAD")).isEqualTo(503);
}

private int getDiagStatusResponseCode(String httpMethod) throws IOException {
URL url = new URL(webServer.getBaseURL() + WebInitializer.DIAGSTATUS_URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(httpMethod);
return conn.getResponseCode();
assertThat(http.responseCode(HEAD, DIAGSTATUS_URL)).isEqualTo(503);
}

private abstract static class TestDiagStatusService implements DiagStatusService {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2018 Red Hat, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.infrautils.testutils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.opendaylight.aaa.web.WebServer;

/**
* HTTP Client.
*
* @author Michael Vorburger.ch
*/
@Singleton
public class TestHttpClient {

public enum Method {
GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE
}

private final WebServer webServer;

@Inject
public TestHttpClient(WebServer webServer) {
this.webServer = webServer;
}

public int responseCode(Method httpMethod, String path) throws IOException {
URL url = new URL(webServer.getBaseURL() + path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(httpMethod.name());
return conn.getResponseCode();
}
}

0 comments on commit 32dc3c8

Please sign in to comment.