diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/data/ContentBlockRepository.java b/vspace/src/main/java/edu/asu/diging/vspace/core/data/ContentBlockRepository.java index 3de254d4b..cb8e144f5 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/core/data/ContentBlockRepository.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/data/ContentBlockRepository.java @@ -1,13 +1,22 @@ package edu.asu.diging.vspace.core.data; import org.javers.spring.annotation.JaversSpringDataAuditable; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.stereotype.Repository; +import java.util.List; + +import javax.transaction.Transactional; import edu.asu.diging.vspace.core.model.impl.ContentBlock; @Repository @JaversSpringDataAuditable public interface ContentBlockRepository extends PagingAndSortingRepository { + @Query("SELECT max(contentOrder) FROM ContentBlock d WHERE d.slide.id = ?1") + public Integer findMaxContentOrder(String slideId); + + public List findBySlide_IdAndContentOrderGreaterThan(String slideId,Integer contentOrder); } diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/IContentBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/IContentBlockManager.java index f39fbe3aa..01193b297 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/core/services/IContentBlockManager.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/IContentBlockManager.java @@ -20,11 +20,11 @@ CreationReturnValue createImageBlock(String slideId, byte[] image, String filena List getAllContentBlocks(String slideId); - void deleteTextBlockById(String blockid) throws BlockDoesNotExistException; + void deleteTextBlockById(String blockid,String slideId) throws BlockDoesNotExistException; - void deleteImageBlockById(String blockid) throws BlockDoesNotExistException; + void deleteImageBlockById(String blockid,String slideId) throws BlockDoesNotExistException; - void deleteChoiceBlockById(String blockid) throws BlockDoesNotExistException; + void deleteChoiceBlockById(String blockid,String slideId) throws BlockDoesNotExistException; void updateTextBlock(TextBlock textBlock); @@ -34,9 +34,10 @@ CreationReturnValue createImageBlock(String slideId, byte[] image, String filena IChoiceBlock getChoiceBlock(String choiceBlockId); - void updateImageBlock(IImageBlock imageBlock, byte[] image, String filename, Integer contentOrder) + void updateImageBlock(IImageBlock imageBlock, byte[] image, String filename) throws ImageCouldNotBeStoredException; IChoiceBlock createChoiceBlock(String slideId, List selectedChoices, Integer contentOrder, boolean showsAll); - + + Integer findMaxContentOrder(String slideId); } diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/ContentBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/ContentBlockManager.java index 0a62a9106..7ec9ab54b 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/ContentBlockManager.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/ContentBlockManager.java @@ -13,6 +13,7 @@ import org.springframework.stereotype.Service; import edu.asu.diging.vspace.core.data.ChoiceContentBlockRepository; +import edu.asu.diging.vspace.core.data.ContentBlockRepository; import edu.asu.diging.vspace.core.data.ImageContentBlockRepository; import edu.asu.diging.vspace.core.data.ImageRepository; import edu.asu.diging.vspace.core.data.TextContentBlockRepository; @@ -32,6 +33,7 @@ import edu.asu.diging.vspace.core.model.ITextBlock; import edu.asu.diging.vspace.core.model.IVSImage; import edu.asu.diging.vspace.core.model.impl.ChoiceBlock; +import edu.asu.diging.vspace.core.model.impl.ContentBlock; import edu.asu.diging.vspace.core.model.impl.ImageBlock; import edu.asu.diging.vspace.core.model.impl.TextBlock; import edu.asu.diging.vspace.core.model.impl.VSImage; @@ -72,6 +74,9 @@ public class ContentBlockManager implements IContentBlockManager { @Autowired private IStorageEngine storage; + @Autowired + private ContentBlockRepository contentBlockRepository; + /* * (non-Javadoc) * @@ -87,7 +92,8 @@ public List getAllContentBlocks(String slideId) { /* * (non-Javadoc) * - * @see edu.asu.diging.vspace.core.services.impl.ITextBlock#createTextBlock(java. + * @see + * edu.asu.diging.vspace.core.services.impl.ITextBlock#createTextBlock(java. * lang.String, java.lang.String) */ @Override @@ -148,20 +154,31 @@ public CreationReturnValue createImageBlock(String slideId, byte[] image, String } /** - * Delete a text block using an id + * Delete a text block using an id and also decrease content order by 1 of all + * the slide's block which are after this block + * + * @param blockId - id of resource to be deleted. If the id is null then the + * functions returns nothing. + * @param slideId - id of the slide in which the text block with blockId is + * present. * - * @param id - id of resource to be deleted. If the id is null then the - * functions returns nothing. - * */ @Override - public void deleteTextBlockById(String id) throws BlockDoesNotExistException { - if (id == null) { + public void deleteTextBlockById(String blockId, String slideId) throws BlockDoesNotExistException { + if (blockId == null) { return; } + Integer contentOrder = null; + Optional contentBlock = contentBlockRepository.findById(blockId); + if (contentBlock.isPresent()) { + contentOrder = contentBlock.get().getContentOrder(); + } else { + throw new BlockDoesNotExistException("Block Id not present"); + } try { - textBlockRepo.deleteById(id); + textBlockRepo.deleteById(blockId); + updateContentOrder(slideId, contentOrder); } catch (EmptyResultDataAccessException e) { throw new BlockDoesNotExistException(e); } @@ -169,20 +186,30 @@ public void deleteTextBlockById(String id) throws BlockDoesNotExistException { } /** - * Delete an image block using an id + * Delete an image block using an id and also decrease content order by 1 of all + * the slide's block which are after this block * - * @param id - id of resource to be deleted. If the id is null then the - * functions returns nothing. - * + * @param blockId - id of resource to be deleted. If the id is null then the + * functions returns nothing. + * @param slideId - id of the slide in which the Image block with blockId is + * present. */ @Override - public void deleteImageBlockById(String id) throws BlockDoesNotExistException { - if (id == null) { + public void deleteImageBlockById(String blockId, String slideId) throws BlockDoesNotExistException { + if (blockId == null) { return; } + Integer contentOrder = null; + Optional contentBlock = contentBlockRepository.findById(blockId); + if (contentBlock.isPresent()) { + contentOrder = contentBlock.get().getContentOrder(); + } else { + throw new BlockDoesNotExistException("Block Id not present"); + } try { - imageBlockRepo.deleteById(id); + imageBlockRepo.deleteById(blockId); + updateContentOrder(slideId, contentOrder); } catch (EmptyResultDataAccessException e) { throw new BlockDoesNotExistException(e); } @@ -190,20 +217,30 @@ public void deleteImageBlockById(String id) throws BlockDoesNotExistException { } /** - * Delete a choices block using an id + * Delete a choices block using an id and also decrease content order by 1 of + * all the slide's block which are after this block * - * @param id - id of resource to be deleted. If the id is null then the - * functions returns nothing. - * + * @param blockId - id of resource to be deleted. If the id is null then the + * functions returns nothing. + * @param slideId - id of the slide in which the choice block with blockId is + * present. */ @Override - public void deleteChoiceBlockById(String id) throws BlockDoesNotExistException { - if (id == null) { + public void deleteChoiceBlockById(String blockId, String slideId) throws BlockDoesNotExistException { + if (blockId == null) { return; } + Integer contentOrder = null; + Optional contentBlock = contentBlockRepository.findById(blockId); + if (contentBlock.isPresent()) { + contentOrder = contentBlock.get().getContentOrder(); + } else { + throw new BlockDoesNotExistException("Block Id not present"); + } try { - choiceBlockRepo.deleteById(id); + choiceBlockRepo.deleteById(blockId); + updateContentOrder(slideId, contentOrder); } catch (EmptyResultDataAccessException e) { throw new BlockDoesNotExistException(e); } @@ -215,7 +252,7 @@ public void updateTextBlock(TextBlock textBlock) { } @Override - public void updateImageBlock(IImageBlock imageBlock, byte[] image, String filename, Integer contentOrder) + public void updateImageBlock(IImageBlock imageBlock, byte[] image, String filename) throws ImageCouldNotBeStoredException { IVSImage slideContentImage = saveImage(image, filename); storeImageFile(image, slideContentImage, filename); @@ -253,17 +290,49 @@ public IChoiceBlock getChoiceBlock(String choiceBlockId) { /* * (non-Javadoc) * - * @see edu.asu.diging.vspace.core.services.impl.IChoiceBlock#createTextBlock(java. + * @see + * edu.asu.diging.vspace.core.services.impl.IChoiceBlock#createTextBlock(java. * lang.String, java.lang.String, java.lang.Integer) */ @Override - public IChoiceBlock createChoiceBlock(String slideId, List selectedChoices, Integer contentOrder, boolean showsAll) { + public IChoiceBlock createChoiceBlock(String slideId, List selectedChoices, Integer contentOrder, + boolean showsAll) { List choices = new ArrayList(); - if(!showsAll) { - choices = selectedChoices.stream().map(choice -> slideManager.getChoice(choice)).collect(Collectors.toList()); + if (!showsAll) { + choices = selectedChoices.stream().map(choice -> slideManager.getChoice(choice)) + .collect(Collectors.toList()); } - IChoiceBlock choiceBlock = choiceBlockFactory.createChoiceBlock(slideManager.getSlide(slideId), contentOrder, choices, showsAll); - return choiceBlockRepo.save((ChoiceBlock)choiceBlock); + IChoiceBlock choiceBlock = choiceBlockFactory.createChoiceBlock(slideManager.getSlide(slideId), contentOrder, + choices, showsAll); + return choiceBlockRepo.save((ChoiceBlock) choiceBlock); + } + + /** + * Retrieving the maximum content order for a slide + */ + @Override + public Integer findMaxContentOrder(String slideId) { + return contentBlockRepository.findMaxContentOrder(slideId); } + /** + * Decreasing content order by 1 of the slide's block which are after the + * specified contentOrder + * + * @param slideId The Id of the slide for which content orders will be + * updated + * @param contentOrder The content orders of the slides which are greater than + * contentOrder will be updated + */ + private void updateContentOrder(String slideId, Integer contentOrder) { + + List contentBlockList = contentBlockRepository.findBySlide_IdAndContentOrderGreaterThan(slideId, + contentOrder); + if (contentBlockList != null) { + for (ContentBlock eachContentBlock : contentBlockList) { + eachContentBlock.setContentOrder(eachContentBlock.getContentOrder() - 1); + } + contentBlockRepository.saveAll(contentBlockList); + } + } } diff --git a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddChoiceBlockController.java b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddChoiceBlockController.java index dfb84d2ae..6b6f0b595 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddChoiceBlockController.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddChoiceBlockController.java @@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; +import edu.asu.diging.vspace.core.data.ContentBlockRepository; import edu.asu.diging.vspace.core.model.IChoiceBlock; import edu.asu.diging.vspace.core.model.ISequence; import edu.asu.diging.vspace.core.services.IContentBlockManager; @@ -26,20 +27,27 @@ public class AddChoiceBlockController { private IContentBlockManager contentBlockManager; @RequestMapping(value = "/staff/module/{moduleId}/slide/{id}/choice/content", method = RequestMethod.POST) - public ResponseEntity> addChoiceBlock(@PathVariable("id") String slideId, - @PathVariable("moduleId") String moduleId, - @RequestParam("contentOrder") Integer contentOrder, @RequestParam("selectedChoices") List selectedChoices, + public ResponseEntity> addChoiceBlock(@PathVariable("id") String slideId, + @PathVariable("moduleId") String moduleId, @RequestParam("selectedChoices") List selectedChoices, @RequestParam("showsAll") boolean showsAll) throws IOException { - IChoiceBlock choiceBlock = contentBlockManager.createChoiceBlock(slideId, selectedChoices, contentOrder, showsAll); - /*After annotating sequence attribute with JsonIgnore in Choice model to fix stack overflow issue - * the sequences are not returned as part of choiceBlock object, hence sequences are explicitly passed - * as part of Response in a list form*/ - Map responseData = new HashMap(); - responseData.put("choiceBlock",choiceBlock); - if(!choiceBlock.isShowsAll()) { - List selectedSequences = choiceBlock.getChoices().stream().map(choice->choice.getSequence()).collect(Collectors.toList()); - responseData.put("selectedSequences",selectedSequences); + Integer contentOrder = contentBlockManager.findMaxContentOrder(slideId); + contentOrder = contentOrder == null ? 0 : contentOrder + 1; + + IChoiceBlock choiceBlock = contentBlockManager.createChoiceBlock(slideId, selectedChoices, contentOrder, + showsAll); + /* + * After annotating sequence attribute with JsonIgnore in Choice model to fix + * stack overflow issue the sequences are not returned as part of choiceBlock + * object, hence sequences are explicitly passed as part of Response in a list + * form + */ + Map responseData = new HashMap(); + responseData.put("choiceBlock", choiceBlock); + if (!choiceBlock.isShowsAll()) { + List selectedSequences = choiceBlock.getChoices().stream().map(choice -> choice.getSequence()) + .collect(Collectors.toList()); + responseData.put("selectedSequences", selectedSequences); } return new ResponseEntity<>(responseData, HttpStatus.OK); } diff --git a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddImageBlockController.java b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddImageBlockController.java index 9172f7739..ed3b727db 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddImageBlockController.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddImageBlockController.java @@ -17,6 +17,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; +import edu.asu.diging.vspace.core.data.ContentBlockRepository; import edu.asu.diging.vspace.core.exception.ImageCouldNotBeStoredException; import edu.asu.diging.vspace.core.model.IVSpaceElement; import edu.asu.diging.vspace.core.services.IContentBlockManager; @@ -30,9 +31,8 @@ public class AddImageBlockController { @RequestMapping(value = "/staff/module/{moduleId}/slide/{id}/image", method = RequestMethod.POST) public ResponseEntity addImageBlock(@PathVariable("id") String slideId, - @PathVariable("moduleId") String moduleId, @RequestParam("file") MultipartFile file, - @RequestParam("contentOrder") Integer contentOrder, Principal principal, RedirectAttributes attributes) - throws IOException { + @PathVariable("moduleId") String moduleId, @RequestParam("file") MultipartFile file, Principal principal, + RedirectAttributes attributes) throws IOException { byte[] image = null; String filename = null; @@ -41,6 +41,9 @@ public ResponseEntity addImageBlock(@PathVariable("id") String slideId, filename = file.getOriginalFilename(); } String imageId; + Integer contentOrder = contentBlockManager.findMaxContentOrder(slideId); + contentOrder = contentOrder == null ? 0 : contentOrder + 1; + try { CreationReturnValue imageBlockReturnValue = contentBlockManager.createImageBlock(slideId, image, filename, contentOrder); diff --git a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddTextBlockController.java b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddTextBlockController.java index 2d7509686..54f23bdc6 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddTextBlockController.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddTextBlockController.java @@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; +import edu.asu.diging.vspace.core.data.ContentBlockRepository; import edu.asu.diging.vspace.core.model.ITextBlock; import edu.asu.diging.vspace.core.services.IContentBlockManager; @@ -22,9 +23,11 @@ public class AddTextBlockController { @RequestMapping(value = "/staff/module/{moduleId}/slide/{id}/textcontent", method = RequestMethod.POST) public ResponseEntity addTextBlock(@PathVariable("id") String slideId, - @PathVariable("moduleId") String moduleId, @RequestParam("content") String content, - @RequestParam("contentOrder") Integer contentOrder) throws IOException { + @PathVariable("moduleId") String moduleId, @RequestParam("content") String content) throws IOException { + Integer contentOrder = contentBlockManager.findMaxContentOrder(slideId); + contentOrder = contentOrder == null ? 0 : contentOrder + 1; + ITextBlock textBlock = contentBlockManager.createTextBlock(slideId, content, contentOrder); return new ResponseEntity<>(textBlock.getId(), HttpStatus.OK); diff --git a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/DeleteChoiceBlockController.java b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/DeleteChoiceBlockController.java index 2a5217232..9adf436dc 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/DeleteChoiceBlockController.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/DeleteChoiceBlockController.java @@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import edu.asu.diging.vspace.core.data.ContentBlockRepository; import edu.asu.diging.vspace.core.exception.BlockDoesNotExistException; import edu.asu.diging.vspace.core.services.IContentBlockManager; @@ -24,9 +25,10 @@ public class DeleteChoiceBlockController { private IContentBlockManager contentBlockManager; @RequestMapping(value = "/staff/module/{moduleId}/slide/{id}/choiceBlock/{blockId}", method = RequestMethod.DELETE) - public ResponseEntity deleteChoiceBlock(@PathVariable("blockId") String blockId) throws IOException { + public ResponseEntity deleteChoiceBlock(@PathVariable("id") String slideId,@PathVariable("blockId") String blockId) throws IOException { + try { - contentBlockManager.deleteChoiceBlockById(blockId); + contentBlockManager.deleteChoiceBlockById(blockId,slideId); } catch (BlockDoesNotExistException e) { logger.warn("Choice Block Id does not exist, bad request.", e); return new ResponseEntity(HttpStatus.BAD_REQUEST); diff --git a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/DeleteImageBlockController.java b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/DeleteImageBlockController.java index dc629d12c..1830d450b 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/DeleteImageBlockController.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/DeleteImageBlockController.java @@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import edu.asu.diging.vspace.core.data.ContentBlockRepository; import edu.asu.diging.vspace.core.exception.BlockDoesNotExistException; import edu.asu.diging.vspace.core.services.IContentBlockManager; @@ -24,9 +25,10 @@ public class DeleteImageBlockController { private IContentBlockManager contentBlockManager; @RequestMapping(value = "/staff/module/{moduleId}/slide/{id}/image/{blockId}", method = RequestMethod.DELETE) - public ResponseEntity deleteImageBlock(@PathVariable("blockId") String blockId) throws IOException { + public ResponseEntity deleteImageBlock(@PathVariable("id") String slideId,@PathVariable("blockId") String blockId) throws IOException { + try { - contentBlockManager.deleteImageBlockById(blockId); + contentBlockManager.deleteImageBlockById(blockId,slideId); } catch (BlockDoesNotExistException e) { logger.warn("Image Id does not exist, bad request.", e); return new ResponseEntity(HttpStatus.BAD_REQUEST); diff --git a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/DeleteTextBlockController.java b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/DeleteTextBlockController.java index 344d73b13..cc1d0cca3 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/DeleteTextBlockController.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/DeleteTextBlockController.java @@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import edu.asu.diging.vspace.core.data.ContentBlockRepository; import edu.asu.diging.vspace.core.exception.BlockDoesNotExistException; import edu.asu.diging.vspace.core.services.IContentBlockManager; @@ -24,9 +25,11 @@ public class DeleteTextBlockController { private IContentBlockManager contentBlockManager; @RequestMapping(value = "/staff/module/{moduleId}/slide/{id}/text/{blockId}", method = RequestMethod.DELETE) - public ResponseEntity deleteTextBlock(@PathVariable("blockId") String blockId) throws IOException { + public ResponseEntity deleteTextBlock(@PathVariable("id") String slideId,@PathVariable("blockId") String blockId) throws IOException { + try { - contentBlockManager.deleteTextBlockById(blockId); + contentBlockManager.deleteTextBlockById(blockId,slideId); + } catch (BlockDoesNotExistException e) { logger.warn("Text Id does not exist, bad request.", e); return new ResponseEntity(HttpStatus.BAD_REQUEST); diff --git a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/EditImageBlockController.java b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/EditImageBlockController.java index 1f3487777..a4c5f07db 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/EditImageBlockController.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/EditImageBlockController.java @@ -29,8 +29,8 @@ public class EditImageBlockController { @RequestMapping(value = "/staff/module/{moduleId}/slide/{id}/image/{imageBlockId}", method = RequestMethod.POST) public ResponseEntity editImageBlock(@PathVariable("id") String slideId, @PathVariable("imageBlockId") String blockId, @PathVariable("moduleId") String moduleId, - @RequestParam("file") MultipartFile file, @RequestParam("contentOrder") Integer contentOrder, - Principal principal, RedirectAttributes attributes) throws IOException { + @RequestParam("file") MultipartFile file, Principal principal, RedirectAttributes attributes) + throws IOException { IImageBlock imageBlock = contentBlockManager.getImageBlock(blockId); byte[] image = null; @@ -40,7 +40,7 @@ public ResponseEntity editImageBlock(@PathVariable("id") String slideId, filename = file.getOriginalFilename(); } try { - contentBlockManager.updateImageBlock(imageBlock, image, filename, contentOrder); + contentBlockManager.updateImageBlock(imageBlock, image, filename); } catch (ImageCouldNotBeStoredException e) { ObjectMapper mapper = new ObjectMapper(); ObjectNode node = mapper.createObjectNode(); diff --git a/vspace/src/main/webapp/WEB-INF/views/staff/dashboard/dashboard.html b/vspace/src/main/webapp/WEB-INF/views/staff/dashboard/dashboard.html index ccc1c61f3..0552a7483 100644 --- a/vspace/src/main/webapp/WEB-INF/views/staff/dashboard/dashboard.html +++ b/vspace/src/main/webapp/WEB-INF/views/staff/dashboard/dashboard.html @@ -16,12 +16,12 @@

Spaces

Confirm
- +

diff --git a/vspace/src/test/java/edu/asu/diging/vspace/core/services/impl/ContentBlockManagerTest.java b/vspace/src/test/java/edu/asu/diging/vspace/core/services/impl/ContentBlockManagerTest.java index 4194ee216..a76ff9247 100644 --- a/vspace/src/test/java/edu/asu/diging/vspace/core/services/impl/ContentBlockManagerTest.java +++ b/vspace/src/test/java/edu/asu/diging/vspace/core/services/impl/ContentBlockManagerTest.java @@ -1,5 +1,13 @@ package edu.asu.diging.vspace.core.services.impl; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + import org.junit.Before; import org.junit.Test; import org.mockito.InjectMocks; @@ -8,65 +16,102 @@ import org.mockito.MockitoAnnotations; import org.springframework.dao.EmptyResultDataAccessException; +import edu.asu.diging.vspace.core.data.ContentBlockRepository; import edu.asu.diging.vspace.core.data.ImageContentBlockRepository; import edu.asu.diging.vspace.core.data.TextContentBlockRepository; import edu.asu.diging.vspace.core.exception.BlockDoesNotExistException; +import edu.asu.diging.vspace.core.model.impl.ContentBlock; public class ContentBlockManagerTest { + + @Mock + private ContentBlockRepository contentBlockRepository; + @Mock private TextContentBlockRepository textBlockRepo; @Mock private ImageContentBlockRepository imageBlockRepo; - + @InjectMocks private ContentBlockManager managerToTest; + private ContentBlock contentBlock; + + private List contentBlockList; + @Before public void setUp() { MockitoAnnotations.initMocks(this); - + contentBlock = new ContentBlock(); + contentBlock.setContentOrder(1); + + ContentBlock contentBlock1 = new ContentBlock(); + contentBlock1.setContentOrder(2); + ContentBlock contentBlock2 = new ContentBlock(); + contentBlock2.setContentOrder(3); + contentBlockList = new ArrayList<>(); + contentBlockList.add(contentBlock1); + contentBlockList.add(contentBlock2); } @Test public void test_deleteTextBlockById_success() throws BlockDoesNotExistException { - String textBlockId = "2"; - managerToTest.deleteTextBlockById(textBlockId); + String textBlockId = "textBlockId_2"; + Optional contentBlockOptional = Optional.of(contentBlock); + when(contentBlockRepository.findById("textBlockId_2")).thenReturn(contentBlockOptional); + when(contentBlockRepository.findBySlide_IdAndContentOrderGreaterThan("slideId_1",Integer.valueOf(1))).thenReturn(contentBlockList); + managerToTest.deleteTextBlockById(textBlockId,"slideId_1"); Mockito.verify(textBlockRepo).deleteById(textBlockId); + List contentOrderList = contentBlockList.stream().map(contentBlock -> contentBlock.getContentOrder()).collect(Collectors.toList()); + assertEquals(Integer.valueOf(1), contentOrderList.get(0)); + assertEquals(Integer.valueOf(2), contentOrderList.get(1)); + Mockito.verify(contentBlockRepository).saveAll(contentBlockList); } @Test(expected = BlockDoesNotExistException.class) public void test_deleteTextBlockById_forNonExistentId() throws BlockDoesNotExistException { String textBlockId = "notARealId"; + Optional contentBlockOptional = Optional.of(contentBlock); + when(contentBlockRepository.findById("notARealId")).thenReturn(contentBlockOptional); Mockito.doThrow(EmptyResultDataAccessException.class).when(textBlockRepo).deleteById(textBlockId); - managerToTest.deleteTextBlockById(textBlockId); + managerToTest.deleteTextBlockById(textBlockId,"slideId_1"); } @Test public void test_deleteTextBlockById_whenIdIsNull() throws BlockDoesNotExistException { String textBlockId = null; - managerToTest.deleteTextBlockById(null); + managerToTest.deleteTextBlockById(null,"slideId_1"); Mockito.verify(textBlockRepo, Mockito.never()).deleteById(textBlockId); } @Test public void test_deleteImageBlockById_success() throws BlockDoesNotExistException { - String imageBlockId = "2"; - managerToTest.deleteImageBlockById(imageBlockId); + String imageBlockId = "imageBlockId_2"; + Optional contentBlockOptional = Optional.of(contentBlock); + when(contentBlockRepository.findById("imageBlockId_2")).thenReturn(contentBlockOptional); + when(contentBlockRepository.findBySlide_IdAndContentOrderGreaterThan("slideId_1",Integer.valueOf(1))).thenReturn(contentBlockList); + managerToTest.deleteImageBlockById(imageBlockId,"slideId_1"); Mockito.verify(imageBlockRepo).deleteById(imageBlockId); + List contentOrderList = contentBlockList.stream().map(contentBlock -> contentBlock.getContentOrder()).collect(Collectors.toList()); + assertEquals(Integer.valueOf(1), contentOrderList.get(0)); + assertEquals(Integer.valueOf(2), contentOrderList.get(1)); + Mockito.verify(contentBlockRepository).saveAll(contentBlockList); } @Test(expected = BlockDoesNotExistException.class) public void test_deleteImageBlockById_forNonExistentId() throws BlockDoesNotExistException { String imageBlockId = "notARealId"; + Optional contentBlockOptional = Optional.of(contentBlock); + when(contentBlockRepository.findById("notARealId")).thenReturn(contentBlockOptional); Mockito.doThrow(EmptyResultDataAccessException.class).when(imageBlockRepo).deleteById(imageBlockId); - managerToTest.deleteImageBlockById(imageBlockId); + managerToTest.deleteImageBlockById(imageBlockId,"slideId_1"); } @Test public void test_deleteImagetBlockById_whenIdIsNull() throws BlockDoesNotExistException { String imageBlockId = null; - managerToTest.deleteImageBlockById(null); + managerToTest.deleteImageBlockById(null,"slideId_1"); Mockito.verify(imageBlockRepo, Mockito.never()).deleteById(imageBlockId); }