restassured_cli 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +139 -0
  5. data/Rakefile +3 -0
  6. data/bin/console +14 -0
  7. data/bin/restassured_cli +46 -0
  8. data/bin/setup +7 -0
  9. data/lib/restassured_cli.rb +77 -0
  10. data/lib/restassured_cli/createcontroller.rb +81 -0
  11. data/lib/restassured_cli/filenames.rb +69 -0
  12. data/lib/restassured_cli/source/maven/pom.xml +14 -0
  13. data/lib/restassured_cli/source/maven/testing/README.md +26 -0
  14. data/lib/restassured_cli/source/maven/testing/pom.xml +27 -0
  15. data/lib/restassured_cli/source/maven/testing/sampleservice-perf-tests/LoginTest.java +14 -0
  16. data/lib/restassured_cli/source/maven/testing/sampleservice-perf-tests/pom.xml +14 -0
  17. data/lib/restassured_cli/source/maven/testing/sampleservice-perf-tests/testng-perf-notests.xml +9 -0
  18. data/lib/restassured_cli/source/maven/testing/sampleservice-perf-tests/testng-perf.xml +9 -0
  19. data/lib/restassured_cli/source/maven/testing/sampleservice-production-tests/logback.xml +15 -0
  20. data/lib/restassured_cli/source/maven/testing/sampleservice-production-tests/pom.xml +21 -0
  21. data/lib/restassured_cli/source/maven/testing/sampleservice-production-tests/testng-production.xml +9 -0
  22. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/default/SampleTest.java +30 -0
  23. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/default/testng-ci.xml +13 -0
  24. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/default/testng-e2e.xml +13 -0
  25. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/default/testng-qa.xml +9 -0
  26. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/full/SampleComponentTest.java +312 -0
  27. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/full/SampleServiceIntegrationTest.java +129 -0
  28. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/full/testng-ci.xml +13 -0
  29. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/full/testng-e2e.xml +9 -0
  30. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/full/testng-qa.xml +9 -0
  31. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/logback.xml +15 -0
  32. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/pom.xml +22 -0
  33. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/resources/ci/sample.properties +0 -0
  34. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/resources/default/sample.properties +2 -0
  35. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/resources/e2e/sample.properties +1 -0
  36. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/resources/qa/sample.properties +0 -0
  37. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/default/BaseConstant.java +48 -0
  38. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/default/InternalConfigManager.java +52 -0
  39. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/default/ServiceTestBase.java +103 -0
  40. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/default/sample/base/Constant.java +12 -0
  41. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/default/sample/base/SampleTestBase.java +15 -0
  42. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/full/Customer.java +83 -0
  43. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/full/Phone.java +31 -0
  44. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/full/SampleServiceHelper.java +8 -0
  45. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/full/sample/base/Constant.java +12 -0
  46. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/full/sample/base/SampleTestBase.java +63 -0
  47. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/pom.xml +14 -0
  48. data/lib/restassured_cli/source/maven/testing/sampleservice-test-parent-pom/pom.xml +225 -0
  49. data/lib/restassured_cli/source/maven/testing/sampleservice-test-runner/pom.xml +84 -0
  50. data/lib/restassured_cli/userinterface.rb +55 -0
  51. data/lib/restassured_cli/version.rb +3 -0
  52. data/spec/restassured_cli_spec.rb +84 -0
  53. data/spec/spec_helper.rb +14 -0
  54. metadata +141 -0
@@ -0,0 +1,129 @@
1
+ package GROUPID.tests.serviceintegration;
2
+
3
+ import GROUPID.tests.library.sample.base.SampleTestBase;
4
+
5
+ import com.jayway.restassured.http.ContentType;
6
+ import org.testng.annotations.BeforeClass;
7
+ import org.testng.annotations.Test;
8
+
9
+ import java.util.concurrent.Callable;
10
+ import java.util.concurrent.TimeUnit;
11
+
12
+ import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
13
+ import static com.github.tomakehurst.wiremock.client.WireMock.any;
14
+ import static com.github.tomakehurst.wiremock.client.WireMock.get;
15
+ import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
16
+ import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED;
17
+ import static com.jayway.awaitility.Awaitility.await;
18
+ import static com.jayway.restassured.RestAssured.given;
19
+
20
+ /**
21
+ * Sample test to demonstrate working with an asynchronous batch transaction.
22
+ * TODO: WireMock is used to stub responses but <b>should be removed for service integration testing</b>.
23
+ * Stubbing in this test is only for demonstrating purpose.
24
+ * @author eing
25
+ */
26
+ public class SampleServiceIntegrationTest extends SampleTestBase {
27
+
28
+ public SampleServiceIntegrationTest() {
29
+ }
30
+
31
+ /**
32
+ * Add stubs to WireMock server for this test.
33
+ * TODO: Remove this for actual service integration testing.
34
+ */
35
+ @BeforeClass
36
+ public void setup() {
37
+ //@formatter:off
38
+ // returns 202: request has been accepted for processing, but the processing has not been completed
39
+ wireMockServer.
40
+ givenThat(
41
+ any(urlMatching("/batch")).
42
+ willReturn(aResponse().
43
+ withHeader("Content-Type", ContentType.JSON.toString()).
44
+ withHeader("X-AX-TRANSACTIONID", "1290").
45
+ withStatus(202)));
46
+ // returns
47
+ // 1. state:IN_PROGRESS in header for first and second requests
48
+ // 2. state:COMPLETE in header on the third request
49
+ wireMockServer.
50
+ givenThat(
51
+ get(urlMatching("/batch/[0-9]+/status")).
52
+ inScenario("Get batch status").
53
+ whenScenarioStateIs(STARTED).
54
+ willReturn(aResponse().
55
+ withStatus(200).
56
+ withHeader("Content-Type", ContentType.JSON.toString()).
57
+ withHeader("state", "IN_PROGRESS")).
58
+ willSetStateTo("Second get status attempt"));
59
+
60
+ wireMockServer.
61
+ givenThat(
62
+ get(urlMatching("/batch/[0-9]+/status")).
63
+ inScenario("Get batch status").
64
+ whenScenarioStateIs("Second get status attempt").
65
+ willReturn(aResponse().
66
+ withStatus(200).
67
+ withHeader("Content-Type", ContentType.JSON.toString()).
68
+ withHeader("state", "IN_PROGRESS")).
69
+ willSetStateTo("Third get status attempt"));
70
+
71
+ wireMockServer.
72
+ givenThat(
73
+ get(urlMatching("/batch/[0-9]+/status")).
74
+ inScenario("Get batch status").
75
+ whenScenarioStateIs("Third get status attempt").
76
+ willReturn(aResponse().
77
+ withStatus(200).
78
+ withHeader("Content-Type", ContentType.JSON.toString()).
79
+ withHeader("state", "COMPLETE")).
80
+ willSetStateTo("Batch activity completed"));
81
+ //@formatter:on
82
+ }
83
+
84
+ /**
85
+ * Example of an asynchronous request and how to use awaitability.
86
+ */
87
+ @Test
88
+ public void testAsyncOperation() throws Exception {
89
+ /**
90
+ * Typically an identifier is returned to the user on a batch process
91
+ * so that you can use it to poll for its status.
92
+ */
93
+ //@formatter:off
94
+ String batchId =
95
+ given().
96
+ spec(requestSpec).
97
+ when().
98
+ post("/batch").
99
+ then().
100
+ statusCode(202).
101
+ extract().header("X-AX-TRANSACTIONID");
102
+ //@formatter:on
103
+ await().atMost(3, TimeUnit.SECONDS).until(batchActivityCompleted(batchId));
104
+ }
105
+
106
+ /**
107
+ * Callback method for Java7. In Java8, you can code it inline in the test i.e.
108
+ * await().atMost(3, TimeUnit.SECONDS).until(() -> batchActivityCompleted(batchId));
109
+ * @param batchId batch job identifier to check status on
110
+ * @return true if state is completed
111
+ * @throws Exception
112
+ */
113
+ private Callable<Boolean> batchActivityCompleted(final String batchId) throws Exception {
114
+ return new Callable<Boolean>() {
115
+ public Boolean call() throws Exception {
116
+ //@formatter:off
117
+ String state =
118
+ given().
119
+ spec(requestSpec).
120
+ when().
121
+ get("/batch/{batchId}/status", batchId).
122
+ thenReturn().header("state");
123
+ //@formatter:on
124
+ return state.equals("COMPLETE");
125
+ }
126
+ };
127
+ }
128
+
129
+ }
@@ -0,0 +1,13 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
3
+ <suite name="SERVICENAME-ci-tests">
4
+ <test name="SERVICENAME service BATS">
5
+ <classes>
6
+ <class name="GROUPID.tests.service.SampleComponentTest">
7
+ <methods>
8
+ <include name="testSimpleGet"/>
9
+ </methods>
10
+ </class>
11
+ </classes>
12
+ </test>
13
+ </suite>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
3
+ <suite name="SERVICENAME-e2e-tests">
4
+ <test name="SERVICENAME service integration tests">
5
+ <packages>
6
+ <package name="GROUPID.tests.serviceintegration.*"/>
7
+ </packages>
8
+ </test>
9
+ </suite>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
3
+ <suite name="SERVICENAME-qa-tests">
4
+ <test name="SERVICENAME service tests">
5
+ <packages>
6
+ <package name="GROUPID.tests.service.*"/>
7
+ </packages>
8
+ </test>
9
+ </suite>
@@ -0,0 +1,15 @@
1
+ <configuration>
2
+ <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener"/>
3
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4
+ <!-- encoders are assigned the type
5
+ ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
6
+ <encoder>
7
+ <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
8
+ </encoder>
9
+ </appender>
10
+
11
+ <root level="DEBUG">
12
+ <appender-ref ref="STDOUT"/>
13
+ </root>
14
+
15
+ </configuration>
@@ -0,0 +1,22 @@
1
+ <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
2
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
+ <parent>
4
+ <groupId>GROUPID.tests</groupId>
5
+ <artifactId>SERVICENAME-test-parent-pom</artifactId>
6
+ <version>PROJECTVERSION</version>
7
+ <relativePath>../SERVICENAME-test-parent-pom</relativePath>
8
+ </parent>
9
+
10
+ <modelVersion>4.0.0</modelVersion>
11
+ <artifactId>SERVICENAME-service-tests</artifactId>
12
+ <name>SERVICENAME-service-tests</name>
13
+
14
+ <dependencies>
15
+ <dependency>
16
+ <groupId>GROUPID.tests</groupId>
17
+ <artifactId>SERVICENAME-test-library</artifactId>
18
+ <version>${project.version}</version>
19
+ </dependency>
20
+ </dependencies>
21
+
22
+ </project>
@@ -0,0 +1,48 @@
1
+ package GROUPID.tests.library.base;
2
+
3
+ /**
4
+ * Constants used by ServiceTestBase and InternalConfigManager.
5
+ * TODO: This package should be in a separate project and added as a dependency.
6
+ * For the purpose of demo, keeping it in the test library module.
7
+ */
8
+ public final class BaseConstant {
9
+
10
+ /**
11
+ * Private constructor to prevent instantiation.
12
+ */
13
+ private BaseConstant() {
14
+ }
15
+
16
+ /**
17
+ * Key in property file to look for for user specified base url e.g. http://www.intuit.com
18
+ */
19
+ public static final String HTTP_PROPERTY_BASEURL = "http.baseurl";
20
+
21
+ /**
22
+ * Key in property file to look for user specified port.
23
+ */
24
+ public static final String HTTP_PROPERTY_PORT = "http.port";
25
+
26
+ /**
27
+ * Default directory containing properties file where it will contain all common properties
28
+ * agnostic of the environment e.g. basePath
29
+ */
30
+ public static final String DEFAULT_CONFIG_DIRECTORY = "/default";
31
+
32
+ /**
33
+ * Attribute name to read in environment value i.e. -Dtargetenv=[ci|qa|e2e|perf|production]
34
+ */
35
+ public static final String ENVIRONMENT_PROPERTY = "targetenv";
36
+
37
+ /**
38
+ * Default targetenv (target environment) if -Dtargetenv is not specified during test execution outside of
39
+ * maven profiles e.g. running tests in IDE (with right click, run test).
40
+ */
41
+ public static final String ENVIRONMENT_DEFAULT = "qa";
42
+
43
+ /**
44
+ * Default properties file that ServiceTestBase will look for if no file is specified in the constructor.
45
+ */
46
+ public static final String PROPERTIES_FILENAME_DEFAULT = "/config.properties";
47
+ }
48
+
@@ -0,0 +1,52 @@
1
+ package GROUPID.tests.library.base;
2
+
3
+
4
+ import org.slf4j.Logger;
5
+ import org.slf4j.LoggerFactory;
6
+
7
+ import java.io.File;
8
+ import java.io.FileInputStream;
9
+ import java.io.IOException;
10
+ import java.io.InputStream;
11
+ import java.util.Properties;
12
+
13
+ /**
14
+ * Helper class to return list of properties.
15
+ * TODO: This package should be in a separate project and added as a dependency.
16
+ * For the purpose of demo, keeping it in the test library module.
17
+ */
18
+ public class InternalConfigManager {
19
+
20
+ private final static Logger logger = LoggerFactory.getLogger(InternalConfigManager.class);
21
+
22
+ /**
23
+ * Reads all properties from file.
24
+ * @return properties
25
+ */
26
+ public synchronized static Properties getAllConfig(String propertiesFile) {
27
+ try {
28
+ InputStream is = InternalConfigManager.class.getResourceAsStream(propertiesFile);
29
+ if (is == null) {
30
+ File file = new File(propertiesFile);
31
+ if (!file.isDirectory()) {
32
+ if (file.exists()) {
33
+ is = new FileInputStream(file);
34
+ } else {
35
+ throw new IOException("File not exist: " + propertiesFile);
36
+ }
37
+ } else {
38
+ throw new IOException("Unexpected dir path passed (a file path should be passed): " +
39
+ propertiesFile);
40
+ }
41
+ }
42
+ Properties testProperties = new Properties();
43
+ testProperties.load(is);
44
+ return testProperties;
45
+ }
46
+ catch (Exception ex)
47
+ {
48
+ logger.error("Cannot load test configuration");
49
+ return null;
50
+ }
51
+ }
52
+ }
@@ -0,0 +1,103 @@
1
+ package GROUPID.tests.library.base;
2
+
3
+ import com.jayway.restassured.RestAssured;
4
+ import com.jayway.restassured.builder.RequestSpecBuilder;
5
+ import com.jayway.restassured.builder.ResponseSpecBuilder;
6
+ import com.jayway.restassured.response.Response;
7
+ import com.jayway.restassured.specification.RequestSpecification;
8
+ import com.jayway.restassured.specification.ResponseSpecification;
9
+ import org.slf4j.Logger;
10
+ import org.slf4j.LoggerFactory;
11
+
12
+ import java.util.Properties;
13
+
14
+ /**
15
+ * This is a generic business agnostic base class which provides properties management,
16
+ * logging and common RestAssured variables such as requestSpec and responseSpec.
17
+ *
18
+ * TODO: This package should be in a separate project and added as a dependency.
19
+ * For the purpose of demo, keeping it in the test library module.
20
+ * @author eing
21
+ */
22
+ public class ServiceTestBase {
23
+
24
+ public RequestSpecification requestSpec;
25
+ public RequestSpecBuilder requestSpecBuilder = null;
26
+ public Response response;
27
+ public ResponseSpecification responseSpec;
28
+ public ResponseSpecBuilder responseSpecBuilder;
29
+ public String propertiesFile = null;
30
+ public static String configDir = null;
31
+ protected final Logger logger;
32
+ protected Properties internalProperties = null;
33
+
34
+ /**
35
+ * Default settings:
36
+ * Property file: config.properties
37
+ * -Dtargetenv=QA
38
+ */
39
+ public ServiceTestBase() {
40
+ this(BaseConstant.PROPERTIES_FILENAME_DEFAULT);
41
+ }
42
+
43
+ public ServiceTestBase(String propertiesFile) {
44
+ logger = LoggerFactory.getLogger(getClass());
45
+ if (configDir == null) {
46
+ String currentEnvironment = System
47
+ .getProperty(BaseConstant.ENVIRONMENT_PROPERTY);
48
+ if (currentEnvironment == null) {
49
+ currentEnvironment = System
50
+ .getenv(BaseConstant.ENVIRONMENT_PROPERTY);
51
+ if (currentEnvironment == null) {
52
+ currentEnvironment = BaseConstant.ENVIRONMENT_DEFAULT;
53
+ }
54
+ }
55
+ configDir = "/" + currentEnvironment;
56
+ }
57
+ this.propertiesFile = propertiesFile;
58
+ requestSpecBuilder = new RequestSpecBuilder();
59
+ responseSpecBuilder = new ResponseSpecBuilder();
60
+
61
+ if (internalProperties == null) {
62
+ // Only need to read from properties file and set http defaults once for all tests
63
+ readFromPropertiesFile();
64
+ setHttpDefaults();
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Read properties file from both /default and /[targetenv] folder.
70
+ * The latter properties file will overwrite the default properties.
71
+ */
72
+ protected void readFromPropertiesFile() {
73
+ // Read default/<properties file> first
74
+ String defaultPropertiesFile = BaseConstant.DEFAULT_CONFIG_DIRECTORY + propertiesFile;
75
+ internalProperties = InternalConfigManager.getAllConfig(defaultPropertiesFile);
76
+
77
+ // Read <targetenv>/<properties file> next which overwrites default
78
+ String configPath = configDir + propertiesFile;
79
+ internalProperties.putAll(InternalConfigManager.getAllConfig(configPath));
80
+ }
81
+
82
+ /**
83
+ * Initialize from property file if specified
84
+ * 1. http.baseurl : base url e.g. http://www.intuit.com (default is localhost)
85
+ * 2. http.port : http port e.g. 9999 (default is 80)
86
+ */
87
+ protected void setHttpDefaults() {
88
+
89
+ // Set the base URI
90
+ String baseUri = internalProperties.getProperty(BaseConstant.HTTP_PROPERTY_BASEURL);
91
+ if (baseUri != null) {
92
+ RestAssured.baseURI = baseUri;
93
+ requestSpecBuilder.setBaseUri(baseUri);
94
+ }
95
+ // Set default port
96
+ String defaultPort = internalProperties.getProperty(BaseConstant.HTTP_PROPERTY_PORT);
97
+ if (defaultPort != null) {
98
+ int port = Integer.parseInt(defaultPort);
99
+ RestAssured.port = port;
100
+ requestSpecBuilder.setPort(port);
101
+ }
102
+ }
103
+ }
@@ -0,0 +1,12 @@
1
+ package GROUPID.tests.library.sample.base;
2
+
3
+ /**
4
+ * Consolidates all the constants into 1 file for code reuse.
5
+ */
6
+ public class Constant {
7
+
8
+ /**
9
+ * Properties file
10
+ */
11
+ public static final String PROPERTIES_FILE = "/sample.properties";
12
+ }
@@ -0,0 +1,15 @@
1
+ package GROUPID.tests.library.sample.base;
2
+
3
+ import GROUPID.tests.library.base.ServiceTestBase;
4
+
5
+ /**
6
+ * Base class for common business logic that applies to all tests.
7
+ * Please put any utility/helper functions in SampleServiceHelper in test-library module.
8
+ */
9
+ public class SampleTestBase extends ServiceTestBase {
10
+
11
+ public SampleTestBase() {
12
+ super(Constant.PROPERTIES_FILE);
13
+ }
14
+
15
+ }