Skip to content

Commit

Permalink
Validation of properties when creating template
Browse files Browse the repository at this point in the history
  • Loading branch information
georgweiss committed Nov 19, 2024
1 parent bafd50b commit 4141e3f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/org/phoebus/olog/LogTemplateResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.commons.collections4.CollectionUtils;
import org.phoebus.olog.entity.LogTemplate;
import org.phoebus.olog.entity.Logbook;
import org.phoebus.olog.entity.Property;
import org.phoebus.olog.entity.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -51,6 +52,10 @@ public class LogTemplateResource {
@SuppressWarnings("unused")
@Autowired
private TagRepository tagRepository;
@SuppressWarnings("unused")
@Autowired
private PropertyRepository propertyRepository;


@GetMapping("{logTemplateId}")
@SuppressWarnings("unused")
Expand Down Expand Up @@ -86,6 +91,7 @@ public LogTemplate createLogTemplate(@RequestBody LogTemplate logTemplate,
if (!CollectionUtils.containsAll(persistedLogbookNames, logbookNames)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, TextUtil.LOG_INVALID_LOGBOOKS);
}

Set<Tag> tags = logTemplate.getTags();
if (tags != null && !tags.isEmpty()) {
Set<String> tagNames = tags.stream().map(Tag::getName).collect(Collectors.toSet());
Expand All @@ -96,6 +102,16 @@ public LogTemplate createLogTemplate(@RequestBody LogTemplate logTemplate,
}
}

Set<Property> properties = logTemplate.getProperties();
if(properties != null && !properties.isEmpty()){
Set<String> propertyNames = properties.stream().map(Property::getName).collect(Collectors.toSet());
Set<String> persistedProperties = new HashSet<>();
propertyRepository.findAll().forEach(p -> persistedProperties.add(p.getName()));
if (!CollectionUtils.containsAll(persistedProperties, propertyNames)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, TextUtil.LOG_INVALID_PROPERTIES);
}
}

LogTemplate newLogTemplate = logTemplateRepository.save(logTemplate);
logger.log(Level.INFO, () -> MessageFormat.format(TextUtil.LOG_TEMPLATE_CREATED, logTemplate.getName(), newLogTemplate.getId()));
return newLogTemplate;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/phoebus/olog/TextUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public class TextUtil {
public static final String LOG_ID_NOT_FOUND = "Log id {0} not found";
public static final String LOG_INVALID_LOGBOOKS = "One or more invalid logbook name(s)";
public static final String LOG_INVALID_TAGS = "One or more invalid tag name(s)";
public static final String LOG_INVALID_PROPERTIES = "One or more invalid property name(s)";
public static final String LOG_MUST_HAVE_LOGBOOK = "A log entry must specify at least one logbook";
public static final String LOG_MUST_HAVE_TITLE = "A log entry must specify a title";
public static final String LOG_NOT_ARCHIVED = "Failed to archive log with id {0}";
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/org/phoebus/olog/LogTemplateResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.parameters.P;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.TestPropertySource;
Expand Down Expand Up @@ -85,6 +86,9 @@ public class LogTemplateResourceTest extends ResourcesTestBase {
@Autowired
private TagRepository tagRepository;

@Autowired
private PropertyRepository propertyRepository;

private static Logbook logbook1;
private static Logbook logbook2;

Expand Down Expand Up @@ -224,6 +228,28 @@ void testCreateLogTemplateBadTags() throws Exception {
reset(logTemplateRepository);
}

@Test
void testCreateLogTemplateBadProperties() throws Exception {
LogTemplate logTemplate = new LogTemplate();
logTemplate.setName("name");
logTemplate.setId(UUID.randomUUID().toString());
logTemplate.setOwner("user");
logTemplate.setTitle("title");
logTemplate.setSource("description");
logTemplate.setLevel("Urgent");
logTemplate.setProperties(Set.of(new Property("invalidPropName")));

when(propertyRepository.findAll()).thenReturn(Collections.singletonList(new Property("validPropName")));
when(logTemplateRepository.save(argThat(new LogTemplateMatcher(logTemplate)))).thenReturn(logTemplate);
MockHttpServletRequestBuilder request = put("/" + OlogResourceDescriptors.LOG_TEMPLATE_RESOURCE_URI)
.content(objectMapper.writeValueAsString(logTemplate))
.header(HttpHeaders.AUTHORIZATION, AUTHORIZATION)
.contentType(JSON);
mockMvc.perform(request).andExpect(status().isBadRequest());
reset(propertyRepository);
reset(logTemplateRepository);
}

@Test
void testCreateLogTemplateDuplicateName() throws Exception {
LogTemplate existing = new LogTemplate();
Expand Down

0 comments on commit 4141e3f

Please sign in to comment.