Commit 2f4dd7d2 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

privileges are serialized properly

parent 557180a3
......@@ -15,16 +15,9 @@ import javax.persistence.UniqueConstraint;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lcsb.mapviewer.modelutils.serializer.model.security.PrivilegeDeserializer;
import lcsb.mapviewer.modelutils.serializer.model.security.PrivilegeSerializer;
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "type", "objectId" }))
@JsonSerialize(using = PrivilegeSerializer.class)
@JsonDeserialize(using = PrivilegeDeserializer.class)
public class Privilege implements Serializable {
private static final long serialVersionUID = 1L;
......
package lcsb.mapviewer.modelutils.serializer.model.security;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lcsb.mapviewer.model.security.Privilege;
public class PrivilegeDeserializer extends StdDeserializer<Privilege> {
/**
*
*/
private static final long serialVersionUID = 1L;
public PrivilegeDeserializer() {
super(Privilege.class);
}
@Override
public Privilege deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) parser.getCodec();
ObjectNode rootNode = mapper.readTree(parser);
return new PrivilegeKeyDeserializer().deserializeKey(rootNode.asText(), ctxt);
}
}
package lcsb.mapviewer.modelutils.serializer.model.security;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lcsb.mapviewer.model.security.Privilege;
public class PrivilegeSerializer extends JsonSerializer<Privilege> {
@Override
public void serialize(final Privilege entry, final JsonGenerator gen,
final SerializerProvider serializers)
throws IOException {
if (entry.isObjectPrivilege()) {
gen.writeString(entry.getType()+":"+entry.getObjectId());
} else {
gen.writeString(entry.getType().name());
}
}
}
\ No newline at end of file
......@@ -144,19 +144,25 @@ public class UserControllerIntegrationTest extends ControllerIntegrationTest {
RequestBuilder request = patch("/api/users/{login}:updatePrivileges", TEST_USER_LOGIN)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsBytes(data))
.content(objectMapper.writeValueAsString(data))
.session(session);
mockMvc.perform(request)
String response = mockMvc.perform(request)
.andDo(document("user/update_privilege_1",
userPathParameters(),
updatePrivilegeRequest(),
responseFields(getUserResponseFields())))
.andExpect(status().is2xxSuccessful());
callInSeparateThread(() -> {
assertEquals(1, userService.getUserByLogin(TEST_USER_LOGIN).getPrivileges().size());
return null;
.andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsString();
Map<String, Object> responseObject = objectMapper.readValue(response, new TypeReference<Map<String, Object>>() {
});
assertTrue(responseObject.get("privileges") instanceof List);
List<?> privileges = (List<?>) responseObject.get("privileges");
assertEquals(1, privileges.size());
assertFalse(privileges.get(0) instanceof String);
assertEquals(1, userService.getUserByLogin(TEST_USER_LOGIN, true).getPrivileges().size());
}
@Test
......
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