Skip to content

Commit

Permalink
Merge pull request #1090 from opensrp/1083-fetch-actions-via-post-v2.2
Browse files Browse the repository at this point in the history
Allow /actions Fetch via POST for v2.2
  • Loading branch information
qiarie authored May 6, 2022
2 parents cd437fa + 942a599 commit d335ce2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 80 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<artifactId>opensrp-server-web</artifactId>
<packaging>war</packaging>
<version>2.9.5-SNAPSHOT</version>
<version>2.9.6-SNAPSHOT</version>
<name>opensrp-server-web</name>
<description>OpenSRP Server Web Application</description>
<url>https://github.com/OpenSRP/opensrp-server-web</url>
Expand Down
21 changes: 8 additions & 13 deletions src/main/java/org/opensrp/web/config/security/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@
*/
package org.opensrp.web.config.security;

import static org.springframework.http.HttpMethod.DELETE;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.OPTIONS;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.HttpMethod.PUT;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;
import org.keycloak.adapters.KeycloakDeployment;
import org.keycloak.adapters.KeycloakDeploymentBuilder;
Expand Down Expand Up @@ -42,6 +30,13 @@
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import static org.springframework.http.HttpMethod.*;

/**
* @author Samuel Githengi created on 04/20/20
*/
Expand Down Expand Up @@ -115,7 +110,7 @@ protected void configure(HttpSecurity http) throws Exception {
.anyRequest().authenticated()
.and()
.csrf()
.ignoringAntMatchers("/rest/**","/multimedia/**")
.ignoringAntMatchers("/rest/**","/multimedia/**","/actions/**")
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("logout.do", "GET"));
Expand Down
45 changes: 31 additions & 14 deletions src/main/java/org/opensrp/web/controller/ActionController.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
package org.opensrp.web.controller;

import static org.opensrp.common.AllConstants.Event.PROVIDER_ID;
import static org.opensrp.web.rest.RestUtils.getIntegerFilter;
import static org.opensrp.web.rest.RestUtils.getStringFilter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import com.google.gson.Gson;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.opensrp.common.AllConstants.BaseEntity;
import org.smartregister.domain.Client;
import org.opensrp.dto.Action;
import org.opensrp.repository.ClientsRepository;
import org.opensrp.scheduler.Alert;
import org.opensrp.scheduler.repository.AlertsRepository;
import org.opensrp.scheduler.service.ActionService;
import org.smartregister.domain.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.opensrp.common.AllConstants.Event.PROVIDER_ID;
import static org.opensrp.web.rest.RestUtils.getIntegerFilter;
import static org.opensrp.web.rest.RestUtils.getStringFilter;

@Controller
public class ActionController {
Expand All @@ -57,6 +58,22 @@ public List<Action> getNewActionForANM(@RequestParam("anmIdentifier") String anm
return actions.stream().map(action -> ActionConvertor.from(action)).collect(Collectors.toList());
}

@RequestMapping(method = RequestMethod.POST, value = "/actions", produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Action> getNewActionForANMByPost(@RequestBody String requestBody) {
try {
JSONObject data = new JSONObject(requestBody);
String anmIdentifier = data.optString("anmIdentifier");
long timeStamp = Long.parseLong(data.optString("timeStamp"));

List<org.opensrp.scheduler.Action> actions = actionService.getNewAlertsForANM(anmIdentifier, timeStamp);
return actions.stream().map(action -> ActionConvertor.from(action)).collect(Collectors.toList());
}
catch (JSONException e) {
logger.error(String.format("Invalid request body {0}", e));
return new ArrayList<>();
}
}

@RequestMapping(method = RequestMethod.GET, value = "/useractions", produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Action> getNewActionForClient(@RequestParam("baseEntityId") String baseEntityId,
@RequestParam("timeStamp") Long timeStamp) {
Expand Down
73 changes: 21 additions & 52 deletions src/test/java/org/opensrp/web/rest/EventResourceTest.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,9 @@
package org.opensrp.web.rest;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atMostOnce;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.nullable;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.opensrp.common.AllConstants.BaseEntity.BASE_ENTITY_ID;
import static org.opensrp.common.AllConstants.BaseEntity.SERVER_VERSIOIN;
import static org.opensrp.util.constants.EventConstants.CASE_NUMBER;
import static org.opensrp.util.constants.EventConstants.EVENT_TYPE_CASE_DETAILS;
import static org.opensrp.util.constants.EventConstants.FLAG;
import static org.opensrp.web.Constants.DEFAULT_GET_ALL_IDS_LIMIT;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.tuple.Pair;
import org.joda.time.DateTime;
import org.json.JSONArray;
Expand All @@ -45,36 +12,38 @@
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;

import static org.opensrp.common.AllConstants.Event.EVENT_TYPE;
import static org.opensrp.common.AllConstants.Event.PROVIDER_ID;
import static org.opensrp.common.AllConstants.Event.LOCATION_ID;
import static org.opensrp.common.AllConstants.Event.TEAM;
import static org.opensrp.common.AllConstants.Event.TEAM_ID;

import org.opensrp.domain.Multimedia;
import org.opensrp.dto.ExportEventDataSummary;
import org.opensrp.dto.ExportFlagProblemEventImageMetadata;
import org.opensrp.dto.ExportImagesSummary;
import org.opensrp.search.EventSearchBean;
import org.opensrp.service.ClientService;
import org.opensrp.service.EventService;
import org.opensrp.service.MultimediaService;
import org.opensrp.service.PlanProcessingStatusService;
import org.opensrp.util.constants.PlanProcessingStatusConstants;
import org.opensrp.web.bean.EventSyncBean;
import org.opensrp.web.bean.Identifier;
import org.smartregister.domain.Client;
import org.smartregister.domain.Event;
import org.opensrp.search.EventSearchBean;
import org.opensrp.service.ClientService;
import org.opensrp.service.EventService;
import org.smartregister.utils.DateTimeTypeConverter;
import org.opensrp.web.bean.EventSyncBean;
import org.opensrp.web.bean.Identifier;
import org.springframework.http.ResponseEntity;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.nullable;
import static org.mockito.Mockito.*;
import static org.opensrp.common.AllConstants.BaseEntity.BASE_ENTITY_ID;
import static org.opensrp.common.AllConstants.BaseEntity.SERVER_VERSIOIN;
import static org.opensrp.common.AllConstants.Event.*;
import static org.opensrp.web.Constants.DEFAULT_GET_ALL_IDS_LIMIT;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class EventResourceTest extends BaseSecureResourceTest<Event> {

Expand Down

0 comments on commit d335ce2

Please sign in to comment.