-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAT-7060 API for delete library with all its versions
- Loading branch information
Showing
10 changed files
with
266 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/gov/cms/madie/cqllibraryservice/exceptions/MeasureServiceException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package gov.cms.madie.cqllibraryservice.exceptions; | ||
|
||
public class MeasureServiceException extends RuntimeException { | ||
public MeasureServiceException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/gov/cms/madie/cqllibraryservice/services/MeasureServiceClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package gov.cms.madie.cqllibraryservice.services; | ||
|
||
import gov.cms.madie.cqllibraryservice.config.EnvironmentConfig; | ||
import gov.cms.madie.cqllibraryservice.dto.LibraryUsage; | ||
import gov.cms.madie.cqllibraryservice.exceptions.MeasureServiceException; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.RequestEntity; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@Service | ||
@AllArgsConstructor | ||
public class MeasureServiceClient { | ||
private EnvironmentConfig environmentConfig; | ||
private RestTemplate restTemplate; | ||
|
||
public List<LibraryUsage> getLibraryUsageInMeasures( | ||
String libraryName, String accessToken, String apiKey) { | ||
try { | ||
URI uri = | ||
URI.create( | ||
environmentConfig.getMeasureServiceBaseUrl() | ||
+ "/measures/library/usage?libraryName=" | ||
+ libraryName); | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.TEXT_PLAIN); | ||
headers.set(HttpHeaders.AUTHORIZATION, accessToken); | ||
headers.set("api-key", apiKey); | ||
ResponseEntity<List<LibraryUsage>> responseEntity = | ||
restTemplate.exchange( | ||
new RequestEntity<>(headers, HttpMethod.GET, uri), | ||
new ParameterizedTypeReference<>() {}); | ||
return responseEntity.getBody(); | ||
} catch (Exception ex) { | ||
String message = "An error occurred while fetching library usage in measures"; | ||
log.error(message, ex); | ||
throw new MeasureServiceException(message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/test/java/gov/cms/madie/cqllibraryservice/services/MeasureServiceClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package gov.cms.madie.cqllibraryservice.services; | ||
|
||
import gov.cms.madie.cqllibraryservice.config.EnvironmentConfig; | ||
import gov.cms.madie.cqllibraryservice.dto.LibraryUsage; | ||
import gov.cms.madie.cqllibraryservice.exceptions.MeasureServiceException; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.RequestEntity; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.client.RestClientException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.List; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class MeasureServiceClientTest { | ||
@Mock private EnvironmentConfig environmentConfig; | ||
@Mock private RestTemplate restTemplate; | ||
|
||
@InjectMocks private MeasureServiceClient measureServiceClient; | ||
|
||
private final String token = "124"; | ||
private final String apiKey = "key"; | ||
private final String libraryName = "Test"; | ||
|
||
@Test | ||
void testGetLibraryUsageInMeasures() { | ||
String owner = "john"; | ||
LibraryUsage libraryUsage = LibraryUsage.builder().name(libraryName).owner(owner).build(); | ||
when(environmentConfig.getMeasureServiceBaseUrl()).thenReturn("url"); | ||
when(restTemplate.exchange(any(RequestEntity.class), any(ParameterizedTypeReference.class))) | ||
.thenReturn(ResponseEntity.ok(List.of(libraryUsage))); | ||
List<LibraryUsage> libraryUsages = | ||
measureServiceClient.getLibraryUsageInMeasures(libraryName, token, apiKey); | ||
assertThat(libraryUsages.size(), is(equalTo(1))); | ||
assertThat(libraryUsages.get(0).getName(), is(equalTo(libraryName))); | ||
assertThat(libraryUsages.get(0).getOwner(), is(equalTo(owner))); | ||
} | ||
|
||
@Test | ||
void testGetLibraryUsageInMeasuresThrowsException() { | ||
String message = "An error occurred while fetching library usage in measures"; | ||
when(environmentConfig.getMeasureServiceBaseUrl()).thenReturn("url"); | ||
doThrow(new RestClientException(message)) | ||
.when(restTemplate) | ||
.exchange(any(RequestEntity.class), any(ParameterizedTypeReference.class)); | ||
Exception ex = | ||
assertThrows( | ||
MeasureServiceException.class, | ||
() -> measureServiceClient.getLibraryUsageInMeasures(libraryName, token, apiKey)); | ||
assertThat(ex.getMessage(), is(equalTo(message))); | ||
} | ||
} |