Skip to content

Commit

Permalink
Merge pull request #230 from teleivo/RESTWS-610
Browse files Browse the repository at this point in the history
RESTWS-610 Test helper RestServiceImpl.addSupportedSearchHandler() sh…
  • Loading branch information
rkorytkowski committed Sep 19, 2016
2 parents fb091ea + 6f9965f commit e3e723f
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 84 deletions.
Expand Up @@ -19,6 +19,8 @@

import org.openmrs.ConceptMap;
import org.openmrs.Patient;
import org.openmrs.module.webservices.rest.web.resource.api.SearchHandler;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingSubclassHandler;

/**
* It is provided as a workaround for missing API methods to fetch {@link ConceptMap}, etc.
Expand All @@ -33,6 +35,10 @@ public interface RestHelperService {

List<Patient> getPatients(Collection<Integer> patientIds);

List<SearchHandler> getRegisteredSearchHandlers();

List<DelegatingSubclassHandler> getRegisteredRegisteredSubclassHandlers();

public static class Field {

private final String name;
Expand Down
Expand Up @@ -25,10 +25,15 @@
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.module.webservices.rest.web.api.RestHelperService;
import org.openmrs.module.webservices.rest.web.resource.api.SearchHandler;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingSubclassHandler;
import org.springframework.transaction.annotation.Transactional;

import static org.openmrs.api.context.Context.getRegisteredComponents;

/**
* REST helper service, which must not be used outside of the REST module.
*/
Expand Down Expand Up @@ -123,4 +128,22 @@ public List<Patient> getPatients(Collection<Integer> patientIds) {

return ret;
}

/**
* @see RestHelperService#getRegisteredSearchHandlers()
*/
@Override
public List<SearchHandler> getRegisteredSearchHandlers() {
final List<SearchHandler> result = Context.getRegisteredComponents(SearchHandler.class);
return result != null ? result : new ArrayList<SearchHandler>();
}

/**
* @see RestHelperService#getRegisteredRegisteredSubclassHandlers()
*/
@Override
public List<DelegatingSubclassHandler> getRegisteredRegisteredSubclassHandlers() {
final List<DelegatingSubclassHandler> result = getRegisteredComponents(DelegatingSubclassHandler.class);
return result != null ? result : new ArrayList<DelegatingSubclassHandler>();
}
}
Expand Up @@ -26,11 +26,11 @@
import org.apache.commons.lang.StringUtils;
import org.hibernate.proxy.HibernateProxy;
import org.openmrs.api.APIException;
import org.openmrs.api.context.Context;
import org.openmrs.module.ModuleUtil;
import org.openmrs.module.webservices.rest.web.OpenmrsClassScanner;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.SubResource;
import org.openmrs.module.webservices.rest.web.api.RestHelperService;
import org.openmrs.module.webservices.rest.web.api.RestService;
import org.openmrs.module.webservices.rest.web.representation.CustomRepresentation;
import org.openmrs.module.webservices.rest.web.representation.NamedRepresentation;
Expand Down Expand Up @@ -61,6 +61,16 @@ public class RestServiceImpl implements RestService {

private volatile List<SearchHandler> allSearchHandlers;

private RestHelperService restHelperService;

public RestHelperService getRestHelperService() {
return restHelperService;
}

public void setRestHelperService(RestHelperService restHelperService) {
this.restHelperService = restHelperService;
}

public RestServiceImpl() {
}

Expand Down Expand Up @@ -170,22 +180,6 @@ public boolean equals(Object obj) {

}

/**
* It should be used in TESTS ONLY.
*
* @param searchHandler
*/
void addSupportedSearchHandler(SearchHandler searchHandler) {
if (searchHandlersByIds == null) {
searchHandlersByIds = new HashMap<RestServiceImpl.SearchHandlerIdKey, SearchHandler>();
}
if (searchHandlersByParameter == null) {
searchHandlersByParameter = new HashMap<SearchHandlerParameterKey, Set<SearchHandler>>();
}

addSupportedSearchHandler(searchHandlersByIds, searchHandlersByParameter, searchHandler);
}

private void initializeResources() {
if (resourceDefinitionsByNames != null) {
return;
Expand Down Expand Up @@ -302,7 +296,7 @@ private void initializeSearchHandlers() {
Map<SearchHandlerParameterKey, Set<SearchHandler>> tempSearchHandlersByParameters = new HashMap<SearchHandlerParameterKey, Set<SearchHandler>>();
Map<String, Set<SearchHandler>> tempSearchHandlersByResource = new HashMap<String, Set<SearchHandler>>();

List<SearchHandler> allSearchHandlers = Context.getRegisteredComponents(SearchHandler.class);
List<SearchHandler> allSearchHandlers = restHelperService.getRegisteredSearchHandlers();
for (SearchHandler searchHandler : allSearchHandlers) {
addSearchHandler(tempSearchHandlersByIds, tempSearchHandlersByParameters, tempSearchHandlersByResource,
searchHandler);
Expand Down Expand Up @@ -553,11 +547,8 @@ public List<DelegatingResourceHandler<?>> getResourceHandlers() throws APIExcept
}
}

@SuppressWarnings("rawtypes")
List<DelegatingSubclassHandler> subclassHandlers = Context.getRegisteredComponents(DelegatingSubclassHandler.class);

for (@SuppressWarnings("rawtypes")
DelegatingSubclassHandler subclassHandler : subclassHandlers) {
List<DelegatingSubclassHandler> subclassHandlers = restHelperService.getRegisteredRegisteredSubclassHandlers();
for (DelegatingSubclassHandler subclassHandler : subclassHandlers) {
resourceHandlers.add(subclassHandler);
}

Expand Down
Expand Up @@ -17,8 +17,9 @@
</property>
<property name="target">
<bean
class="org.openmrs.module.webservices.rest.web.api.impl.RestServiceImpl"
autowire="byType" />
class="org.openmrs.module.webservices.rest.web.api.impl.RestServiceImpl">
<property name="restHelperService" ref="restHelperService"></property>
</bean>
</property>
<property name="preInterceptors">
<ref bean="serviceInterceptors" />
Expand Down

0 comments on commit e3e723f

Please sign in to comment.