In rally , how to dynamically generate a search query string in java from a given list of criteria for webservice v2.0

StackOverflow https://stackoverflow.com/questions/21985038

  •  15-10-2022
  •  | 
  •  

Question

I have a rally defect search criteria set in a java hashmap. This hashmap contains key as fieldname of Rally & value as field value in Rally. From this hashmap I want to generate a string of query parameters which will be passed in webservice url.

Please note that this hashmap may contain criteria for custom fields as well.

Was it helpful?

Solution

With Java it is recommended to use Rally Rest Tookit For Java instead of accessing endpoints directly. Here is a code that queries on stories.

public class FindStories {

    public static void main(String[] args) throws Exception {

        String host = "https://rally1.rallydev.com";
        String username = "user@co.com";
        String password = "secret";
        String applicationName = "RESTExampleFindStories";

        RallyRestApi restApi = null;
            try {
            restApi = new RallyRestApi(
                    new URI(host),
                    username,
                    password);
            restApi.setApplicationName(applicationName); 

            QueryRequest storyRequest = new QueryRequest("Requirement");
            storyRequest.setFetch(new Fetch(new String[] {"Name", "FormattedID", "ScheduleState", "State", "PlanEstimate", "TaskRemainingTotal", "CreationDate"}));
            storyRequest.setLimit(1000);
            storyRequest.setScopedDown(false);
            storyRequest.setScopedUp(false);
            storyRequest.setQueryFilter((new QueryFilter("Project.Name", "=", "Demandware")).and(new QueryFilter("Release.Name", "=", "201311 IT Integrated Release")));
            QueryResponse storyQueryResponse = restApi.query(storyRequest);
            System.out.println("Successful: " + storyQueryResponse.wasSuccessful());
            System.out.println("Size: " + storyQueryResponse.getTotalResultCount());
            System.out.println("Results Size: " + storyQueryResponse.getResults().size());
            for (int i=0; i<storyQueryResponse.getResults().size();i++){
                JsonObject storyJsonObject = storyQueryResponse.getResults().get(i).getAsJsonObject();
                System.out.println("Name: " + storyJsonObject.get("Name") + " ScheduleState: " + storyJsonObject.get("ScheduleState") + " State: " + storyJsonObject.get("State") + " PlanEstimate: " + storyJsonObject.get("PlanEstimate") + " TaskRemainingTotal: " + storyJsonObject.get("TaskRemainingTotal"));
            }
        } finally {
            if (restApi != null) {
                restApi.close();
            }
        }
    }

}

Here is an example based on Rally Rest Tookit For Java that creates a defect. I used a hashmap:

public class aRESTcreateDefectHashMap {

    public static void main(String[] args) throws URISyntaxException, IOException {
            String host = "https://rally1.rallydev.com";
            String username = "user@co.com";
            String password = "secret";
            String wsapiVersion = "v2.0";
            String projectRef = "/project/12352608219";
            String workspaceRef = "/workspace/12352608129"; 
            String applicationName = "RestExample_createDefectWithHashMap";
     RallyRestApi restApi = new RallyRestApi(
            new URI(host),
            username,
            password);
     restApi.setWsapiVersion(wsapiVersion);
     restApi.setApplicationName(applicationName);   



     try {
         System.out.println("Creating a defect...");
         HashMap<String, String> defectHash = new HashMap<String, String>();
         defectHash.put("Name", "some defect 12345");
         defectHash.put("Project", projectRef);
         defectHash.put("c_MyKB", "in progress");         //custom dropdown field

         JsonObject newDefect = new JsonObject();

         Iterator it = defectHash.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pairs = (Map.Entry)it.next();
                System.out.println(pairs.getKey() + " = " + pairs.getValue());
                newDefect.addProperty(pairs.getKey().toString() ,pairs.getValue().toString() );
            }
            CreateRequest createRequest = new CreateRequest("defect", newDefect);
            CreateResponse createResponse = restApi.create(createRequest);
            if (createResponse.wasSuccessful()) {

                System.out.println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));          

                //Read defect
                String ref = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
                System.out.println(String.format("\nReading Defect %s...", ref));
                GetRequest getRequest = new GetRequest(ref);           
            } else {
                String[] createErrors;
                createErrors = createResponse.getErrors();
                System.out.println("Error occurred creating a defect: ");
                for (int j=0; j<createErrors.length;j++) {
                    System.out.println(createErrors[j]);
                }
            }



     } finally {
        restApi.close();
        }
    }

When using the toolkit you do not need to deal with the security token and making sure your client maintains the session. The token is necessary for create and update requests in v2.0 of WSAPI. The toolkit does this for you. It also has addProperty method with this syntax, similar to hashmap's syntax:

newDefect.addProperty("Name", "my new defect");

We recommend using the toolkit. If you chose not to, you have to get the security token from this endpoint:

HttpGet httpGet = new HttpGet("https://rally1.rallydev.com/slm/webservice/v2.0/security/authorize"); 

and later append it to the create request:

HttpPost createDefect = new HttpPost("https://rally1.rallydev.com/slm/webservice/v2.0/defect/create?key="+key);

and use your hasphamp where you create the payload input instead of {\"Name\":\"my new defect\"} below.

List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("content-type", "application/json"));
StringEntity input = new StringEntity("{\"Defect\":{\"Name\":\"my new defect\"}}");
input.setContentType("application/json");
createDefect.setEntity(input);
for (NameValuePair h : pairs)
    {
        createDefect.addHeader(h.getName(), h.getValue());
    }
HttpResponse resp = httpClient.execute(createDefect);

We suggest using the toolkit instead of accessing the endpoints directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top