postman-code-generators icon indicating copy to clipboard operation
postman-code-generators copied to clipboard

Feature/java native codegen

Open virenderm01 opened this issue 4 years ago • 10 comments

Pure Java code generator for issue #155 Issue

The code generator creates snippet in java 8, no other dependencies are required to run the code.

Changes: Added new code gen with name java-native. All the changes required to generate the code is added under the folder codegen/java-native The code structure and style is similar to java-okhttp codegen.

Sample Code Snippet

import java.io.*;
import javax.net.ssl.HttpsURLConnection;
import java.net.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.function.BiConsumer;
public class main {
   public static void main(String []args) throws IOException{
      URL obj = new URL("https://postman-echo.com/get");
      URLConnection connection = obj.openConnection();
      HttpURLConnection con = (HttpURLConnection) connection;
      con.setRequestMethod("GET");
      Map<String, String> headers = new HashMap<>();
      BiConsumer<String,String> headerMap = (key,value) -> {
         if(headers.get(key)!=null){
            String val = headers.get(key);
            val = val + ", "+ value;
            headers.put(key, val);
         }else{
            headers.put(key,value) ;
         }
      };
      headerMap.accept("key", "value1");
      headerMap.accept("key", "value2");
      for (Map.Entry<String, String> entry : headers.entrySet()) {
         con.setRequestProperty(entry.getKey(), entry.getValue());
      }con.setRequestProperty("Accept","application/json");
      con.setDoOutput(false);
      BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
      String temp = null;
      StringBuilder response = new StringBuilder();
      while ((temp = in.readLine()) != null) {
         response.append(temp).append(" ");
      }
      System.out.println(response.toString());
      in.close();
   
}
}

Test Configuration Unit Tests and newman test are all passing, the log for the same can be found in the below link. Overall Code coverage is above 85% for tests. Java native code logs are from line 5246 Travis Build Logs

virenderm01 avatar May 30 '20 15:05 virenderm01

Hi @shreys7 , Can you review it once, I see it's been more than a month but no action was taken on this. I have added all the necessary test cases, please let me know if anything else is required.

virenderm01 avatar Jul 02 '20 18:07 virenderm01

By the way,

if(connection instanceof HttpsURLConnection){
    con = (HttpsURLConnection) connection;
}else{
    con = (HttpURLConnection) connection;
}

cab be simplified to con = (HttpURLConnection) connection; as HttpsURLConnection extends HttpsURLConnection.

danthe1st avatar Jul 25 '20 21:07 danthe1st

@danthe1st you meant

HttpURLConnection con = (HttpsURLConnection) connection;

I have created another branch and have made the changes. Let me know if I had misinterpreted your comment.

virenderm01 avatar Jul 28 '20 12:07 virenderm01

You can just cast to HttpURLConnection, even if it is a HttpsURLConnection. You therefore don't need the if.

If you cast a HttpUrlConnection to a HttpsURLConnection, it will not work.

danthe1st avatar Jul 28 '20 12:07 danthe1st

Hi @umeshp7 @shreys7 , Can you guys please review this, let me know if there are any changes required or if there are any issues that might have been missed out while creating the pull request.

Regards, Virender Moudgil

virenderm01 avatar Aug 30 '20 16:08 virenderm01

i'd really like to see this - can we all have a status uptade on this?

doej1367 avatar Mar 09 '22 12:03 doej1367

Thanks for the contribution @virenderm01. Could you please pull the latest changes from upstream so we can start the review process?

dhwaneetbhatt avatar Mar 28 '23 10:03 dhwaneetbhatt

@dhwaneetbhatt updated the branch, the confilicts are now resolved.

virenderm01 avatar Jul 19 '23 06:07 virenderm01

@dhwaneetbhatt any update on merging this ?

virenderm01 avatar Apr 01 '24 10:04 virenderm01

@virenderm01 I'm no longer a maintainer of this repository. Please reach out to Postman Support. They should be able to help you.

dhwaneetbhatt avatar Apr 01 '24 10:04 dhwaneetbhatt