Skip to content

Commit

Permalink
Add test to ensure content attributes are updated when content is upd…
Browse files Browse the repository at this point in the history
…ated on an entity with an @Version atrtibute

Fixes #79
  • Loading branch information
Paul Warren committed Oct 23, 2019
1 parent b087999 commit d03b8c6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,6 @@ protected void handleMultipart(HttpServletRequest request, HttpServletResponse r

info.getImpementation().setContent(domainObj, content);

// re-fetch to make sure we have the latest
if (BeanUtils.hasFieldWithAnnotation(domainObj, Version.class)) {
domainObj = findOne(repositories, info.getDomainObjectClass(), id);
}

save(repositories, domainObj);

if (isNew) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public class ContentEntityRestControllerIntegrationTest {
version.setRepo(repo4);
version.setStore(store4);
version.setEtag(format("\"%s\"", testEntity4.getVersion()));
version.setEntity(testEntity4);
});
version = Version.tests();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package internal.org.springframework.content.rest.controllers;

import java.util.Optional;

import internal.org.springframework.content.rest.support.ContentEntity;
import lombok.Setter;

import org.springframework.content.commons.repository.ContentStore;
import org.springframework.data.repository.CrudRepository;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;

import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.Context;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.FIt;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.It;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
Expand All @@ -19,13 +25,15 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@Setter
public class Version {

private MockMvc mvc;
private String url;
private CrudRepository repo;
private ContentStore store;
private String etag;
private ContentEntity entity;

public static Version tests() {
return new Version();
Expand Down Expand Up @@ -71,9 +79,25 @@ public static Version tests() {
It("should update the content", () -> {
mvc.perform(put(url)
.content("Hello Modified Spring Content World!")
.contentType("text/plain")
.contentType("text/other")
.header("if-match", etag))
.andExpect(status().isOk());
});
It("should update the content attributes", () -> {
mvc.perform(multipart(url)
.file(new MockMultipartFile("file",
"test-file-modified.txt",
"text/other", "Hello Modified Spring Content World!".getBytes()))
.header("if-match", etag))
.andExpect(status().isOk());

if (entity != null) {
Optional<ContentEntity> fetched = repo.findById(entity.getId());
assertThat(fetched.isPresent(), is(true));
assertThat(fetched.get().getLen(), is(36L));
assertThat(fetched.get().getMimeType(), is("text/other"));
assertThat(fetched.get().getOriginalFileName(), is("test-file-modified.txt"));
}
});
});
Context("a PUT to /{store}/{id} with a non-matching If-Match header", () -> {
Expand Down Expand Up @@ -131,24 +155,4 @@ public static Version tests() {
});
});
}

public void setMvc(MockMvc mvc) {
this.mvc = mvc;
}

public void setUrl(String url) {
this.url = url;
}

public void setRepo(CrudRepository repo) {
this.repo = repo;
}

public void setStore(ContentStore store) {
this.store = store;
}

public void setEtag(String etag) {
this.etag = etag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.content.commons.annotations.ContentId;
import org.springframework.content.commons.annotations.ContentLength;
import org.springframework.content.commons.annotations.MimeType;
import org.springframework.content.commons.annotations.OriginalFileName;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand All @@ -23,7 +24,7 @@
@EntityListeners(AuditingEntityListener.class)
@Getter
@Setter
public class TestEntity4 {
public class TestEntity4 implements ContentEntity {
public @Id @GeneratedValue Long id;

public String name;
Expand All @@ -33,4 +34,5 @@ public class TestEntity4 {
private @Version Long version;
private @CreatedDate Date createdDate;
private @LastModifiedDate Date modifiedDate;
private @OriginalFileName String originalFileName;
}

0 comments on commit d03b8c6

Please sign in to comment.