Commit cd8024b9 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

update option moved to controller

parent dbd13f64
package lcsb.mapviewer.api.configuration;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
......@@ -21,6 +20,7 @@ import lcsb.mapviewer.annotation.services.dapi.DapiConnectionException;
import lcsb.mapviewer.annotation.services.dapi.dto.UserDto;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.converter.Converter;
import lcsb.mapviewer.converter.graphics.AbstractImageGenerator;
import lcsb.mapviewer.converter.graphics.ImageGenerators;
......@@ -33,6 +33,7 @@ import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.field.ModificationState;
import lcsb.mapviewer.model.overlay.DataOverlayType;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.ConfigurationElementType;
import lcsb.mapviewer.model.user.ConfigurationOption;
import lcsb.mapviewer.model.user.annotator.BioEntityField;
import lcsb.mapviewer.modelutils.map.ClassTreeNode;
......@@ -125,13 +126,24 @@ public class ConfigurationController extends BaseController {
configurationRestImpl.acceptDapiRelease(database, release);
}
static class OptionUpdateData {
public ConfigurationOption option;
}
@PreAuthorize("hasAuthority('IS_ADMIN')")
@PatchMapping(value = "/options/{option}")
public ConfigurationOption updateOption(@RequestBody String body, @PathVariable(value = "option") String option)
throws IOException, QueryException {
Map<String, Object> node = parseBody(body);
Map<String, Object> data = getData(node, "option");
return configurationRestImpl.updateOption(option, data);
public ConfigurationOption updateOption(@RequestBody OptionUpdateData data,
@PathVariable(value = "option") ConfigurationElementType type) throws QueryException {
if (data.option == null) {
throw new QueryException("No data provided");
}
String value = data.option.getValue();
try {
configurationService.setConfigurationValue(type, value);
} catch (InvalidArgumentException e) {
throw new QueryException(e.getMessage(), e);
}
return configurationService.getValue(type);
}
public List<Map<String, Object>> getImageFormats() {
......
......@@ -61,17 +61,6 @@ public class ConfigurationRestImpl extends BaseRestImpl {
this.configurationService = configurationService;
}
public ConfigurationOption updateOption(String option, Map<String, Object> data) throws QueryException {
ConfigurationElementType type = ConfigurationElementType.valueOf(option);
String value = (String) data.get("value");
try {
configurationService.setConfigurationValue(type, value);
} catch (InvalidArgumentException e) {
throw new QueryException(e.getMessage(), e);
}
return configurationService.getValue(type);
}
public Map<String, Object> getDapiConfig() {
Map<String, Object> result = new HashMap<>();
result.put("validConnection", dapiConnector.isValidConnection());
......
......@@ -4,7 +4,6 @@ import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import lcsb.mapviewer.api.configuration.AllConfigurationTests;
import lcsb.mapviewer.api.convert.AllConvertTests;
import lcsb.mapviewer.api.files.AllFileTests;
import lcsb.mapviewer.api.genomics.AllGenomicsTests;
......@@ -13,7 +12,7 @@ import lcsb.mapviewer.api.projects.AllProjectTests;
import lcsb.mapviewer.api.users.AllUserTests;
@RunWith(Suite.class)
@SuiteClasses({ AllConfigurationTests.class,
@SuiteClasses({
AllConvertTests.class,
AllFileTests.class,
AllGenomicsTests.class,
......
package lcsb.mapviewer.api.configuration;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
ConfigurationRestImplTest.class })
public class AllConfigurationTests {
}
package lcsb.mapviewer.api.configuration;
import static org.junit.Assert.assertNotNull;
import java.util.HashMap;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.*;
import org.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.api.RestTestFunctions;
import lcsb.mapviewer.model.user.ConfigurationElementType;
import lcsb.mapviewer.model.user.ConfigurationOption;
import lcsb.mapviewer.services.QueryException;
public class ConfigurationRestImplTest extends RestTestFunctions {
@Autowired
public ConfigurationRestImpl configurationRestImpl;
@SuppressWarnings("unused")
private Logger logger = LogManager.getLogger();
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test(expected = QueryException.class)
public void testSetSmtpPortToInvalid() throws Exception {
Map<String, Object> data = new HashMap<>();
data.put("value", "not a number");
configurationRestImpl.updateOption(ConfigurationElementType.EMAIL_SMTP_PORT.name(), data);
}
@Test
public void testSetSmtpPortToValid() throws Exception {
Map<String, Object> data = new HashMap<>();
data.put("value", "255");
ConfigurationOption result = configurationRestImpl.updateOption(
ConfigurationElementType.EMAIL_SMTP_PORT.name(), data);
assertNotNull(result);
}
}
......@@ -49,6 +49,9 @@ public class ConfigurationControllerIntegrationTest extends ControllerIntegratio
@Autowired
private ProjectSnippets snippets;
@Autowired
private ObjectMapper objectMapper;
@Before
public void setup() {
user = createUser(TEST_USER_LOGIN, TEST_USER_PASSWORD);
......@@ -68,10 +71,10 @@ public class ConfigurationControllerIntegrationTest extends ControllerIntegratio
option.put("value", "not a number");
Map<String, Object> data = new HashMap<>();
data.put("option", option);
String body = new Gson().toJson(data);
RequestBuilder request = patch("/api/configuration/options/" + ConfigurationElementType.EMAIL_SMTP_PORT.name())
.content(body)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(data))
.session(session);
mockMvc.perform(request)
......@@ -118,10 +121,10 @@ public class ConfigurationControllerIntegrationTest extends ControllerIntegratio
option.put("value", ConfigurationElementType.EMAIL_SMTP_PORT.getDefaultValue());
Map<String, Object> data = new HashMap<>();
data.put("option", option);
String body = new Gson().toJson(data);
RequestBuilder request = patch("/api/configuration/options/{option}", ConfigurationElementType.EMAIL_SMTP_PORT.name())
.content(body)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(data))
.session(session);
mockMvc.perform(request)
......@@ -134,6 +137,38 @@ public class ConfigurationControllerIntegrationTest extends ControllerIntegratio
.andExpect(status().is2xxSuccessful());
}
@Test
public void testSetOptionEmptyBody() throws Exception {
MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
RequestBuilder request = patch("/api/configuration/options/{option}", ConfigurationElementType.EMAIL_SMTP_PORT.name())
.contentType(MediaType.APPLICATION_JSON)
.content("{}")
.session(session);
mockMvc.perform(request)
.andExpect(status().isBadRequest());
}
@Test
public void testSetOptionInvalidType() throws Exception {
MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
Map<String, Object> option = new HashMap<>();
option.put("value", "25");
Map<String, Object> data = new HashMap<>();
data.put("option", option);
RequestBuilder request = patch("/api/configuration/options/{option}", "blabla")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(data))
.session(session);
mockMvc.perform(request)
.andExpect(status().isBadRequest());
}
@Test
public void accessConfigurationOptionsAsAdmin() throws Exception {
MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
......@@ -162,10 +197,10 @@ public class ConfigurationControllerIntegrationTest extends ControllerIntegratio
option.put("value", "123");
Map<String, Object> data = new HashMap<>();
data.put("option", option);
String body = new Gson().toJson(data);
RequestBuilder request = patch("/api/configuration/options/" + ConfigurationElementType.EMAIL_SMTP_PORT.name())
.content(body)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(data))
.session(session);
mockMvc.perform(request)
......@@ -274,7 +309,7 @@ public class ConfigurationControllerIntegrationTest extends ControllerIntegratio
user.setEmail("piotr.gawron@uni.lu");
RequestBuilder request = post("/api/configuration/dapi:registerUser")
.content(new ObjectMapper().writeValueAsString(user))
.content(objectMapper.writeValueAsString(user))
.contentType(MediaType.APPLICATION_JSON)
.session(session);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment