Skip to content

Commit

Permalink
Merge branch 'master' into QPPCT-562
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Harrison committed Dec 21, 2017
2 parents f84a927 + b4d2ce9 commit eb4a2a8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ USE_SYNC_EXECUTOR=

# Sets the last date that CPC+ files should be accepted by the converter.
CPC_END_DATE=

# If set, the CPC+ APIs are disabled.
NO_CPC_PLUS_API=
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import gov.cms.qpp.conversion.api.model.Constants;
import gov.cms.qpp.conversion.api.model.UnprocessedCpcFileData;
import gov.cms.qpp.conversion.api.services.CpcFileService;
import java.io.IOException;
import java.util.List;
import gov.cms.qpp.conversion.util.EnvironmentHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -18,6 +17,9 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
* Controller to handle cpc file data
*/
Expand All @@ -37,9 +39,14 @@ public class CpcFileControllerV1 {
*/
@RequestMapping(method = RequestMethod.GET, value = "/unprocessed-files",
headers = {"Accept=" + Constants.V1_API_ACCEPT})
public ResponseEntity<List> getUnprocessedCpcPlusFiles() throws IOException {
public ResponseEntity<List<UnprocessedCpcFileData>> getUnprocessedCpcPlusFiles() {
API_LOG.info("CPC+ unprocessed files request received");

if (blockCpcPlusApi()) {
API_LOG.info("CPC+ unprocessed files request blocked by feature flag");
return new ResponseEntity<>(null, null, HttpStatus.FORBIDDEN);
}

List<UnprocessedCpcFileData> unprocessedCpcFileDataList = cpcFileService.getUnprocessedCpcPlusFiles();

API_LOG.info("CPC+ unprocessed files request succeeded");
Expand All @@ -63,6 +70,11 @@ public ResponseEntity<InputStreamResource> getFileById(@PathVariable("fileId") S
throws IOException {
API_LOG.info("CPC+ file request received");

if (blockCpcPlusApi()) {
API_LOG.info("CPC+ file request blocked by feature flag");
return new ResponseEntity<>(null, null, HttpStatus.FORBIDDEN);
}

InputStreamResource content = cpcFileService.getFileById(fileId);

API_LOG.info("CPC+ file request succeeded");
Expand All @@ -72,4 +84,13 @@ public ResponseEntity<InputStreamResource> getFileById(@PathVariable("fileId") S

return new ResponseEntity<>(content, httpHeaders, HttpStatus.OK);
}

/**
* Checks whether the the CPC+ APIs should not be allowed to execute.
*
* @return Whether the CPC+ APIs should be blocked.
*/
private boolean blockCpcPlusApi() {
return EnvironmentHelper.isPresent(Constants.NO_CPC_PLUS_API_ENV_VARIABLE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Constants {
public static final String SUBMISSION_API_TOKEN_ENV_VARIABLE = "SUBMISSION_API_TOKEN";
public static final String VALIDATION_URL_ENV_VARIABLE = "VALIDATION_URL";
public static final String USE_SYNC_EXECUTOR = "USE_SYNC_EXECUTOR";
public static final String NO_CPC_PLUS_API_ENV_VARIABLE = "NO_CPC_PLUS_API";
public static final String V1_API_ACCEPT = "application/vnd.qpp.cms.gov.v1+json";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package gov.cms.qpp.conversion.api.controllers.v1;

import gov.cms.qpp.conversion.api.model.Constants;
import gov.cms.qpp.conversion.api.model.Metadata;
import gov.cms.qpp.conversion.api.model.UnprocessedCpcFileData;
import gov.cms.qpp.conversion.api.services.CpcFileService;
import gov.cms.qpp.test.MockitoExtension;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
Expand All @@ -40,11 +44,16 @@ void setUp() {
expectedUnprocessedCpcFileDataList = createMockedUnprocessedDataList();
}

@AfterEach
void turnOffFeatureFlag() {
System.clearProperty(Constants.NO_CPC_PLUS_API_ENV_VARIABLE);
}

@Test
void testGetUnprocessedFileList() throws IOException {
void testGetUnprocessedFileList() {
when(cpcFileService.getUnprocessedCpcPlusFiles()).thenReturn(expectedUnprocessedCpcFileDataList);

ResponseEntity qppResponse = cpcFileControllerV1.getUnprocessedCpcPlusFiles();
ResponseEntity<List<UnprocessedCpcFileData>> qppResponse = cpcFileControllerV1.getUnprocessedCpcPlusFiles();

verify(cpcFileService).getUnprocessedCpcPlusFiles();

Expand All @@ -62,6 +71,26 @@ void testGetFileById() throws IOException {
.isEqualTo("1234");
}

@Test
void testEndpoint1WithFeatureFlagDisabled() {
System.setProperty(Constants.NO_CPC_PLUS_API_ENV_VARIABLE, "trueOrWhatever");

ResponseEntity<List<UnprocessedCpcFileData>> cpcResponse = cpcFileControllerV1.getUnprocessedCpcPlusFiles();

assertThat(cpcResponse.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
assertThat(cpcResponse.getBody()).isNull();
}

@Test
void testEndpoint2WithFeatureFlagDisabled() throws IOException {
System.setProperty(Constants.NO_CPC_PLUS_API_ENV_VARIABLE, "trueOrWhatever");

ResponseEntity<InputStreamResource> cpcResponse = cpcFileControllerV1.getFileById("meep");

assertThat(cpcResponse.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
assertThat(cpcResponse.getBody()).isNull();
}

List<UnprocessedCpcFileData> createMockedUnprocessedDataList() {
Metadata metadata = new Metadata();
metadata.setSubmissionLocator("Test");
Expand Down

0 comments on commit eb4a2a8

Please sign in to comment.