From 4141e3fb3dac5ed3ff466d84bc40774822242069 Mon Sep 17 00:00:00 2001 From: georgweiss Date: Tue, 19 Nov 2024 13:56:00 +0100 Subject: [PATCH] Validation of properties when creating template --- .../org/phoebus/olog/LogTemplateResource.java | 16 ++++++++++++ src/main/java/org/phoebus/olog/TextUtil.java | 1 + .../phoebus/olog/LogTemplateResourceTest.java | 26 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/src/main/java/org/phoebus/olog/LogTemplateResource.java b/src/main/java/org/phoebus/olog/LogTemplateResource.java index 54033ee..d62217d 100644 --- a/src/main/java/org/phoebus/olog/LogTemplateResource.java +++ b/src/main/java/org/phoebus/olog/LogTemplateResource.java @@ -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; @@ -51,6 +52,10 @@ public class LogTemplateResource { @SuppressWarnings("unused") @Autowired private TagRepository tagRepository; + @SuppressWarnings("unused") + @Autowired + private PropertyRepository propertyRepository; + @GetMapping("{logTemplateId}") @SuppressWarnings("unused") @@ -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 tags = logTemplate.getTags(); if (tags != null && !tags.isEmpty()) { Set tagNames = tags.stream().map(Tag::getName).collect(Collectors.toSet()); @@ -96,6 +102,16 @@ public LogTemplate createLogTemplate(@RequestBody LogTemplate logTemplate, } } + Set properties = logTemplate.getProperties(); + if(properties != null && !properties.isEmpty()){ + Set propertyNames = properties.stream().map(Property::getName).collect(Collectors.toSet()); + Set 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; diff --git a/src/main/java/org/phoebus/olog/TextUtil.java b/src/main/java/org/phoebus/olog/TextUtil.java index 07f17f1..e0c3678 100644 --- a/src/main/java/org/phoebus/olog/TextUtil.java +++ b/src/main/java/org/phoebus/olog/TextUtil.java @@ -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}"; diff --git a/src/test/java/org/phoebus/olog/LogTemplateResourceTest.java b/src/test/java/org/phoebus/olog/LogTemplateResourceTest.java index 7565208..0f04f9c 100644 --- a/src/test/java/org/phoebus/olog/LogTemplateResourceTest.java +++ b/src/test/java/org/phoebus/olog/LogTemplateResourceTest.java @@ -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; @@ -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; @@ -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();