Skip to content

Commit

Permalink
SHIBUI-2270 / SHIBUI-2373
Browse files Browse the repository at this point in the history
Fixing output JSON for the UI so type values are correct
  • Loading branch information
chasegawa committed Sep 2, 2022
1 parent ec9713f commit 7eead4a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.internet2.tier.shibboleth.admin.ui.domain.shib.properties;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
import org.hibernate.envers.Audited;

Expand All @@ -12,6 +13,7 @@
@Entity(name = "shib_property_setting")
@Audited
@Data
@JsonSerialize(using = ShibPropertySettingJacksonSerializer.class)
public class ShibPropertySetting {
@Id
@GeneratedValue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package edu.internet2.tier.shibboleth.admin.ui.domain.shib.properties;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

public class ShibPropertySettingJacksonSerializer extends StdSerializer<ShibPropertySetting> {
public ShibPropertySettingJacksonSerializer() {
this(null);
}

public ShibPropertySettingJacksonSerializer(Class<ShibPropertySetting> t) {
super(t);
}

@Override
public void serialize(ShibPropertySetting sps, JsonGenerator generator, SerializerProvider provider) throws IOException {
generator.writeStartObject();
generator.writeNumberField("resourceId", sps.getResourceId());
generator.writeStringField("configFile", sps.getConfigFile());
generator.writeStringField("propertyName", sps.getPropertyName());
if (sps.getCategory() != null) {
generator.writeStringField("category", sps.getCategory());
}
generator.writeStringField("displayType", sps.getDisplayType());

switch (sps.getDisplayType()) {
case "boolean":
generator.writeBooleanField("propertyValue", Boolean.valueOf(sps.getPropertyValue()));
break;
case "number":
generator.writeNumberField("propertyValue", Long.parseLong(sps.getPropertyValue()));
break;
default:
generator.writeStringField("propertyValue", sps.getPropertyValue());
}

generator.writeEndObject();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class ShibPropertiesControllerTests extends AbstractBaseDataJpaTest {
it.propertyName = 'foo'
it.configFile = 'defaults.properties'
it.propertyValue = 'bar'
it.displayType = 'string'

it
}
Expand All @@ -71,6 +72,7 @@ class ShibPropertiesControllerTests extends AbstractBaseDataJpaTest {
it.propertyName = 'foo2'
it.configFile = 'defaults.properties'
it.propertyValue = 'bar2'
it.displayType = 'string'

it
}
Expand Down Expand Up @@ -151,13 +153,15 @@ class ShibPropertiesControllerTests extends AbstractBaseDataJpaTest {
it.propertyName = 'food.for.thought'
it.configFile = 'defaults.properties'
it.propertyValue = 'true'
it.displayType = 'boolean'

it
}
ShibPropertySetting prop2 = new ShibPropertySetting().with { it ->
it.propertyName = 'food2.for2.thought'
it.configFile = 'defaults.properties'
it.propertyValue = 'true'
it.displayType = 'boolean'

it
}
Expand Down Expand Up @@ -207,4 +211,60 @@ class ShibPropertiesControllerTests extends AbstractBaseDataJpaTest {
result.andExpect(status().isOk()).andExpect(jsonPath("\$.name").value("newName"))
propertySetRepo.findByResourceId(defaultSetResourceId).name.equals("newName")
}

@WithMockAdmin
def "Validate that JSON data is correct for UI"() {
given:
ShibPropertySetting prop = new ShibPropertySetting().with { it ->
it.propertyName = 'asBoolean'
it.configFile = 'defaults.properties'
it.propertyValue = 'true'
it.displayType = 'boolean'

it
}
propertySettingRepo.save(prop)
ShibPropertySetting prop2 = new ShibPropertySetting().with { it ->
it.propertyName = 'asNumber'
it.configFile = 'defaults.properties'
it.propertyValue = '33'
it.displayType = 'number'

it
}
propertySettingRepo.save(prop2)
ShibPropertySetting prop3 = new ShibPropertySetting().with { it ->
it.propertyName = 'anythingElse'
it.configFile = 'defaults.properties'
it.propertyValue = '33'
it.displayType = 'string'

it
}
propertySettingRepo.save(prop3)
ShibPropertySet set = new ShibPropertySet().with {it ->
it.properties.add(prop)
it.properties.add(prop2)
it.properties.add(prop3)
it.name = 'somerandom'

it
}
def savedSet = propertySetRepo.save(set)
entityManager.flush()
entityManager.clear()

when:
def result = mockMvc.perform(get("/api/shib/property/set/" + savedSet.getResourceId()))
System.println(result.andReturn().getResponse().getContentAsString())
then:
result.andExpect(status().isOk())
.andExpect(jsonPath("\$.resourceId").value(savedSet.getResourceId()))
.andExpect(jsonPath("\$.properties[0].propertyName").value("asBoolean"))
.andExpect(jsonPath("\$.properties[0].propertyValue").value(Boolean.TRUE))
.andExpect(jsonPath("\$.properties[1].propertyName").value("asNumber"))
.andExpect(jsonPath("\$.properties[1].propertyValue").value(33))
.andExpect(jsonPath("\$.properties[2].propertyName").value("anythingElse"))
.andExpect(jsonPath("\$.properties[2].propertyValue").value("33"))
}
}

0 comments on commit 7eead4a

Please sign in to comment.