Search in sources :

Example 1 with Attachment

use of com.icthh.xm.ms.entity.domain.Attachment in project xm-ms-entity by xm-online.

the class AttachmentService method save.

/**
 * Save a attachment.
 *
 * @param attachment the entity to save
 * @return the persisted entity
 */
@LogicExtensionPoint("Save")
public Attachment save(Attachment attachment) {
    startUpdateDateGenerationStrategy.preProcessStartDate(attachment, attachment.getId(), attachmentRepository, Attachment::setStartDate, Attachment::getStartDate);
    Attachment result = attachmentRepository.save(attachment);
    attachmentSearchRepository.save(result);
    return result;
}
Also used : Attachment(com.icthh.xm.ms.entity.domain.Attachment) LogicExtensionPoint(com.icthh.xm.commons.lep.LogicExtensionPoint)

Example 2 with Attachment

use of com.icthh.xm.ms.entity.domain.Attachment in project xm-ms-entity by xm-online.

the class XmEntityServiceImpl method addFileAttachment.

@Override
public XmEntity addFileAttachment(XmEntity entity, MultipartFile file) {
    // save multipart file to storage
    String storedFileName = storageService.store(file, null);
    log.debug("Multipart file stored with name {}", storedFileName);
    String targetTypeKey = entity.getTypeKey();
    TypeSpec typeSpec = xmEntitySpecService.findTypeByKey(targetTypeKey);
    if (typeSpec == null || ObjectUtils.isEmpty(typeSpec.getAttachments())) {
        throw new IllegalStateException("Attachment type key not found for entity " + targetTypeKey);
    }
    // get first attachment spec type for now
    String attachmentTypeKey = typeSpec.getAttachments().stream().findFirst().get().getKey();
    log.debug("Attachment type key {}", attachmentTypeKey);
    Attachment attachment = attachmentService.save(new Attachment().typeKey(attachmentTypeKey).name(file.getOriginalFilename()).contentUrl(storedFileName).startDate(Instant.now()).valueContentType(file.getContentType()).valueContentSize(file.getSize()).xmEntity(entity));
    log.debug("Attachment stored with id {}", attachment.getId());
    entity.getAttachments().add(attachment);
    return entity;
}
Also used : Attachment(com.icthh.xm.ms.entity.domain.Attachment) TypeSpec(com.icthh.xm.ms.entity.domain.spec.TypeSpec)

Example 3 with Attachment

use of com.icthh.xm.ms.entity.domain.Attachment in project xm-ms-entity by xm-online.

the class XmEntityResourceExtendedIntTest method testAttachmentStartDate.

@Test
@Transactional
public void testAttachmentStartDate() throws Exception {
    Attachment attachment = new Attachment().typeKey("A").name("1");
    XmEntity entity = new XmEntity().name(" ").key(randomUUID()).typeKey("TEST_DELETE").attachments(asSet(attachment));
    MutableObject<Instant> startDate = new MutableObject<>();
    MutableObject<XmEntity> entityHolder = new MutableObject<>();
    byte[] content = TestUtil.convertObjectToJsonBytes(entity);
    restXmEntityMockMvc.perform(post("/api/xm-entities").contentType(TestUtil.APPLICATION_JSON_UTF8).content(content)).andDo(r -> entityHolder.setValue(readValue(r))).andDo(r -> log.info(r.getResponse().getContentAsString())).andExpect(status().is2xxSuccessful());
    Long id = entityHolder.getValue().getId();
    restXmEntityMockMvc.perform(get("/api/xm-entities/" + id)).andDo(r -> startDate.setValue(readValue(r).getAttachments().iterator().next().getStartDate())).andDo(r -> log.info(r.getResponse().getContentAsString())).andExpect(status().is2xxSuccessful());
    assertNotNull(startDate.getValue());
    content = TestUtil.convertObjectToJsonBytes(entityHolder.getValue());
    restXmEntityMockMvc.perform(put("/api/xm-entities").contentType(TestUtil.APPLICATION_JSON_UTF8).content(content)).andDo(r -> log.info(r.getResponse().getContentAsString())).andExpect(status().is2xxSuccessful());
    restXmEntityMockMvc.perform(get("/api/xm-entities/" + id)).andDo(r -> log.info(r.getResponse().getContentAsString())).andDo(r -> assertEquals(startDate.getValue(), readValue(r).getAttachments().iterator().next().getStartDate())).andExpect(status().is2xxSuccessful());
}
Also used : MockMvcResultMatchers.jsonPath(org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath) Validator(org.springframework.validation.Validator) SneakyThrows(lombok.SneakyThrows) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) TenantContextHolder(com.icthh.xm.commons.tenant.TenantContextHolder) WebappTenantOverrideConfiguration(com.icthh.xm.ms.entity.config.tenant.WebappTenantOverrideConfiguration) ResultActions(org.springframework.test.web.servlet.ResultActions) XmEntityPermittedRepository(com.icthh.xm.ms.entity.repository.XmEntityPermittedRepository) MockitoAnnotations(org.mockito.MockitoAnnotations) ExceptionTranslator(com.icthh.xm.commons.exceptions.spring.web.ExceptionTranslator) Matchers.everyItem(org.hamcrest.Matchers.everyItem) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) Matchers.nullValue(org.hamcrest.Matchers.nullValue) After(org.junit.After) Spy(org.mockito.Spy) Map(java.util.Map) EntityApp(com.icthh.xm.ms.entity.EntityApp) SpringRunner(org.springframework.test.context.junit4.SpringRunner) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Content(com.icthh.xm.ms.entity.domain.Content) BeforeTransaction(org.springframework.test.context.transaction.BeforeTransaction) MockMvcRequestBuilders.put(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put) Link(com.icthh.xm.ms.entity.domain.Link) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ImmutableMap(com.google.common.collect.ImmutableMap) MediaType(org.springframework.http.MediaType) JsonAutoDetect(com.fasterxml.jackson.annotation.JsonAutoDetect) XmEntitySearchRepository(com.icthh.xm.ms.entity.repository.search.XmEntitySearchRepository) Instant(java.time.Instant) SecurityBeanOverrideConfiguration(com.icthh.xm.ms.entity.config.SecurityBeanOverrideConfiguration) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Assertions.fail(org.assertj.core.api.Assertions.fail) XmAuthenticationContext(com.icthh.xm.commons.security.XmAuthenticationContext) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) WithMockUser(org.springframework.security.test.context.support.WithMockUser) LepManager(com.icthh.xm.lep.api.LepManager) Optional(java.util.Optional) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) XmAuthenticationContextHolder(com.icthh.xm.commons.security.XmAuthenticationContextHolder) Matchers.containsString(org.hamcrest.Matchers.containsString) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CollectionHelper.asSet(org.hibernate.validator.internal.util.CollectionHelper.asSet) THREAD_CONTEXT_KEY_TENANT_CONTEXT(com.icthh.xm.commons.lep.XmLepConstants.THREAD_CONTEXT_KEY_TENANT_CONTEXT) Mock(org.mockito.Mock) Constants(com.icthh.xm.ms.entity.config.Constants) RunWith(org.junit.runner.RunWith) com.icthh.xm.ms.entity.service(com.icthh.xm.ms.entity.service) MockMvcResultMatchers.content(org.springframework.test.web.servlet.result.MockMvcResultMatchers.content) THREAD_CONTEXT_KEY_AUTH_CONTEXT(com.icthh.xm.commons.lep.XmLepConstants.THREAD_CONTEXT_KEY_AUTH_CONTEXT) TenantContextUtils(com.icthh.xm.commons.tenant.TenantContextUtils) MockMvc(org.springframework.test.web.servlet.MockMvc) PageableHandlerMethodArgumentResolver(org.springframework.data.web.PageableHandlerMethodArgumentResolver) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) TestUtil.sameInstant(com.icthh.xm.ms.entity.web.rest.TestUtil.sameInstant) MockMvcRequestBuilders.post(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post) MvcResult(org.springframework.test.web.servlet.MvcResult) MockMvcBuilders(org.springframework.test.web.servlet.setup.MockMvcBuilders) PropertyAccessor(com.fasterxml.jackson.annotation.PropertyAccessor) IdOrKey(com.icthh.xm.ms.entity.domain.ext.IdOrKey) XmEntityServiceImpl(com.icthh.xm.ms.entity.service.impl.XmEntityServiceImpl) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) StartUpdateDateGenerationStrategy(com.icthh.xm.ms.entity.service.impl.StartUpdateDateGenerationStrategy) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) lombok.val(lombok.val) Attachment(com.icthh.xm.ms.entity.domain.Attachment) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) EntityManager(javax.persistence.EntityManager) Tag(com.icthh.xm.ms.entity.domain.Tag) JsonPath(com.jayway.jsonpath.JsonPath) Location(com.icthh.xm.ms.entity.domain.Location) XmEntityPermittedSearchRepository(com.icthh.xm.ms.entity.repository.search.XmEntityPermittedSearchRepository) UUID.randomUUID(java.util.UUID.randomUUID) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Event(com.icthh.xm.ms.entity.domain.Event) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) XmEntityRepository(com.icthh.xm.ms.entity.repository.XmEntityRepository) Calendar(com.icthh.xm.ms.entity.domain.Calendar) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) ProfileEventProducer(com.icthh.xm.ms.entity.repository.kafka.ProfileEventProducer) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) LepConfiguration(com.icthh.xm.ms.entity.config.LepConfiguration) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Transactional(org.springframework.transaction.annotation.Transactional) Instant(java.time.Instant) TestUtil.sameInstant(com.icthh.xm.ms.entity.web.rest.TestUtil.sameInstant) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) Attachment(com.icthh.xm.ms.entity.domain.Attachment) MutableObject(org.apache.commons.lang3.mutable.MutableObject) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Attachment

use of com.icthh.xm.ms.entity.domain.Attachment in project xm-ms-entity by xm-online.

the class XmEntityResourceExtendedIntTest method createEntityComplexIncoming.

/**
 * Creates incoming Entity as from HTTP request.
 * Potentially cam be moved to DTO
 *
 * @param em - Entity manager
 * @return XmEntity for incoming request
 */
public static XmEntity createEntityComplexIncoming(EntityManager em) {
    XmEntity entity = createEntity(em);
    entity.getTags().add(new Tag().typeKey(DEFAULT_TAG_KEY).startDate(DEFAULT_START_DATE).name(DEFAULT_TAG_NAME));
    entity.getAttachments().add(new Attachment().typeKey(DEFAULT_ATTACHMENT_KEY).name(DEFAULT_ATTACHMENT_NAME).startDate(DEFAULT_ATTACHMENT_START_DATE).endDate(DEFAULT_ATTACHMENT_END_DATE).valueContentType(DEFAULT_ATTACHMENT_CONTENT_TYPE).content(new Content().value(DEFAULT_ATTACHMENT_CONTENT_VALUE.getBytes())).contentUrl(DEFAULT_ATTACHMENT_URL).description(DEFAULT_ATTACHMENT_DESCRIPTION));
    entity.getLocations().add(new Location().typeKey(DEFAULT_LOCATION_KEY).name(DEFAULT_LOCATION_NAME).countryKey(DEFAULT_LOCATION_COUNTRY_KEY));
    return entity;
}
Also used : Content(com.icthh.xm.ms.entity.domain.Content) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) Attachment(com.icthh.xm.ms.entity.domain.Attachment) Tag(com.icthh.xm.ms.entity.domain.Tag) Location(com.icthh.xm.ms.entity.domain.Location)

Example 5 with Attachment

use of com.icthh.xm.ms.entity.domain.Attachment in project xm-ms-entity by xm-online.

the class AttachmentResourceExtendedIntTest method createAttachmentWithEntryDate.

@Test
@Transactional
public void createAttachmentWithEntryDate() throws Exception {
    int databaseSizeBeforeCreate = attachmentRepository.findAll().size();
    // Create the Attachment
    restAttachmentMockMvc.perform(post("/api/attachments").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(attachment))).andExpect(status().isCreated()).andExpect(jsonPath("$.startDate").value(MOCKED_START_DATE.toString()));
    // Validate the Attachment in the database
    List<Attachment> voteList = attachmentRepository.findAll();
    assertThat(voteList).hasSize(databaseSizeBeforeCreate + 1);
    Attachment testAttachment = voteList.get(voteList.size() - 1);
    assertThat(testAttachment.getStartDate()).isEqualTo(MOCKED_START_DATE);
    // Validate the Attachment in Elasticsearch
    Attachment attachmentEs = attachmentSearchRepository.findOne(testAttachment.getId());
    assertThat(attachmentEs).isEqualToComparingFieldByField(testAttachment);
}
Also used : Attachment(com.icthh.xm.ms.entity.domain.Attachment) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Attachment (com.icthh.xm.ms.entity.domain.Attachment)14 Test (org.junit.Test)8 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)8 Transactional (org.springframework.transaction.annotation.Transactional)8 XmEntity (com.icthh.xm.ms.entity.domain.XmEntity)6 Location (com.icthh.xm.ms.entity.domain.Location)4 Tag (com.icthh.xm.ms.entity.domain.Tag)4 Calendar (com.icthh.xm.ms.entity.domain.Calendar)3 Link (com.icthh.xm.ms.entity.domain.Link)3 LogicExtensionPoint (com.icthh.xm.commons.lep.LogicExtensionPoint)2 Comment (com.icthh.xm.ms.entity.domain.Comment)2 Content (com.icthh.xm.ms.entity.domain.Content)2 Event (com.icthh.xm.ms.entity.domain.Event)2 Rating (com.icthh.xm.ms.entity.domain.Rating)2 Vote (com.icthh.xm.ms.entity.domain.Vote)2 JsonAutoDetect (com.fasterxml.jackson.annotation.JsonAutoDetect)1 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 PropertyAccessor (com.fasterxml.jackson.annotation.PropertyAccessor)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1