From 15683eda2a3185b9c7ad357f468755b0c4d11a86 Mon Sep 17 00:00:00 2001 From: Vincent Behar Date: Sun, 18 Sep 2011 18:14:08 +0200 Subject: [PATCH] use betamax to unit-test HTTP requests without a live RunDeck instance --- pom.xml | 16 +++++- .../org/rundeck/api/RundeckClientTest.java | 55 +++++++++++++++++++ src/test/resources/betamax.properties | 4 ++ .../resources/betamax/tapes/get_projects.yaml | 20 +++++++ 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/rundeck/api/RundeckClientTest.java create mode 100644 src/test/resources/betamax.properties create mode 100644 src/test/resources/betamax/tapes/get_projects.yaml diff --git a/pom.xml b/pom.xml index 7cc80db..ee14ddf 100644 --- a/pom.xml +++ b/pom.xml @@ -416,12 +416,12 @@ org.apache.httpcomponents httpclient - 4.1.1 + 4.1.2 org.apache.httpcomponents httpmime - 4.1.1 + 4.1.2 @@ -452,6 +452,18 @@ 4.8.2 test + + com.github.robfletcher + betamax + 1.0 + test + + + org.codehaus.groovy + groovy-all + 1.7.10 + test + diff --git a/src/test/java/org/rundeck/api/RundeckClientTest.java b/src/test/java/org/rundeck/api/RundeckClientTest.java new file mode 100644 index 0000000..19081ba --- /dev/null +++ b/src/test/java/org/rundeck/api/RundeckClientTest.java @@ -0,0 +1,55 @@ +/* + * Copyright 2011 Vincent Behar + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.rundeck.api; + +import java.util.List; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.rundeck.api.domain.RundeckProject; +import betamax.Betamax; +import betamax.Recorder; + +/** + * Test the {@link RundeckClient}. Uses betamax to unit-test HTTP requests without a live RunDeck instance. + * + * @author Vincent Behar + */ +public class RundeckClientTest { + + @Rule + public Recorder recorder = new Recorder(); + + private RundeckClient client; + + @Test + @Betamax(tape = "get_projects") + public void getProjects() throws Exception { + List projects = client.getProjects(); + Assert.assertEquals(1, projects.size()); + Assert.assertEquals("test", projects.get(0).getName()); + Assert.assertNull(projects.get(0).getDescription()); + } + + @Before + public void setUp() throws Exception { + // not that you can put whatever here, because we don't actually connect to the RunDeck instance + // but instead use betamax as a proxy to serve the previously recorded tapes (in src/test/resources) + client = new RundeckClient("http://rundeck.local:4440", "PVnN5K3OPc5vduS3uVuVnEsD57pDC5pd"); + } + +} diff --git a/src/test/resources/betamax.properties b/src/test/resources/betamax.properties new file mode 100644 index 0000000..e212574 --- /dev/null +++ b/src/test/resources/betamax.properties @@ -0,0 +1,4 @@ +betamax.tapeRoot=src/test/resources/betamax/tapes +betamax.proxyPort=1337 +betamax.proxyTimeout=5000 +betamax.defaultMode=READ_ONLY \ No newline at end of file diff --git a/src/test/resources/betamax/tapes/get_projects.yaml b/src/test/resources/betamax/tapes/get_projects.yaml new file mode 100644 index 0000000..e07efd6 --- /dev/null +++ b/src/test/resources/betamax/tapes/get_projects.yaml @@ -0,0 +1,20 @@ +!tape +name: get_projects +interactions: +- recorded: 2011-09-18T16:04:45.973Z + request: + method: GET + uri: http://rundeck.local:4440/api/2/projects + headers: + Host: rundeck.local:4440 + Proxy-Connection: Keep-Alive + User-Agent: RunDeck API Java Client 2 + X-RunDeck-Auth-Token: PVnN5K3OPc5vduS3uVuVnEsD57pDC5pd + response: + status: 200 + headers: + Content-Type: text/xml; charset=utf-8 + Expires: Thu, 01 Jan 1970 00:00:00 GMT + Server: Jetty(6.1.21) + Set-Cookie: JSESSIONID=mxrbh6byhvxt;Path=/ + body: test