Exemplo n.º 1
0
  @Test
  public void editDocumentWithParams() throws DocumentNotFoundException {

    final DocumentReference documentReference =
        newDocumentReference("hello.txt")
            .withDocumentClass("test")
            .withIndex("name", "Silvio")
            .build();

    when(documentService.findDocumentReference(1L)).thenReturn(documentReference);
    when(documentService.updateIndices(documentReference)).thenReturn(documentReference);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setParameter("name", "Wangler");
    final ModelAndView modelAndView = controller.editDocument(1L, request);

    assertThat(modelAndView.getViewName(), is("import.successful"));
    assertThat(modelAndView.getModel().size(), is(1));
    assertThat(modelAndView.getModel().containsKey("doc"), is(true));
    final DocumentReference doc = (DocumentReference) modelAndView.getModel().get("doc");
    assertThat(doc, is(documentReference));
    assertThat(
        doc.getIndices().get(new TranslatableKey("name")).getValue().toString(), is("Wangler"));

    InOrder order = inOrder(documentService);

    order.verify(documentService).findDocumentReference(1L);
    order.verify(documentService).updateIndices(documentReference);
    order.verifyNoMoreInteractions();
  }
Exemplo n.º 2
0
  @Test
  @SuppressWarnings("unchecked")
  public void edit() throws Exception {

    final DocumentClass documentClass = new DocumentClass("hello");
    final DocumentReference documentReference =
        newDocumentReference("hello.txt").withDocumentClass(documentClass).build();

    when(documentService.findDocumentReference(1L)).thenReturn(documentReference);
    SortedSet<Attribute> attributes = new TreeSet<>();
    attributes.add(new Attribute("a", false, AttributeDataType.CURRENCY));
    attributes.add(new Attribute("b", false, AttributeDataType.STRING));
    when(documentService.findAttributes(documentClass)).thenReturn(attributes);

    final ModelAndView modelAndView = controller.editDocument(1L);

    assertThat(modelAndView.getViewName(), is("edit.doc"));
    assertThat(modelAndView.getModel().size(), is(2));
    assertThat(modelAndView.getModel().containsKey("doc"), is(true));
    assertThat((DocumentReference) modelAndView.getModel().get("doc"), is(documentReference));
    assertThat(modelAndView.getModel().containsKey("attributes"), is(true));
    assertThat(((SortedSet<Attribute>) modelAndView.getModel().get("attributes")).size(), is(2));

    InOrder order = inOrder(documentService);
    order.verify(documentService).findDocumentReference(1L);
    order.verify(documentService).findAttributes(documentReference.getDocumentClass());
    order.verifyNoMoreInteractions();
  }
Exemplo n.º 3
0
  @Test
  public void editDocumentWhenThrowsException() throws Exception {

    when(documentService.findDocumentReference(1L)).thenThrow(new DocumentNotFoundException(1L));

    final ModelAndView modelAndView = controller.editDocument(1L, new MockHttpServletRequest());

    assertThat(modelAndView.getViewName(), is("modification.doc.failed"));
    assertThat(modelAndView.getModel().isEmpty(), is(true));
  }
Exemplo n.º 4
0
  @Test
  public void editWhenThrowsException() throws Exception {

    when(documentService.findDocumentReference(1L)).thenThrow(new DocumentNotFoundException(1L));

    final ModelAndView modelAndView = controller.editDocument(1L);

    assertThat(modelAndView.getViewName(), is("edit.doc"));
    assertThat(modelAndView.getModel().isEmpty(), is(true));
  }
Exemplo n.º 5
0
  @Test
  public void getTheDocumentContentExpectNotFound()
      throws DocumentNotFoundException, DocumentNotInStoreException, UnsupportedEncodingException {

    MockHttpServletResponse response = new MockHttpServletResponse();
    long expectedDocumentId = 2L;
    when(documentService.findPhysicalDocument(expectedDocumentId))
        .thenThrow(new DocumentNotFoundException(expectedDocumentId));
    controller.getDocument(expectedDocumentId, response);
    assertThat(response.getStatus(), is(SC_NOT_FOUND));
  }
Exemplo n.º 6
0
  @Test
  public void getTheDocumentContentExpectNotFound3()
      throws DocumentNotFoundException, DocumentNotInStoreException, IOException {

    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getOutputStream()).thenThrow(new IOException());
    long expectedDocumentId = 2L;
    when(documentService.findPhysicalDocument(expectedDocumentId))
        .thenReturn(new PhysicalDocument());
    controller.getDocument(expectedDocumentId, response);

    verify(response).setStatus(SC_INTERNAL_SERVER_ERROR);
  }
Exemplo n.º 7
0
  @Test
  public void getTheDocumentContentCausesAccessDenied()
      throws DocumentNotFoundException, DocumentNotInStoreException, UnsupportedEncodingException {

    MockHttpServletResponse response = new MockHttpServletResponse();

    PhysicalDocument physicalDocument =
        new PhysicalDocument(new DocumentClass("hhh"), "hello".getBytes(), null, "hello.txt");
    physicalDocument.setMimeType("aaa/bbb");
    when(documentService.findPhysicalDocument(1L)).thenThrow(new AccessDeniedException("YOLO"));

    controller.getDocument(1L, response);

    assertThat(response.getStatus(), is(SC_FORBIDDEN));
  }
Exemplo n.º 8
0
  @Test
  public void getTheDocumentContent()
      throws DocumentNotFoundException, DocumentNotInStoreException, UnsupportedEncodingException {

    MockHttpServletResponse response = new MockHttpServletResponse();

    PhysicalDocument physicalDocument =
        new PhysicalDocument(new DocumentClass("hhh"), "hello".getBytes(), null, "hello.txt");
    physicalDocument.setMimeType("aaa/bbb");
    when(documentService.findPhysicalDocument(1L)).thenReturn(physicalDocument);

    controller.getDocument(1L, response);

    assertThat(response.getStatus(), is(SC_OK));
    assertThat(response.getContentAsString(), is("hello"));
    assertThat(response.getContentType(), is("aaa/bbb"));
    assertThat(response.getHeader("Content-Disposition"), is("inline; filename=\"hello.txt\""));
  }
Exemplo n.º 9
0
  @Test
  public void editDocument() throws DocumentNotFoundException {

    final DocumentReference documentReference =
        newDocumentReference("hello.txt").withDocumentClass("test").build();

    when(documentService.findDocumentReference(1L)).thenReturn(documentReference);

    final ModelAndView modelAndView = controller.editDocument(1L, new MockHttpServletRequest());

    assertThat(modelAndView.getViewName(), is("import.successful"));
    assertThat(modelAndView.getModel().size(), is(1));
    assertThat(modelAndView.getModel().containsKey("doc"), is(true));
    assertThat((DocumentReference) modelAndView.getModel().get("doc"), is(documentReference));

    InOrder order = inOrder(documentService);

    order.verify(documentService).findDocumentReference(1L);
    order.verifyNoMoreInteractions();
  }