Skip to content

Commit 66e2fde

Browse files
authored
Merge pull request #46 from BidnessForB/refactor-collections
Adding simple request constructor
2 parents f89383b + cae11cd commit 66e2fde

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,28 @@ Got a [Collection ID](https://support.postman.com/hc/en-us/articles/506378509531
4141
Collection pmcTest = Collection.pmcFactory(new PostmanID("<your collection id>"));
4242
```
4343

44+
NOTE: You must set an environment variable named `POSTMAN_API_KEY` with the value of your Postman API key.
45+
4446
### Create Collections from scratch
4547

4648
You can create a new, empty collection as well
4749

4850
```java
49-
Collection newColl = PostmanFactory("New collection");
51+
Collection newColl = Collection.pmcFactory();
52+
newColl.setName("My new collection");
5053
```
5154

5255
then create and add elements to your new collection
5356

57+
### Add requests
58+
59+
You can add a new request just by providing a URL:
60+
61+
```java
62+
Collection newColl = Collection.pmcFactory();
63+
newColl.addRequest("https://postman-echo.com/get");
64+
```
65+
5466
### Edit collections: add, remove, edit and move Collection elements
5567

5668
JPostman allows you to add new Folders and Requests to your Postman Collections. You can also add variables, Pre-Request and Test Scripts, requests, and responses. In fact,
@@ -112,7 +124,7 @@ JPostman allows you to generate JSON for your collections. You can also write y
112124
Use the `Collection.validate() method to ensure that the JSON emitted by your instance of Collection conforms with the Postman Collection schema:
113125

114126
```java
115-
boolean isValid = myCollection.validate()
127+
boolean isValid = myCollection.validate();
116128
```
117129

118130
JPostman uses the NetworkNT [json-schema-validator](https://github.com/networknt/json-schema-validator) to validate JSON against a JSON schema.

src/main/java/com/postman/collection/Collection.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,31 @@ public Request addRequest(RequestBody reqElement, String name, Response response
156156
return newReq;
157157
}
158158

159+
/**
160+
*
161+
* Create and add a new <code>request</code> as a top level child item of this collection. The request will be named 'New Request'
162+
*
163+
*
164+
* @param url The URL for the request. Cannot be null or non-zero
165+
* @return Item The new Request item
166+
* @throws RecursiveItemAddException If this collection already include this instance in it's array of items.
167+
* @throws DuplicateVariableKeyException If parsing the URL results in the creation of duplicate parameter variables.
168+
* @throws IllegalPropertyAccessException
169+
* @throws InvalidCollectionActionExample If a null or zero-length string URL is passed.
170+
*
171+
*/
172+
public Request addRequest(String url) throws RecursiveItemAddException, IllegalPropertyAccessException, InvalidCollectionActionException, DuplicateVariableKeyException {
173+
174+
if(url == null || url.length() < 1) {
175+
throw new InvalidCollectionActionException("Url must be a non-zero length string");
176+
}
177+
178+
Request newReq = this.addRequest(new RequestBody(enumHTTPRequestMethod.GET, url), "New Request");
179+
180+
181+
return newReq;
182+
183+
}
159184

160185
/**
161186
*

src/test/java/com/postman/collection/AppTest.java

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.postman.collection;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
45
import static org.junit.Assert.assertNotEquals;
56
import static org.junit.Assert.assertNotNull;
67
import static org.junit.Assert.assertNull;
@@ -1155,6 +1156,42 @@ public void TestCollectionRequests() {
11551156
{
11561157
assertTrue("Unexpected duplicate key: " + e.getMessage(), false);
11571158
}
1159+
Request reqItem3 = null;
1160+
try {
1161+
reqItem3 = pmcTest.addRequest("https://postman-echo.com/get");
1162+
}
1163+
catch (Exception e) {
1164+
assertTrue("Exception adding collection", false);
1165+
}
1166+
1167+
assertEquals(reqItem3.getRequestBody().getUrl().getRaw(), "https://postman-echo.com/get");
1168+
1169+
try {
1170+
reqItem3 = pmcTest.addRequest("https://postman-echo.com/get?foo=bar&bat={{boo}}");
1171+
}
1172+
catch(Exception e) {
1173+
assertTrue("Exception adding collection", false);
1174+
}
1175+
1176+
assertEquals(reqItem3.getRequestBody().getUrl().getRaw(), "https://postman-echo.com/get?foo=bar&bat={{boo}}");
1177+
1178+
1179+
try {
1180+
//should trigger a duplicate key exception?
1181+
reqItem3 = pmcTest.addRequest("https://postman-echo.com/:pathvar1/get?foo=bar&bat={{boo}}");
1182+
}
1183+
catch(Exception e) {
1184+
assertTrue("Exception adding collection", false);
1185+
}
1186+
1187+
assertEquals(reqItem3.getRequestBody().getUrl().getRaw(), "https://postman-echo.com/:pathvar1/get?foo=bar&bat={{boo}}");
1188+
1189+
assertEquals(reqItem3.getRequestBody().getUrl().getPathVariables().size(), 1);
1190+
assertNotNull(reqItem3.getRequestBody().getUrl().getPathVariable("pathvar1"));
1191+
1192+
1193+
1194+
11581195

11591196

11601197

@@ -1169,7 +1206,7 @@ public void TestCollectionRequests() {
11691206

11701207

11711208

1172-
assertEquals(2, pmcTest.getItems(enumItemType.REQUEST).size());
1209+
assertEquals(5, pmcTest.getItems(enumItemType.REQUEST).size());
11731210

11741211
try {
11751212
newFolder = pmcTest.addFolder("New Folder");
@@ -1182,7 +1219,7 @@ public void TestCollectionRequests() {
11821219
try {
11831220
pmcTest.moveItem(reqItem1, newFolder);
11841221
assertEquals("New Folder", pmcTest.getRequest("GET echo").getParent().getName());
1185-
assertEquals(2, pmcTest.getItems().size());
1222+
assertEquals(5, pmcTest.getItems().size());
11861223
}
11871224
catch(Exception e)
11881225
{
@@ -1196,9 +1233,25 @@ public void TestCollectionRequests() {
11961233

11971234

11981235
pmcTest.removeItem("POST echo");
1199-
assertEquals(1, pmcTest.getItems(enumItemType.REQUEST).size());
1236+
assertEquals(4, pmcTest.getItems(enumItemType.REQUEST).size());
12001237
assertNotNull(pmcTest.getItem("GET echo"));
12011238

1239+
//Should trigger duplicate variable key exception
1240+
try {
1241+
reqItem3 = pmcTest.addRequest("https://postman-echo.com/:pathvar1/:pathvar1/get?foo=bar&bat={{boo}}");
1242+
}
1243+
catch(DuplicateVariableKeyException e) {
1244+
assertTrue("Duplicate variable key exception expected", true);
1245+
}
1246+
catch(Exception e) {
1247+
assertTrue("Unexpected exception: " + e.getMessage(), false);
1248+
}
1249+
1250+
1251+
validateAndWriteToFile(pmcTest, new Throwable().getStackTrace()[0]);
1252+
1253+
1254+
12021255

12031256
}
12041257

0 commit comments

Comments
 (0)