Skip to content

Commit

Permalink
114022: Removed Mocks, fixed ITs
Browse files Browse the repository at this point in the history
  • Loading branch information
Wout-atmire committed Apr 12, 2024
1 parent cf1e9da commit 432aea7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.dspace.app.rest;


import static org.apache.commons.lang.StringUtils.defaultIfEmpty;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -26,16 +29,17 @@ public class AtmireVersionsController {
protected ConfigurationService configurationService
= DSpaceServicesFactory.getInstance().getConfigurationService();

public static final String CONFIG_DIR = "atmire-versions.directory";
public static final String ATMIRE_VERSIONS_FILE = "atmire-versions.json";
public static final int BUFFER_SIZE = 4096 * 10;
private static final String mimetype = "applicaton/json";
private static final String mimetype = MediaType.APPLICATION_JSON;

@PreAuthorize("hasAuthority('ADMIN')")
@RequestMapping(value = "/atmire-versions", method = RequestMethod.GET)
public ResponseEntity<String> atmireVersioning(HttpServletRequest request, HttpServletResponse response) {

// get the directory location of the atmire-versioning file out of the config
String directory = configurationService.getProperty("atmire-versions.directory");
String directory = defaultIfEmpty(configurationService.getProperty(CONFIG_DIR), "/");
if (!directory.endsWith("/")) {
directory += '/';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"this" : "is for testing"
}
Original file line number Diff line number Diff line change
@@ -1,58 +1,50 @@
package org.dspace.app.rest;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.IOException;

import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
import org.junit.Ignore;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;


public class AtmireVersionsControllerIT extends AbstractControllerIntegrationTest {


@Autowired
private AtmireVersionsController atmireVersionsController;

private final ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
@Override
public void destroy() throws Exception {
configurationService.setProperty(AtmireVersionsController.CONFIG_DIR, null);
super.destroy();
}

@Test
@Ignore
public void test_unauthorisedUserReadsversioning() throws Exception {
// no token given to ensure the request is not authorised
getClient().perform(get("/api/atmire-versions"))
.andExpect(status().isUnauthorized());
}

@Test
@Ignore
public void test_adminReadsNotExistingFile() throws Exception {
when(atmireVersionsController.readFileAsJson(anyString())).thenThrow(new IOException("file not found"));
configurationService.setProperty(AtmireVersionsController.CONFIG_DIR, "/something/wrong/");

String token = getAuthToken(admin.getEmail(), password);
getClient(token).perform(get("/api/atmire-versions"))
.andExpect(status().isNotFound());
}


@Test
public void test_adminReadsVersioningFile() throws Exception {

when(atmireVersionsController.readFileAsJson(anyString())).thenReturn("{ 'some':'json' }");
configurationService.setProperty(AtmireVersionsController.CONFIG_DIR, "src/test/data/dspaceFolder/");

String token = getAuthToken(admin.getEmail(), password);
getClient(token).perform(get("/api/atmire-versions"))
.andExpect(status().isOk())
.andExpect(content().string("{ 'some':'json' }"))
.andExpect(header().string("Content-Type", MediaType.APPLICATION_JSON_VALUE))
.andExpect(header().string("filename", AtmireVersionsController.ATMIRE_VERSIONS_FILE))
.andExpect(header().string("size", ""+AtmireVersionsController.BUFFER_SIZE));
.andExpect(content().string("{\"this\":\"is for testing\"}"))
.andExpect(header().string("Content-Type", "application/json;charset=UTF-8"))
.andExpect(header().string("Content-Disposition",
String.format("attachment;filename=\"%s\"",AtmireVersionsController.ATMIRE_VERSIONS_FILE)));
}
}

0 comments on commit 432aea7

Please sign in to comment.