[jboss-svn-commits] JBL Code SVN: r35648 - in labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk: src/main/java/org/jboss/community/sbs/plugin/reports/monthly and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Oct 21 10:08:25 EDT 2010
Author: velias
Date: 2010-10-21 10:08:24 -0400 (Thu, 21 Oct 2010)
New Revision: 35648
Added:
labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/main/java/org/jboss/community/sbs/plugin/reports/monthly/MapBasedSystemConfigProvider.java
labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/test/java/org/jboss/community/sbs/plugin/reports/monthly/valuesource/FISHEYEValueSourceTest.java
labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/test/java/org/jboss/community/sbs/plugin/reports/monthly/valuesource/HttpFISHEYEMockServer.java
Modified:
labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/pom.xml
labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/main/java/org/jboss/community/sbs/plugin/reports/monthly/SystemConfigProvider.java
labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/main/java/org/jboss/community/sbs/plugin/reports/monthly/valuesource/FISHEYEValueSource.java
Log:
FISHEYEValueSource implemented with unit tests
Modified: labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/pom.xml
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/pom.xml 2010-10-21 14:01:38 UTC (rev 35647)
+++ labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/pom.xml 2010-10-21 14:08:24 UTC (rev 35648)
@@ -32,6 +32,15 @@
</developer>
</developers>
+ <dependencies>
+ <dependency>
+ <groupId>org.simpleframework</groupId>
+ <artifactId>simple</artifactId>
+ <version>4.1.21</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
<build>
<finalName>reports-plugin</finalName>
<plugins>
Added: labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/main/java/org/jboss/community/sbs/plugin/reports/monthly/MapBasedSystemConfigProvider.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/main/java/org/jboss/community/sbs/plugin/reports/monthly/MapBasedSystemConfigProvider.java (rev 0)
+++ labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/main/java/org/jboss/community/sbs/plugin/reports/monthly/MapBasedSystemConfigProvider.java 2010-10-21 14:08:24 UTC (rev 35648)
@@ -0,0 +1,72 @@
+package org.jboss.community.sbs.plugin.reports.monthly;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import com.jivesoftware.community.JiveGlobals;
+
+/**
+ * System configuration properties provider based on internal Map. Fill it by {@link #addConfigProperty(String, String)}
+ * .
+ *
+ * @author Vlastimil Elias (velias at redhat dot com)
+ *
+ * @see JiveGlobals
+ */
+public class MapBasedSystemConfigProvider implements SystemConfigProvider {
+
+ private Map<String, String> map = new HashMap<String, String>();
+
+ /**
+ * Add config property into provider. Be carefull value is parseable into target data type!
+ *
+ * @param name of property.
+ * @param value of property.
+ */
+ public void addConfigProperty(String name, String value) {
+ map.put(name, value);
+ }
+
+ @Override
+ public boolean getBooleanProperty(String name) {
+ return Boolean.parseBoolean(map.get(name));
+ }
+
+ @Override
+ public boolean getBooleanProperty(String name, boolean defaultValue) {
+ String s = map.get(name);
+ if (s == null)
+ return defaultValue;
+ return Boolean.parseBoolean(s);
+ }
+
+ @Override
+ public int getIntProperty(String name, int defaultValue) {
+ String s = map.get(name);
+ if (s == null)
+ return defaultValue;
+ return Integer.parseInt(s);
+ }
+
+ @Override
+ public long getLongProperty(String name, long defaultValue) {
+ String s = map.get(name);
+ if (s == null)
+ return defaultValue;
+ return Long.parseLong(s);
+ }
+
+ @Override
+ public String getStringProperty(String name) {
+ return map.get(name);
+ }
+
+ @Override
+ public String getStringProperty(String name, String defaultValue) {
+ String s = map.get(name);
+ if (s == null)
+ return defaultValue;
+ return s;
+ }
+
+}
Modified: labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/main/java/org/jboss/community/sbs/plugin/reports/monthly/SystemConfigProvider.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/main/java/org/jboss/community/sbs/plugin/reports/monthly/SystemConfigProvider.java 2010-10-21 14:01:38 UTC (rev 35647)
+++ labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/main/java/org/jboss/community/sbs/plugin/reports/monthly/SystemConfigProvider.java 2010-10-21 14:08:24 UTC (rev 35648)
@@ -10,15 +10,56 @@
*/
public interface SystemConfigProvider {
+ /**
+ * Get String system configuration property.
+ *
+ * @param name of property
+ * @return value of property (may be null)
+ */
String getStringProperty(String name);
+ /**
+ * Get String system configuration property.
+ *
+ * @param name of property
+ * @param defaultValue default value
+ * @return value of property or defaultValue
+ */
String getStringProperty(String name, String defaultValue);
+ /**
+ * Get Boolean system configuration property.
+ *
+ * @param name of property
+ * @param defaultValue default value
+ * @return value of property or defaultValue
+ */
boolean getBooleanProperty(String name);
+ /**
+ * Get Boolean system configuration property.
+ *
+ * @param name of property
+ * @param defaultValue default value
+ * @return value of property or defaultValue
+ */
boolean getBooleanProperty(String name, boolean defaultValue);
+ /**
+ * Get Integer system configuration property.
+ *
+ * @param name of property
+ * @param defaultValue default value
+ * @return value of property or defaultValue
+ */
int getIntProperty(String name, int defaultValue);
+ /**
+ * Get Long system configuration property.
+ *
+ * @param name of property
+ * @param defaultValue default value
+ * @return value of property or defaultValue
+ */
long getLongProperty(String name, long defaultValue);
}
Modified: labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/main/java/org/jboss/community/sbs/plugin/reports/monthly/valuesource/FISHEYEValueSource.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/main/java/org/jboss/community/sbs/plugin/reports/monthly/valuesource/FISHEYEValueSource.java 2010-10-21 14:01:38 UTC (rev 35647)
+++ labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/main/java/org/jboss/community/sbs/plugin/reports/monthly/valuesource/FISHEYEValueSource.java 2010-10-21 14:08:24 UTC (rev 35648)
@@ -1,19 +1,79 @@
package org.jboss.community.sbs.plugin.reports.monthly.valuesource;
+import java.io.IOException;
+import java.util.ArrayList;
import java.util.Date;
+import java.util.List;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.NameValuePair;
+import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.commons.httpclient.auth.AuthScope;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.lang.StringUtils;
import org.jboss.community.sbs.plugin.reports.monthly.SystemConfigProvider;
import org.jboss.community.sbs.plugin.reports.monthly.aggregator.AggregatorUtils;
import org.jboss.community.sbs.plugin.reports.monthly.config.FishEyeRepositoryConfig;
+import com.ibm.icu.text.SimpleDateFormat;
+
/**
* Data source to obtain report value for one repository/path pair in FishEye.
+ * <p>
+ * System Config parameters used by this value source:
+ * <ul>
+ * <li><code>org.jboss.community.sbs.plugin.reports.monthly.fisheye.url</code> - String - http/s url to fisheye server
+ * where is <a href="https://docspace.corp.redhat.com/docs/DOC-36206#API" target="_blank">"Red Hat Contributors report"
+ * plugin API</a>.
+ * <li><code>org.jboss.community.sbs.plugin.reports.monthly.fisheye.u</code> - String - username used to call FishEye
+ * <li><code>org.jboss.community.sbs.plugin.reports.monthly.fisheye.p</code> - String - password used to call FishEye
+ * <li><code>org.jboss.community.sbs.plugin.reports.monthly.fisheye.debug</code> - Boolean - if set to <code>true</code>
+ * no http/s calls to FishEye are performed, generator returns mock value only (zero)
+ * </ul>
*
* @author Vlastimil Elias (velias at redhat dot com)
*/
public class FISHEYEValueSource extends ValueSourceBase<FishEyeRepositoryConfig> {
/**
+ * FishEye call parameter name for Maximal date.
+ */
+ protected static final String FISHEYE_PARAM_NAME_MAXDATE = "mad";
+
+ /**
+ * FishEye call parameter name for Minimal date.
+ */
+ protected static final String FISHEYE_PARAM_NAME_MINDATE = "mid";
+
+ /**
+ * FishEye call parameter name for Repository path.
+ */
+ protected static final String FISHEYE_PARAM_NAME_PATH = "p";
+
+ /**
+ * FishEye call parameter name for Repository name.
+ */
+ protected static final String FISHEYE_PARAM_NAME_REPO = "r";
+
+ /**
+ * Date parameters format used on FishEye call.
+ */
+ protected static final String FISHEYE_PARAM_DATE_FORMAT = "yyyy-M-d";
+
+ public static final String CONFIG_KEY_URL = "org.jboss.community.sbs.plugin.reports.monthly.fisheye.url";
+ public static final String CONFIG_KEY_USERNAME = "org.jboss.community.sbs.plugin.reports.monthly.fisheye.u";
+ public static final String CONFIG_KEY_PWD = "org.jboss.community.sbs.plugin.reports.monthly.fisheye.p";
+ public static final String CONFIG_KEY_DEBUG = "org.jboss.community.sbs.plugin.reports.monthly.fisheye.debug";
+
+ protected boolean debug = false;
+
+ protected HttpClient client;
+
+ protected boolean doAuthentication = false;
+
+ /**
* Constructor.
*
* @param sysConfig System configuration provider to be used
@@ -22,12 +82,114 @@
*/
public FISHEYEValueSource(SystemConfigProvider sysConfig, Date dateFrom, Date dateTo) {
super(sysConfig, dateFrom, dateTo);
+ if (sysConfig != null && !sysConfig.getBooleanProperty(CONFIG_KEY_DEBUG)) {
+ debug = false;
+ } else {
+ debug = true;
+ }
+ if (!debug) {
+ initHttpClient();
+ }
}
+ /**
+ * Init HTTP client.
+ */
+ protected void initHttpClient() {
+
+ String url = StringUtils.trimToNull(sysConfig.getStringProperty(CONFIG_KEY_URL));
+ if (url == null) {
+ throw new IllegalArgumentException("FishEye url not defined in system config property: " + CONFIG_KEY_URL);
+ }
+
+ client = new HttpClient();
+ String un = StringUtils.trimToNull(this.sysConfig.getStringProperty(CONFIG_KEY_USERNAME));
+ String pwd = StringUtils.trimToEmpty(this.sysConfig.getStringProperty(CONFIG_KEY_PWD));
+ if (un != null) {
+ client.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
+ new UsernamePasswordCredentials(un, pwd));
+ doAuthentication = true;
+ }
+ }
+
@Override
- public String[] getValues(FishEyeRepositoryConfig id) {
- // TODO implement me
- return AggregatorUtils.createFilledArray(2, "0");
+ public String[] getValues(FishEyeRepositoryConfig id) throws Exception {
+
+ if (id == null) {
+ throw new IllegalArgumentException("FishEyeRepositoryConfig can't be null");
+ }
+
+ if (debug) {
+ return AggregatorUtils.createFilledArray(2, "0");
+ }
+
+ HttpMethod method = new GetMethod(StringUtils.trimToNull(sysConfig.getStringProperty(CONFIG_KEY_URL)));
+ method.setDoAuthentication(doAuthentication);
+ method.setFollowRedirects(true);
+ method.setQueryString(prepareParams(id));
+ try {
+
+ int statusCode = client.executeMethod(method);
+
+ if (statusCode != HttpStatus.SC_OK) {
+ throw new IOException("FishEye HTTP call failed with http code: " + method.getStatusCode() + " and message: "
+ + method.getStatusLine());
+ }
+
+ return processResponse(method.getResponseBody());
+
+ } finally {
+ method.releaseConnection();
+ }
+
}
+ /**
+ * Prepare HTTP parameters to call FishEye server.
+ *
+ * @param id of repository to call
+ * @return prepared HTTP parameters
+ */
+ protected NameValuePair[] prepareParams(FishEyeRepositoryConfig id) {
+ List<NameValuePair> params = new ArrayList<NameValuePair>();
+ params.add(new NameValuePair(FISHEYE_PARAM_NAME_REPO, id.getName()));
+ if (id.getPath() != null) {
+ params.add(new NameValuePair(FISHEYE_PARAM_NAME_PATH, id.getPath()));
+ }
+ SimpleDateFormat sdf = new SimpleDateFormat(FISHEYE_PARAM_DATE_FORMAT);
+ if (dateFrom != null) {
+ params.add(new NameValuePair(FISHEYE_PARAM_NAME_MINDATE, sdf.format(dateFrom)));
+ }
+ if (dateTo != null) {
+ params.add(new NameValuePair(FISHEYE_PARAM_NAME_MAXDATE, sdf.format(dateTo)));
+ }
+ return params.toArray(new NameValuePair[] {});
+ }
+
+ /**
+ * Parse HTTP call response into report values.
+ *
+ * @param responseBody to parse
+ * @return values parsed from response
+ * @throws IOException if response is incorrect
+ */
+ protected String[] processResponse(byte[] responseBody) throws IOException {
+ if (responseBody == null || responseBody.length == 0) {
+ throw new IOException("FishEye HTTP call failed: empty response");
+ }
+
+ String retsponse = (new String(responseBody)).trim();
+ String[] responseParsed = retsponse.split(":");
+
+ if (responseParsed.length < 3 || !"ok".equals(responseParsed[0])) {
+ throw new IOException("FishEye HTTP call failed: Bad response: " + retsponse);
+ }
+
+ String[] retvalue = new String[2];
+ retvalue[0] = responseParsed[1];
+ retvalue[1] = responseParsed[2];
+
+ return retvalue;
+ }
+
}
Added: labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/test/java/org/jboss/community/sbs/plugin/reports/monthly/valuesource/FISHEYEValueSourceTest.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/test/java/org/jboss/community/sbs/plugin/reports/monthly/valuesource/FISHEYEValueSourceTest.java (rev 0)
+++ labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/test/java/org/jboss/community/sbs/plugin/reports/monthly/valuesource/FISHEYEValueSourceTest.java 2010-10-21 14:08:24 UTC (rev 35648)
@@ -0,0 +1,249 @@
+package org.jboss.community.sbs.plugin.reports.monthly.valuesource;
+
+import java.io.IOException;
+
+import org.apache.commons.httpclient.NameValuePair;
+import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.commons.httpclient.auth.AuthScope;
+import org.jboss.community.sbs.plugin.reports.monthly.MapBasedSystemConfigProvider;
+import org.jboss.community.sbs.plugin.reports.monthly.config.FishEyeRepositoryConfig;
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.ibm.icu.text.SimpleDateFormat;
+
+/**
+ * Unit test for {@link FISHEYEValueSource}.
+ *
+ * @author Vlastimil Elias (velias at redhat dot com)
+ */
+public class FISHEYEValueSourceTest {
+
+ @Test
+ public void processResponse() throws IOException {
+
+ FISHEYEValueSource tested = new FISHEYEValueSource(null, null, null);
+
+ String[] ret = null;
+ try {
+ ret = tested.processResponse(null);
+ Assert.fail("No exception thrown");
+ } catch (IOException e) {
+ // OK
+ }
+
+ try {
+ ret = tested.processResponse("".getBytes());
+ Assert.fail("No exception thrown");
+ } catch (IOException e) {
+ // OK
+ }
+
+ try {
+ ret = tested.processResponse("fault:Erro xxxx".getBytes());
+ Assert.fail("No exception thrown");
+ } catch (IOException e) {
+ // OK
+ }
+
+ try {
+ ret = tested.processResponse("ok:1".getBytes());
+ Assert.fail("No exception thrown");
+ } catch (IOException e) {
+ // OK
+ }
+
+ try {
+ ret = tested.processResponse("nook:1:10".getBytes());
+ Assert.fail("No exception thrown");
+ } catch (IOException e) {
+ // OK
+ }
+
+ ret = tested.processResponse("ok:1:10".getBytes());
+ Assert.assertEquals("1", ret[0]);
+ Assert.assertEquals("10", ret[1]);
+ }
+
+ @Test
+ public void prepareParams() throws Exception {
+ FISHEYEValueSource tested = new FISHEYEValueSource(null, null, null);
+ SimpleDateFormat sdf = new SimpleDateFormat(FISHEYEValueSource.FISHEYE_PARAM_DATE_FORMAT);
+
+ // repo only set
+ FishEyeRepositoryConfig id = new FishEyeRepositoryConfig("REPO");
+ NameValuePair[] val = tested.prepareParams(id);
+
+ Assert.assertEquals("REPO", getParameter(val, FISHEYEValueSource.FISHEYE_PARAM_NAME_REPO));
+ Assert.assertNull(getParameter(val, FISHEYEValueSource.FISHEYE_PARAM_NAME_PATH));
+ Assert.assertNull(getParameter(val, FISHEYEValueSource.FISHEYE_PARAM_NAME_MINDATE));
+ Assert.assertNull(getParameter(val, FISHEYEValueSource.FISHEYE_PARAM_NAME_MAXDATE));
+
+ // all params set
+ id.setPath("/path/t");
+ tested.dateFrom = sdf.parse("2010-10-01");
+ tested.dateTo = sdf.parse("2010-10-31");
+ val = tested.prepareParams(id);
+
+ Assert.assertEquals("REPO", getParameter(val, FISHEYEValueSource.FISHEYE_PARAM_NAME_REPO));
+ Assert.assertEquals("/path/t", getParameter(val, FISHEYEValueSource.FISHEYE_PARAM_NAME_PATH));
+ Assert.assertEquals("2010-10-1", getParameter(val, FISHEYEValueSource.FISHEYE_PARAM_NAME_MINDATE));
+ Assert.assertEquals("2010-10-31", getParameter(val, FISHEYEValueSource.FISHEYE_PARAM_NAME_MAXDATE));
+ }
+
+ protected String getParameter(NameValuePair[] val, String name) {
+ for (NameValuePair p : val) {
+ if (p.getName().equals(name)) {
+ return p.getValue();
+ }
+ }
+ return null;
+ }
+
+ @Test
+ public void initHttpClient() {
+ MapBasedSystemConfigProvider config = new MapBasedSystemConfigProvider();
+ config.addConfigProperty(FISHEYEValueSource.CONFIG_KEY_DEBUG, "false");
+
+ FISHEYEValueSource tested = null;
+ try {
+ tested = new FISHEYEValueSource(config, null, null);
+ Assert.fail("IllegalArgumentException not thrown");
+ } catch (IllegalArgumentException e) {
+ // OK
+ }
+
+ // no authentication
+ config.addConfigProperty(FISHEYEValueSource.CONFIG_KEY_URL, "http://test.jboss.org");
+ tested = new FISHEYEValueSource(config, null, null);
+ Assert.assertNotNull(tested.client);
+ Assert.assertFalse(tested.debug);
+ Assert.assertFalse(tested.doAuthentication);
+
+ // with authentication
+ config.addConfigProperty(FISHEYEValueSource.CONFIG_KEY_USERNAME, "un");
+ config.addConfigProperty(FISHEYEValueSource.CONFIG_KEY_PWD, "pw");
+ tested = new FISHEYEValueSource(config, null, null);
+ Assert.assertNotNull(tested.client);
+ Assert.assertFalse(tested.debug);
+ Assert.assertTrue(tested.doAuthentication);
+ UsernamePasswordCredentials cr = (UsernamePasswordCredentials) tested.client.getState().getCredentials(
+ new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM));
+ Assert.assertEquals("un", cr.getUserName());
+ Assert.assertEquals("pw", cr.getPassword());
+
+ }
+
+ @Test
+ public void getValues_DEBUG() throws Exception {
+
+ // test debug mode of provider
+ MapBasedSystemConfigProvider config = new MapBasedSystemConfigProvider();
+ config.addConfigProperty(FISHEYEValueSource.CONFIG_KEY_DEBUG, "true");
+
+ FISHEYEValueSource tested = new FISHEYEValueSource(config, null, null);
+
+ String[] val = null;
+ try {
+ val = tested.getValues(null);
+ Assert.fail();
+ } catch (IllegalArgumentException e) {
+ // OK
+ }
+
+ FishEyeRepositoryConfig id = new FishEyeRepositoryConfig("REPO");
+ val = tested.getValues(id);
+ Assert.assertEquals(2, val.length);
+ Assert.assertEquals("0", val[0]);
+ Assert.assertEquals("0", val[1]);
+ }
+
+ @Test
+ public void getValues_REMOTE_NOAUTH() throws Exception {
+ HttpFISHEYEMockServer server = new HttpFISHEYEMockServer();
+ try {
+
+ MapBasedSystemConfigProvider config = new MapBasedSystemConfigProvider();
+ config.addConfigProperty(FISHEYEValueSource.CONFIG_KEY_DEBUG, "false");
+ config.addConfigProperty(FISHEYEValueSource.CONFIG_KEY_URL, "http://localhost:" + HttpFISHEYEMockServer.PORT);
+
+ FISHEYEValueSource tested = new FISHEYEValueSource(config, null, null);
+
+ // all ok
+ server.connect();
+ FishEyeRepositoryConfig id = new FishEyeRepositoryConfig("REPO");
+ String[] val = tested.getValues(id);
+ server.disconnect();
+ Assert.assertEquals(2, val.length);
+ Assert.assertEquals("10", val[0]);
+ Assert.assertEquals("100", val[1]);
+ Assert.assertEquals("REPO", server.p_repo);
+ Assert.assertNull(server.p_path);
+ Assert.assertNull(server.p_mindate);
+ Assert.assertNull(server.p_maxdate);
+ Assert.assertNull(server.h_auth);
+
+ // data error from server
+ try {
+ server.dataok = false;
+ server.connect();
+ val = tested.getValues(id);
+ Assert.fail();
+ } catch (IOException e) {
+ // ok
+ } finally {
+ server.disconnect();
+ }
+
+ try {
+ server.dataok = true;
+ server.statusok = false;
+ server.connect();
+ val = tested.getValues(id);
+ Assert.fail();
+ } catch (IOException e) {
+ // ok
+ } finally {
+ server.disconnect();
+ }
+
+ } finally {
+ server.disconnect();
+ }
+ }
+
+ @Test
+ public void getValues_REMOTE_AUTH() throws Exception {
+ HttpFISHEYEMockServer server = new HttpFISHEYEMockServer();
+ server.doauth = true;
+ try {
+
+ MapBasedSystemConfigProvider config = new MapBasedSystemConfigProvider();
+ config.addConfigProperty(FISHEYEValueSource.CONFIG_KEY_DEBUG, "false");
+ config.addConfigProperty(FISHEYEValueSource.CONFIG_KEY_URL, "http://localhost:" + HttpFISHEYEMockServer.PORT);
+ config.addConfigProperty(FISHEYEValueSource.CONFIG_KEY_USERNAME, "user");
+ config.addConfigProperty(FISHEYEValueSource.CONFIG_KEY_PWD, "pwd");
+
+ FISHEYEValueSource tested = new FISHEYEValueSource(config, null, null);
+
+ // all ok
+ server.connect();
+ FishEyeRepositoryConfig id = new FishEyeRepositoryConfig("REPO");
+ id.setPath("/path");
+ String[] val = tested.getValues(id);
+ server.disconnect();
+ Assert.assertEquals(2, val.length);
+ Assert.assertEquals("10", val[0]);
+ Assert.assertEquals("100", val[1]);
+ Assert.assertEquals("REPO", server.p_repo);
+ Assert.assertEquals("/path", server.p_path);
+ Assert.assertNull(server.p_mindate);
+ Assert.assertNull(server.p_maxdate);
+ Assert.assertNotNull(server.h_auth);
+
+ } finally {
+ server.disconnect();
+ }
+
+ }
+}
Added: labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/test/java/org/jboss/community/sbs/plugin/reports/monthly/valuesource/HttpFISHEYEMockServer.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/test/java/org/jboss/community/sbs/plugin/reports/monthly/valuesource/HttpFISHEYEMockServer.java (rev 0)
+++ labs/jbosslabs/labs-3.0-build/integration/sbs/reports/trunk/src/test/java/org/jboss/community/sbs/plugin/reports/monthly/valuesource/HttpFISHEYEMockServer.java 2010-10-21 14:08:24 UTC (rev 35648)
@@ -0,0 +1,99 @@
+package org.jboss.community.sbs.plugin.reports.monthly.valuesource;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+
+import org.simpleframework.http.Query;
+import org.simpleframework.http.Request;
+import org.simpleframework.http.Response;
+import org.simpleframework.http.core.Container;
+import org.simpleframework.transport.connect.Connection;
+import org.simpleframework.transport.connect.SocketConnection;
+
+/**
+ * HTTP server mock used to simulate FishEye API used in {@link FISHEYEValueSource}.
+ *
+ * @author Vlastimil Elias (velias at redhat dot com)
+ */
+public class HttpFISHEYEMockServer implements Container {
+
+ public static final int PORT = 8070;
+
+ protected boolean dataok = true;
+ protected boolean statusok = true;
+ protected boolean doauth = false;
+
+ Connection connection = null;
+
+ String p_repo = null;
+ String p_path = null;
+ String p_mindate = null;
+ String p_maxdate = null;
+ String h_auth = null;
+
+ public void connect() throws Exception {
+ if (connection != null) {
+ connection.close();
+ connection = null;
+ }
+
+ connection = new SocketConnection(this);
+ SocketAddress address = new InetSocketAddress(PORT);
+ connection.connect(address);
+ }
+
+ public void disconnect() throws Exception {
+ if (connection != null) {
+ connection.close();
+ connection = null;
+ }
+ }
+
+ @Override
+ public void handle(Request request, Response response) {
+
+ try {
+
+ PrintStream body = response.getPrintStream();
+
+ Query q = request.getQuery();
+
+ p_repo = q.get(FISHEYEValueSource.FISHEYE_PARAM_NAME_REPO);
+ p_path = q.get(FISHEYEValueSource.FISHEYE_PARAM_NAME_PATH);
+ p_mindate = q.get(FISHEYEValueSource.FISHEYE_PARAM_NAME_MINDATE);
+ p_maxdate = q.get(FISHEYEValueSource.FISHEYE_PARAM_NAME_MAXDATE);
+
+ h_auth = request.getValue("Authorization");
+
+ long time = System.currentTimeMillis();
+
+ response.set("Content-Type", "text/plain");
+ response.set("Server", "HelloWorld/1.0 (Simple 4.0)");
+ response.setDate("Date", time);
+ response.setDate("Last-Modified", time);
+
+ if (doauth && h_auth == null) {
+ response.setCode(401);
+ response.set("WWW-Authenticate", "Basic realm=\"Secure Area\"");
+ body.close();
+ } else {
+
+ if (!statusok) {
+ response.setCode(500);
+ body.close();
+ } else {
+ if (dataok) {
+ body.println("ok:10:100");
+ } else {
+ body.println("fault:Error in DB");
+ }
+ body.close();
+ }
+ }
+ } catch (IOException e) {
+ }
+ }
+
+}
More information about the jboss-svn-commits
mailing list