Skip to content

Commit

Permalink
Merge pull request #13 from jembi/get-content-fix
Browse files Browse the repository at this point in the history
ContentHandlerService API alterations
  • Loading branch information
rcrichton committed Aug 19, 2014
2 parents 23e7b2c + aa0e48e commit de801d9
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 62 deletions.
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>shr-contenthandler</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
</parent>

<artifactId>shr-contenthandler-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ public interface ContentHandlerService extends OpenmrsService {

/**
* Returns a content handler for a specified content type.
* Will return a default handler for unknown types.
* Will return null for unknown types.
*
* @return An appropriate content handler for a specified content type
* @should Get an appropriate content handler for a specified content type
* @should Return a clone of the requested handler using the handler's cloneHandler method
* @should Return the default handler (UnstructuredDataHandler) for an unknown content type
* @should Never return null
* @should Return null for an unknown content type
*/
ContentHandler getContentHandler(String contentType);

Expand Down Expand Up @@ -76,11 +75,10 @@ public interface ContentHandlerService extends OpenmrsService {
* <p>
* @see #getContentHandler(String)
*
* @return An appropriate content handler for a specified content type
* @should Get an appropriate content handler for a specified content type
* @return An appropriate content handler for a specified format code and type code
* @should Get an appropriate content handler for a specified format code and type code
* @should Return a clone of the requested handler using the handler's cloneHandler method
* @should Return the default handler (UnstructuredDataHandler) for an unknown content type
* @should Never return null
* @should Return null for an unknown typeCode and formatCode
*/
ContentHandler getContentHandler(CodedValue typeCode, CodedValue formatCode);

Expand Down Expand Up @@ -112,18 +110,19 @@ public interface ContentHandlerService extends OpenmrsService {


/**
* Get an instance of the default handler for a specific content type.
* Get an instance of the default unstructured data handler for a specific content type.
*
* @return The default handler
* @return The default unstructured data handler
* @should Return the default handler (UnstructuredDataHandler)
*/
ContentHandler getDefaultHandler(String contentType);
ContentHandler getDefaultUnstructuredHandler(String contentType);

/**
* Get an instance of the default handler for a specific type and format code.
* Get an instance of the default unstructured data handler for a specific type and format code.
*
* @return The default handler
* @return The default unstructured data handler
* @should Return the default handler (UnstructuredDataHandler)
*/
ContentHandler getDefaultHandler(CodedValue typeCode, CodedValue formatCode);
ContentHandler getDefaultUnstructuredHandler(CodedValue typeCode, CodedValue formatCode);

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ public class ContentHandlerServiceImpl extends BaseOpenmrsService implements Con
protected final Map<String, ContentHandler> contentTypeHandlers = new HashMap<String, ContentHandler>();
protected final Map<TypeFormatCode, ContentHandler> typeFormatCodeHandlers = new HashMap<TypeFormatCode, ContentHandler>();


@Override
public ContentHandler getContentHandler(String contentType) {
if (contentType==null || contentType.isEmpty() || !contentTypeHandlers.containsKey(contentType)) {
return getDefaultHandler(contentType);
return null;
}

return contentTypeHandlers.get(contentType).cloneHandler();
Expand Down Expand Up @@ -82,7 +81,7 @@ public ContentHandler getContentHandler(CodedValue typeCode, CodedValue formatCo
if (typeCode==null || typeCode.getCode().isEmpty() || typeCode.getCodingScheme().isEmpty() ||
formatCode==null || formatCode.getCode().isEmpty() || formatCode.getCodingScheme().isEmpty() ||
!typeFormatCodeHandlers.containsKey(new TypeFormatCode(typeCode, formatCode))) {
return getDefaultHandler(typeCode, formatCode);
return null;
}

return typeFormatCodeHandlers.get(new TypeFormatCode(typeCode, formatCode)).cloneHandler();
Expand Down Expand Up @@ -167,12 +166,12 @@ public boolean equals(Object obj) {


@Override
public ContentHandler getDefaultHandler(String contentType) {
public ContentHandler getDefaultUnstructuredHandler(String contentType) {
return new UnstructuredDataHandler(contentType);
}

@Override
public ContentHandler getDefaultHandler(CodedValue typeCode, CodedValue formatCode) {
public ContentHandler getDefaultUnstructuredHandler(CodedValue typeCode, CodedValue formatCode) {
return new UnstructuredDataHandler(typeCode, formatCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,11 @@ public void getContentHandler_shouldGetAnAppropriateContentHandlerForASpecifiedC
* @verifies Return the default handler (UnstructuredDataHandler) for an unknown content type
*/
@Test
public void getContentHandler_shouldReturnTheDefaultHandlerUnstructuredDataHandlerForAnUnknownContentType()
public void getContentHandler_shouldReturnNullForAnUnknownContentType()
throws Exception {
ContentHandlerService chs = getService();

assertTrue(chs.getContentHandler("application/nothing-here") instanceof UnstructuredDataHandler);
}

/**
* @see ContentHandlerService#getContentHandler(String)
* @verifies Never return null
*/
@Test
public void getContentHandler_shouldNeverReturnNull() throws Exception {
ContentHandlerService chs = getService();

assertNotNull(chs.getContentHandler("text/plain"));

ContentHandler mockHandler = mock(ContentHandler.class);
chs.registerContentHandler("text/plain", mockHandler);
when(mockHandler.cloneHandler()).thenReturn(mockHandler);
assertNotNull(chs.getContentHandler("text/plain"));

assertNotNull(chs.getContentHandler("application/xml"));
assertNotNull(chs.getContentHandler("application/nothing-here"));
assertNull(chs.getContentHandler("application/nothing-here"));
}

/**
Expand Down Expand Up @@ -293,27 +274,11 @@ public void getContentHandler_tfCode_shouldGetAnAppropriateContentHandlerForASpe
* @verifies Return the default handler (UnstructuredDataHandler) for an unknown type and format code
*/
@Test
public void getContentHandler_tfCode_shouldReturnTheDefaultHandlerUnstructuredDataHandlerForAnUnknownContentType()
public void getContentHandler_tfCode_shouldReturnNullForAnUnknownContentType()
throws Exception {
ContentHandlerService chs = getService();

assertTrue(chs.getContentHandler(TEST_TYPE_CODE, TEST_FORMAT_CODE) instanceof UnstructuredDataHandler);
}

/**
* @see ContentHandlerService#getContentHandler(String,String)
* @verifies Never return null
*/
@Test
public void getContentHandler_tfCode_shouldNeverReturnNull() throws Exception {
ContentHandlerService chs = getService();

assertNotNull(chs.getContentHandler(TEST_TYPE_CODE, TEST_FORMAT_CODE));

ContentHandler mockHandler = mock(ContentHandler.class);
chs.registerContentHandler(TEST_TYPE_CODE, TEST_FORMAT_CODE, mockHandler);
when(mockHandler.cloneHandler()).thenReturn(mockHandler);
assertNotNull(chs.getContentHandler(TEST_TYPE_CODE, TEST_FORMAT_CODE));
assertNull(chs.getContentHandler(TEST_TYPE_CODE, TEST_FORMAT_CODE));
}

/**
Expand Down Expand Up @@ -448,7 +413,7 @@ public void registerContentHandler_tfCode_shouldThrowANullPointerExceptionIfProt
public void getDefaultHandler_contentType_shouldReturnTheDefaultHandlerUnstructuredDataHandler()
throws Exception {
ContentHandlerService chs = getService();
assertTrue(chs.getDefaultHandler("text/xml") instanceof UnstructuredDataHandler);
assertTrue(chs.getDefaultUnstructuredHandler("text/xml") instanceof UnstructuredDataHandler);
}

/**
Expand All @@ -459,6 +424,6 @@ public void getDefaultHandler_contentType_shouldReturnTheDefaultHandlerUnstructu
public void getDefaultHandler_tfCodes_shouldReturnTheDefaultHandlerUnstructuredDataHandler()
throws Exception {
ContentHandlerService chs = getService();
assertTrue(chs.getDefaultHandler(TEST_TYPE_CODE, TEST_FORMAT_CODE) instanceof UnstructuredDataHandler);
assertTrue(chs.getDefaultUnstructuredHandler(TEST_TYPE_CODE, TEST_FORMAT_CODE) instanceof UnstructuredDataHandler);
}
}
2 changes: 1 addition & 1 deletion omod/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>shr-contenthandler</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
</parent>

<artifactId>shr-contenthandler-omod</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.openmrs.module</groupId>
<artifactId>shr-contenthandler</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
<packaging>pom</packaging>
<name>SHR Content Handler Module</name>
<description>Provides interfaces and services for dynamically linking content processors with particular content types.</description>
Expand Down

0 comments on commit de801d9

Please sign in to comment.