config service: return status based on whether set/unset were successful

This commit is contained in:
Adam
2019-08-08 12:57:38 -04:00
parent 26855079fe
commit 730ad1c3c6
3 changed files with 19 additions and 8 deletions

View File

@@ -81,7 +81,10 @@ public class ConfigController
return; return;
} }
configService.setKey(session.getUser(), key, value); if (!configService.setKey(session.getUser(), key, value))
{
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
} }
@RequestMapping(path = "/{key:.+}", method = DELETE) @RequestMapping(path = "/{key:.+}", method = DELETE)
@@ -98,6 +101,9 @@ public class ConfigController
return; return;
} }
configService.unsetKey(session.getUser(), key); if (!configService.unsetKey(session.getUser(), key))
{
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
} }
} }

View File

@@ -121,7 +121,7 @@ public class ConfigService
return new Configuration(config); return new Configuration(config);
} }
public void setKey( public boolean setKey(
int userId, int userId,
String key, String key,
@Nullable String value @Nullable String value
@@ -129,39 +129,41 @@ public class ConfigService
{ {
if (key.startsWith("$") || key.startsWith("_")) if (key.startsWith("$") || key.startsWith("_"))
{ {
return; return false;
} }
String[] split = key.split("\\.", 2); String[] split = key.split("\\.", 2);
if (split.length != 2) if (split.length != 2)
{ {
return; return false;
} }
Object jsonValue = parseJsonString(value); Object jsonValue = parseJsonString(value);
mongoCollection.updateOne(eq("_userId", userId), mongoCollection.updateOne(eq("_userId", userId),
set(split[0] + "." + split[1].replace('.', ':'), jsonValue), set(split[0] + "." + split[1].replace('.', ':'), jsonValue),
upsertUpdateOptions); upsertUpdateOptions);
return true;
} }
public void unsetKey( public boolean unsetKey(
int userId, int userId,
String key String key
) )
{ {
if (key.startsWith("$") || key.startsWith("_")) if (key.startsWith("$") || key.startsWith("_"))
{ {
return; return false;
} }
String[] split = key.split("\\.", 2); String[] split = key.split("\\.", 2);
if (split.length != 2) if (split.length != 2)
{ {
return; return false;
} }
mongoCollection.updateOne(eq("_userId", userId), mongoCollection.updateOne(eq("_userId", userId),
unset(split[0] + "." + split[1].replace('.', ':'))); unset(split[0] + "." + split[1].replace('.', ':')));
return true;
} }
private static Object parseJsonString(String value) private static Object parseJsonString(String value)

View File

@@ -34,6 +34,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@@ -67,6 +68,8 @@ public class ConfigControllerTest
{ {
when(authFilter.handle(any(HttpServletRequest.class), any(HttpServletResponse.class))) when(authFilter.handle(any(HttpServletRequest.class), any(HttpServletResponse.class)))
.thenReturn(mock(SessionEntry.class)); .thenReturn(mock(SessionEntry.class));
when(configService.setKey(anyInt(), anyString(), anyString())).thenReturn(true);
} }
@Test @Test