Skip to content

Commit 47c5f60

Browse files
committed
Upsert to postman
1 parent 8ce02eb commit 47c5f60

File tree

3 files changed

+92
-31
lines changed

3 files changed

+92
-31
lines changed

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ public void updateInPostman(PostmanID workspaceID) {
707707
* @param outputFile The file into which to write the JSON
708708
* @throws IOException If there is an error attempting to create or write to the specified path
709709
*/
710-
public PostmanID upsertToPostman(PostmanID workspaceID) throws IOException, InterruptedException {
710+
public PostmanID upsertToPostman(PostmanID workspaceID) throws IOException, InterruptedException, CollectionNotFoundException, InvalidCollectionActionException {
711711
String colData = this.toJson();
712712
colData = colData.substring(colData.indexOf("\"item\":"));
713713
String apiURL = "https://api.getpostman.com/collections";
@@ -723,9 +723,11 @@ public PostmanID upsertToPostman(PostmanID workspaceID) throws IOException, Inte
723723
}
724724

725725
HttpRequest request = null;
726-
if(workspaceID != null && workspaceID.getID().length() > 0) {
726+
if(this.getPostmanID() == null) {
727727
//In this case we are creating
728-
apiURL = apiURL + "?workspace=" + workspaceID.getID();
728+
if(workspaceID != null && workspaceID.getID().length() > 0)
729+
apiURL = apiURL + "?workspace=" + workspaceID.getID();
730+
729731
request = HttpRequest.newBuilder(
730732
URI.create(apiURL))
731733
.header("accept", "application/json")
@@ -734,7 +736,7 @@ public PostmanID upsertToPostman(PostmanID workspaceID) throws IOException, Inte
734736
.POST(HttpRequest.BodyPublishers.ofString(bodyJSON))
735737
.build();
736738
}
737-
else if (workspaceID == null && this.getPostmanID() != null) {
739+
else if (this.getPostmanID() != null) {
738740
apiURL = apiURL + "/" + this.getPostmanID();
739741
request = HttpRequest.newBuilder(
740742
URI.create(apiURL))
@@ -745,29 +747,33 @@ else if (workspaceID == null && this.getPostmanID() != null) {
745747
.build();
746748
}
747749

750+
var response = client.send(request, BodyHandlers.ofString());
751+
748752

749-
750-
GsonBuilder gsonBuilder = new GsonBuilder();
751-
Gson customGson = gsonBuilder.create();
753+
if(response.statusCode() == 200) {
754+
GsonBuilder gsonBuilder = new GsonBuilder();
755+
Gson customGson = gsonBuilder.create();
752756

753757

754758
// use the client to send the request
755-
var response = client.send(request, BodyHandlers.ofString());
759+
756760
Type hashType = new TypeToken<HashMap<String,HashMap<String, String>>>() {}.getType();
757-
HashMap<String, HashMap> respJSON = customGson.fromJson(response.body(), hashType);
758-
/*
759-
if(response.statusCode() == 404) {
761+
HashMap<String, HashMap<String, String>> respJSON = customGson.fromJson(response.body(), hashType);
762+
this.setPostmanID(respJSON.get("collection").get("id").toString());
763+
764+
}
765+
else if(response.statusCode() == 404) {
760766
throw new CollectionNotFoundException("Collection not found or invalid endopint");
761767
}
762-
else if(response.statusCode() != 200)
768+
else
763769
{
764770
throw new InvalidCollectionActionException("An error occurred retrieving the collection" + (response.body() == null ? "[no response info]" : response.body()));
765771
}
766-
*/
767772

768-
this.setPostmanID(respJSON.get("collection").get("id").toString());
773+
return new PostmanID(this.getPostmanID());
774+
775+
769776

770-
return new PostmanID(this.getPostmanID());
771777

772778
}
773779

src/main/resources/com/postman/collection/auth.postman_collection.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"info": {
3-
"_postman_id": "23889826-fde8df9b-b240-49c4-83f5-bbe78db4ab60",
3+
"_postman_id": "23889826-169dff8a-c684-4ccc-b8df-7ad436efda57",
44
"name": "Auth",
55
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
66
"_exporter_id": "23889826"

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

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.ArrayList;
1616
import java.util.Arrays;
1717
import java.io.File;
18+
19+
import org.apache.commons.collections4.functors.FalsePredicate;
1820
import org.junit.Test;
1921

2022
import com.networknt.schema.ValidationMessage;
@@ -25,6 +27,7 @@
2527
import java.nio.file.DirectoryStream;
2628
import java.nio.file.Path;
2729
import java.nio.file.Paths;
30+
import java.security.Timestamp;
2831
import java.nio.file.Files;
2932
import java.io.IOException;
3033
import java.net.MalformedURLException;
@@ -1523,34 +1526,86 @@ public void testParentChain() {
15231526

15241527
@Test
15251528
public void testWriteToPostman() {
1526-
Collection pmcTest = null;
1529+
Collection pmcTest = Collection.pmcFactory();
15271530
Collection pmcTest2 = null;
1528-
1531+
BodyElement body = null;
1532+
RequestBody req = null;
1533+
Response resp = null;
1534+
String colName = new java.sql.Timestamp(System.currentTimeMillis()).toString();
1535+
//Test creating a new collection
15291536
try {
1530-
pmcTest = Collection.pmcFactory(new File(filePath + resourcePath + "/auth.postman_collection.json"));
1531-
pmcTest2 = Collection.pmcFactory(new PostmanID("23889826-598a2a23-0a3b-454f-9309-ebd0004145d9"));
1532-
pmcTest2.setName("Auth renamed again");
1537+
1538+
body = new BodyElement(enumRequestBodyMode.URLENCODED);
1539+
body.setFormdata("x-field-1", "value 1", "This is value 1");
1540+
body.setFormdata("x-field-2", "value 2", "This is value 2");
1541+
req = new RequestBody(enumHTTPRequestMethod.POST, "https://postman-echo.com/post");
1542+
req.setBody(body);
1543+
resp = new Response("NORMAL Urlencoded", req, "OK", 200, "this is the expected response body");
1544+
pmcTest.addRequest(req, "URLEncoded body", resp);
1545+
pmcTest.setName("UPSERT test " + colName);
15331546
}
1534-
catch(Exception e)
1535-
{
1536-
assertTrue("IOException: " + e.getMessage(), false);
1547+
catch(Exception eee) {
1548+
eee.printStackTrace();
1549+
assertTrue("Exception creating test collection: " + eee.getMessage(), false);
1550+
}
1551+
1552+
try {
1553+
assertNull("No postmanID before upsert: ", pmcTest.getPostmanID());
1554+
pmcTest.upsertToPostman(null);
1555+
assertNotNull("Postman ID successfully assigned: " , pmcTest.getPostmanID());
1556+
}
1557+
catch(Exception e) {
1558+
e.printStackTrace();
1559+
assertTrue("Exception upserting new collection to Postman: " + e.getMessage(), false);
15371560
}
15381561

1562+
//Upsert a collection to a workspace
15391563
try {
1540-
//Create a new collection
1541-
pmcTest.upsertToPostman(new PostmanID("d827e22e-3694-408d-b18b-d3eea108c001"));
1542-
//update an existing collection
15431564

1544-
pmcTest2.upsertToPostman(null);
1545-
// pmcTest2.setName("Auth Renamed Again");
1546-
/// pmcTest2.updateInPostman(null);
1565+
body = new BodyElement(enumRequestBodyMode.URLENCODED);
1566+
body.setFormdata("x-field-1", "value 1", "This is value 1");
1567+
body.setFormdata("x-field-2", "value 2", "This is value 2");
1568+
req = new RequestBody(enumHTTPRequestMethod.POST, "https://postman-echo.com/post");
1569+
req.setBody(body);
1570+
resp = new Response("NORMAL Urlencoded", req, "OK", 200, "this is the expected response body");
1571+
pmcTest.addRequest(req, "URLEncoded body", resp);
1572+
pmcTest.setName("UPSERT test to Workspace" + colName);
1573+
pmcTest.upsertToPostman(new PostmanID("23889826-169dff8a-c684-4ccc-b8df-7ad436efda57"));
1574+
}
1575+
catch(Exception eee) {
1576+
eee.printStackTrace();
1577+
assertTrue("Exception creating test collection: " + eee.getMessage(), false);
1578+
}
1579+
1580+
//Fetch a known collection from Postman, change the name, then Upsert it
1581+
try {
1582+
1583+
pmcTest = Collection.pmcFactory(new PostmanID("23889826-169dff8a-c684-4ccc-b8df-7ad436efda57"));
1584+
String origName = pmcTest.getName();
1585+
pmcTest.setName(origName + "- UPSERTED");
1586+
pmcTest.upsertToPostman(null);
1587+
pmcTest = Collection.pmcFactory(new PostmanID("23889826-169dff8a-c684-4ccc-b8df-7ad436efda57"));
1588+
assertTrue(pmcTest.getName().equals(origName + "- UPSERTED"));
1589+
}
1590+
catch(Exception ee) {
1591+
ee.printStackTrace();
1592+
assertTrue("Exception writing known collection: " + ee.getMessage(), false);
1593+
}
1594+
1595+
1596+
try {
1597+
//read in a collection with an ID
1598+
pmcTest = Collection.pmcFactory(new File(filePath + resourcePath + "/auth.postman_collection.json"));
1599+
pmcTest.setName("Auth renamed again " + new java.sql.Timestamp(System.currentTimeMillis()));
1600+
pmcTest.upsertToPostman(null);
15471601
}
15481602
catch(Exception e)
15491603
{
1550-
assertTrue("Exception: " + e.getMessage(), false);
1604+
assertTrue("IOException: " + e.getMessage(), false);
15511605
}
15521606

15531607

1608+
15541609
}
15551610

15561611
}

0 commit comments

Comments
 (0)