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

added Marriage Proposal API #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/main/java/hostirosti/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public final class Constants {
/**
* Api version constant.
*/
public static final String API_VERSION = "v1";
public static final String API_VERSION = "v2";

/**
* Disable public constructor.
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/hostirosti/JavaRESTExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,17 @@ public final Response getHelloWorld() {
return Response.status(OK)
.entity(gson.toJson(new HelloWorld())).build();
}

/**
* REST API endpoint to get Marriage Proposal Answers per HTTP GET.
* @return Response with Marriage Proposal object and status code HTTP_OK
*/
@GET
@Path(Constants.API_PREFIX + Constants.API_VERSION + "/marriage-proposal")
@Produces(MediaType.APPLICATION_JSON)
public final Response getMarriageProposal() {
Gson gson = new Gson();
return Response.status(OK)
.entity(gson.toJson(new MarriageProposal())).build();
}
}
68 changes: 68 additions & 0 deletions src/main/java/hostirosti/MarriageProposal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package hostirosti;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.util.HashMap;
import java.util.Map;

/**
* Marriage Proposal Statements for Demo
*/
public class MarriageProposal {

/**
* The ultimate question
*/
private final String question = "Will you marry me?";

/**
* The ultimate answers from GitHub, JenkinsCI, GCP
*/
private Map<String, String> answers = new HashMap<String, String>();


/**
* Initialize Marriage Proposal object and set hard-coded answers for
* Demo.
*/
public MarriageProposal() {
answers.put("JenkinsCI", "I do!");
answers.put("GitHub","Hmm,... ok I do!");
answers.put("Google", "Is MAYBE an option?");
}

/**
*
* @param obj the reference object with which to compare
* @return true if object is the same
*/
@Override
public boolean equals(final Object obj) {
if (!(obj instanceof MarriageProposal)) {
return false;
}
if (obj == this) {
return true;
}

MarriageProposal marriageProposal = (MarriageProposal) obj;
return new EqualsBuilder().
append(question, marriageProposal.question).
append(answers, marriageProposal.answers).
isEquals();
}

/**
*
* @return hash code value for this object
*/
@Override
public int hashCode() {

return new HashCodeBuilder(33, 79).
append(question).
append(answers).
toHashCode();
}
}
57 changes: 33 additions & 24 deletions src/non-packaged-resources/jmeter/JavaRESTExampleJMeter.jmx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
<boolProp name="HTTPSampler.auto_redirects">true</boolProp>
<stringProp name="RestSampler.base_host">${testserver.endpoint.protocol}://${testserver.endpoint.host}/</stringProp>
<stringProp name="RestSampler.resource">api/v1/hello-world</stringProp>
<stringProp name="RestSampler.resource">api/v2/hello-world</stringProp>
<stringProp name="RestSampler.port_number">${testserver.endpoint.port}</stringProp>
<stringProp name="RestSampler.request_headers"></stringProp>
</com.atlantbh.jmeter.plugins.rest.RestSampler>
Expand All @@ -62,34 +62,43 @@
</com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.JSONPathAssertion>
<hashTree/>
</hashTree>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="false">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<com.atlantbh.jmeter.plugins.rest.RestSampler guiclass="com.atlantbh.jmeter.plugins.rest.gui.RestGui" testclass="com.atlantbh.jmeter.plugins.rest.RestSampler" testname="Rest Sampler" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain"></stringProp>
<stringProp name="HTTPSampler.port"></stringProp>
<stringProp name="HTTPSampler.connect_timeout"></stringProp>
<stringProp name="HTTPSampler.response_timeout"></stringProp>
<stringProp name="HTTPSampler.protocol"></stringProp>
<stringProp name="HTTPSampler.contentEncoding"></stringProp>
<stringProp name="HTTPSampler.path">/api/v1/hello-world</stringProp>
<stringProp name="RestSampler.request_body"></stringProp>
<stringProp name="HTTPSampler.method">GET</stringProp>
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
<boolProp name="HTTPSampler.auto_redirects">false</boolProp>
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
<boolProp name="HTTPSampler.monitor">false</boolProp>
<stringProp name="HTTPSampler.embedded_url_re"></stringProp>
</HTTPSamplerProxy>
<boolProp name="HTTPSampler.auto_redirects">true</boolProp>
<stringProp name="RestSampler.base_host">${testserver.endpoint.protocol}://${testserver.endpoint.host}/</stringProp>
<stringProp name="RestSampler.resource">api/v2/marriage-proposal</stringProp>
<stringProp name="RestSampler.port_number">${testserver.endpoint.port}</stringProp>
<stringProp name="RestSampler.request_headers"></stringProp>
</com.atlantbh.jmeter.plugins.rest.RestSampler>
<hashTree>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="false">
<collectionProp name="Asserion.test_strings">
<stringProp name="1070695754">{&quot;helloWorld&quot;:&quot;Hello World! :)&quot;}</stringProp>
</collectionProp>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
<intProp name="Assertion.test_type">8</intProp>
</ResponseAssertion>
<com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.JSONPathAssertion guiclass="com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.gui.JSONPathAssertionGui" testclass="com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.JSONPathAssertion" testname="JSON Path Assertion" enabled="true">
<stringProp name="JSON_PATH">question</stringProp>
<stringProp name="EXPECTED_VALUE">Will you marry me?</stringProp>
<boolProp name="JSONVALIDATION">true</boolProp>
</com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.JSONPathAssertion>
<hashTree/>
<com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.JSONPathAssertion guiclass="com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.gui.JSONPathAssertionGui" testclass="com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.JSONPathAssertion" testname="JSON Path Assertion" enabled="true">
<stringProp name="JSON_PATH">answers.GitHub</stringProp>
<stringProp name="EXPECTED_VALUE">Hmm,... ok I do!</stringProp>
<boolProp name="JSONVALIDATION">true</boolProp>
</com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.JSONPathAssertion>
<hashTree/>
<com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.JSONPathAssertion guiclass="com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.gui.JSONPathAssertionGui" testclass="com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.JSONPathAssertion" testname="JSON Path Assertion" enabled="true">
<stringProp name="JSON_PATH">answers.Google</stringProp>
<stringProp name="EXPECTED_VALUE">Is MAYBE an option?</stringProp>
<boolProp name="JSONVALIDATION">true</boolProp>
</com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.JSONPathAssertion>
<hashTree/>
<com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.JSONPathAssertion guiclass="com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.gui.JSONPathAssertionGui" testclass="com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.JSONPathAssertion" testname="JSON Path Assertion" enabled="true">
<stringProp name="JSON_PATH">answers.JenkinsCI</stringProp>
<stringProp name="EXPECTED_VALUE">I do!</stringProp>
<boolProp name="JSONVALIDATION">true</boolProp>
</com.atlantbh.jmeter.plugins.jsonutils.jsonpathassertion.JSONPathAssertion>
<hashTree/>
</hashTree>
<ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="true">
Expand Down
35 changes: 32 additions & 3 deletions src/test/java/hostirosti/JavaRESTExampleIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hostirosti;

import static com.eclipsesource.restfuse.Assert.*;
import static org.junit.Assert.assertTrue;

import com.eclipsesource.restfuse.HttpJUnitRunner;
import com.eclipsesource.restfuse.Destination;
Expand Down Expand Up @@ -51,19 +52,47 @@ public String getDestinationFromEnvironment() {
public void checkApiInfoResponse() {
assertOk(response);

String jsonResponse = response.getBody();

assertTrue(jsonResponse.contains(Constants.API_PREFIX));
assertTrue(jsonResponse.contains(Constants.API_VERSION));

Gson gson = new Gson();
ApiInfo apiInfo = gson.fromJson(response.getBody(), ApiInfo.class);

ApiInfo apiInfo = gson.fromJson(jsonResponse, ApiInfo.class);

Assert.assertEquals(apiInfo, new ApiInfo());
}

@HttpTest(method = Method.GET, path = "/api/v1/hello-world")
@HttpTest(method = Method.GET, path = Constants.API_PREFIX + Constants.API_VERSION +"/hello-world")
public void checkHelloWorldResponse() {
assertOk(response);

String jsonResponse = response.getBody();

assertTrue(jsonResponse.contains("Hello Googlers! :)"));
assertTrue(jsonResponse.contains("helloWorld"));

Gson gson = new Gson();
HelloWorld helloWord = gson.fromJson(response.getBody(), HelloWorld.class);
HelloWorld helloWord = gson.fromJson(jsonResponse, HelloWorld.class);

Assert.assertEquals(helloWord, new HelloWorld());
}

@HttpTest(method = Method.GET, path = Constants.API_PREFIX + Constants.API_VERSION +"/marriage-proposal")
public void checkMarriageProposalResponse() {
assertOk(response);

String jsonResponse = response.getBody();

assertTrue(jsonResponse.contains("question"));
assertTrue(jsonResponse.contains("JenkinsCI"));
assertTrue(jsonResponse.contains("GitHub"));
assertTrue(jsonResponse.contains("Google"));

Gson gson = new Gson();
MarriageProposal marriageProposal = gson.fromJson(jsonResponse, MarriageProposal.class);

Assert.assertEquals(marriageProposal, new MarriageProposal());
}
}
95 changes: 87 additions & 8 deletions src/test/java/hostirosti/JavaRESTExampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,118 @@
import static org.junit.Assert.assertTrue;

import com.google.gson.Gson;
import hostirosti.ApiInfo;
import hostirosti.HelloWorld;
import hostirosti.JavaRESTExample;
import org.junit.Before;
import org.junit.BeforeClass;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.Map;

@RunWith(JUnit4.class)
public class JavaRESTExampleTest extends JavaRESTExampleTestBase {
JavaRESTExample jre = new JavaRESTExample();
Gson gson = new Gson();

/**
* Test ApiInfo API Response content
*/
@Test
public void testGetApiInfo() {
Response response = jre.getApiInfo();
ApiInfo apiInfo = gson.fromJson((String)response.getEntity(), ApiInfo.class);
String jsonResponse = (String)response.getEntity();

assertTrue(jsonResponse.contains(Constants.API_PREFIX));
assertTrue(jsonResponse.contains(Constants.API_VERSION));

ApiInfo apiInfo = gson.fromJson(jsonResponse, ApiInfo.class);

assertEquals(new ApiInfo(), apiInfo);
}


/**
* Test ApiInfo hashCode function
*/
@Test
public void testApiInfoHashCode() {
ApiInfo apiInfo = new ApiInfo();

assertEquals(apiInfo.hashCode(),
new HashCodeBuilder(29, 83).
append(Constants.API_PREFIX).
append(Constants.API_VERSION).
toHashCode());
}


/**
* Test HelloWorld API Response content
*/
@Test
public void testHelloWorld() {
public void testGetHelloWorld() {
Response response = jre.getHelloWorld();
String jsonResponse = (String)response.getEntity();

assertTrue(jsonResponse.contains("Hello Googlers! :)"));
assertTrue(jsonResponse.contains("helloWorld"));

HelloWorld helloWorld = gson.fromJson((String)response.getEntity(), HelloWorld.class);
HelloWorld helloWorld = gson.fromJson(jsonResponse, HelloWorld.class);

assertEquals(new HelloWorld(), helloWorld);
}


/**
* Test HelloWorld hashCode function
*/
@Test
public void testHelloWorldHashCode() {
HelloWorld helloWorld = new HelloWorld();

assertEquals(helloWorld.hashCode(),
new HashCodeBuilder(27, 87).
append("Hello Googlers! :)").
toHashCode());
}


/**
* Test MarriageProposal API Response content
*/
@Test
public void testGetMarriageProposal() {
Response response = jre.getMarriageProposal();
String jsonResponse = (String)response.getEntity();

assertTrue(jsonResponse.contains("question"));
assertTrue(jsonResponse.contains("JenkinsCI"));
assertTrue(jsonResponse.contains("GitHub"));
assertTrue(jsonResponse.contains("Google"));

MarriageProposal marriageProposal = gson.fromJson(jsonResponse, MarriageProposal.class);

assertEquals(new MarriageProposal(), marriageProposal);
}


/**
* Test MarriageProposal hashCode function
*/
@Test
public void testMarriageProposalHashCode() {
MarriageProposal marriageProposal = new MarriageProposal();

Map<String, String> answers = new HashMap<String, String>();
answers.put("JenkinsCI", "I do!");
answers.put("GitHub","Hmm,... ok I do!");
answers.put("Google", "Is MAYBE an option?");

assertEquals(marriageProposal.hashCode(),
new HashCodeBuilder(33, 79).
append("Will you marry me?").
append(answers).
toHashCode());
}
}