-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Okapi login and authorized requests (#104)
* okapi login and authorized requests * update javadoc comments * add simple token utility test * initial catalog service test * remove unused mocks * throw HttpServerErrorException for proper response * some more naive okapi request tests
- Loading branch information
Showing
12 changed files
with
312 additions
and
40 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
service/src/main/java/edu/tamu/catalog/properties/Credentials.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,24 @@ | ||
package edu.tamu.catalog.properties; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class Credentials { | ||
|
||
private String username; | ||
|
||
private String password; | ||
|
||
@JsonCreator | ||
public Credentials( | ||
@JsonProperty(value = "username", required = true) String username, | ||
@JsonProperty(value = "password", required = true) String password | ||
) { | ||
setUsername(username); | ||
setPassword(password); | ||
} | ||
|
||
} |
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
23 changes: 23 additions & 0 deletions
23
service/src/main/java/edu/tamu/catalog/utility/TokenUtility.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,23 @@ | ||
package edu.tamu.catalog.utility; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class TokenUtility { | ||
|
||
private static final Map<String, String> tokens = new HashMap<>(); | ||
|
||
private TokenUtility() { | ||
|
||
} | ||
|
||
public static synchronized void setToken(String catalog, String token) { | ||
tokens.put(catalog, token); | ||
} | ||
|
||
public static synchronized Optional<String> getToken(String catalog) { | ||
return Optional.ofNullable(tokens.get(catalog)); | ||
} | ||
|
||
} |
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
110 changes: 110 additions & 0 deletions
110
service/src/test/java/edu/tamu/catalog/service/FolioCatalogServiceTest.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,110 @@ | ||
package edu.tamu.catalog.service; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Matchers.eq; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import edu.tamu.catalog.properties.Credentials; | ||
import edu.tamu.catalog.properties.FolioProperties; | ||
|
||
@RunWith(SpringRunner.class) | ||
public class FolioCatalogServiceTest { | ||
|
||
private ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
private Credentials credentials = new Credentials("diku_admin", "admin"); | ||
|
||
private FolioProperties properties = new FolioProperties( | ||
"folio", | ||
"folio", | ||
"http://localhost:9130", | ||
"http://localhost:8080", | ||
"diku", | ||
credentials, | ||
"mock_api_key", | ||
"localhost" | ||
); | ||
|
||
@InjectMocks | ||
private FolioCatalogService folioCatalogService = new FolioCatalogService(properties); | ||
|
||
@Mock | ||
private RestTemplate restTemplate; | ||
|
||
@Before | ||
public void setup() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
headers.set("X-Okapi-Token", "token"); | ||
when(restTemplate.postForObject(eq("http://localhost:9130/authn/login"), any(HttpEntity.class), eq(ResponseEntity.class))) | ||
.thenReturn(new ResponseEntity<String>("{ \"username\": \"diku_admin\", \"password\": \"admin\" }", headers, HttpStatus.CREATED)); | ||
} | ||
|
||
@Test | ||
public void testOkapiHeadRequest() { | ||
testOkapiRequest("http://localhost:9130/locations", HttpMethod.HEAD, HttpStatus.OK); | ||
} | ||
|
||
@Test | ||
public void testOkapiGetRequest() { | ||
testOkapiRequest("http://localhost:9130/locations", HttpMethod.GET, HttpStatus.OK); | ||
} | ||
|
||
@Test | ||
public void testOkapiPostRequest() { | ||
JsonNode requestBody = objectMapper.createObjectNode(); | ||
testOkapiRequest("http://localhost:9130/locations", HttpMethod.POST, requestBody, HttpStatus.CREATED); | ||
} | ||
|
||
@Test | ||
public void testOkapiPutRequest() { | ||
JsonNode requestBody = objectMapper.createObjectNode(); | ||
testOkapiRequest("http://localhost:9130/locations/uuid", HttpMethod.PUT, requestBody, HttpStatus.OK); | ||
} | ||
|
||
@Test | ||
public void testOkapiDeleteRequest() { | ||
testOkapiRequest("http://localhost:9130/locations/uuid", HttpMethod.DELETE, HttpStatus.OK); | ||
} | ||
|
||
private void testOkapiRequest(String url, HttpMethod method, HttpStatus status) { | ||
JsonNode responseBody = objectMapper.createObjectNode(); | ||
mockExchange(url, method, responseBody, status); | ||
ResponseEntity<JsonNode> response = folioCatalogService.okapiRequest(url, method, JsonNode.class); | ||
assertEquals(status, response.getStatusCode()); | ||
} | ||
|
||
private void testOkapiRequest(String url, HttpMethod method, JsonNode requestBody, HttpStatus status) { | ||
JsonNode responseBody = objectMapper.createObjectNode(); | ||
mockExchange(url, method, responseBody, status); | ||
ResponseEntity<JsonNode> response = folioCatalogService.okapiRequest(url, method, requestBody, JsonNode.class); | ||
assertEquals(status, response.getStatusCode()); | ||
} | ||
|
||
private void mockExchange(String url, HttpMethod method, JsonNode responseBody, HttpStatus status) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
headers.set("X-Okapi-Tenant", "diku"); | ||
when(restTemplate.exchange(eq(url), eq(method), any(HttpEntity.class), eq(JsonNode.class))) | ||
.thenReturn(new ResponseEntity<JsonNode>(responseBody, headers, status)); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
.../edu/tamu/catalog/util/Marc21XmlTest.java → ...u/tamu/catalog/utility/Marc21XmlTest.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
Oops, something went wrong.