<commit_before>/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2013 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
 * You should have received a copy of the GNU Affero General Public License along with this program.  If not, see http://www.gnu.org/licenses.  For additional information contact <EMAIL>. 
 */

package org.openlmis.web.controller;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.openlmis.authentication.web.UserAuthenticationSuccessHandler;
import org.openlmis.core.domain.OrderConfiguration;
import org.openlmis.core.domain.Right;
import org.openlmis.core.exception.DataException;
import org.openlmis.db.categories.UnitTests;
import org.openlmis.order.domain.DateFormat;
import org.openlmis.order.domain.Order;
import org.openlmis.order.domain.OrderFileColumn;
import org.openlmis.order.dto.OrderDTO;
import org.openlmis.order.dto.OrderFileTemplateDTO;
import org.openlmis.order.service.OrderService;
import org.openlmis.web.form.RequisitionList;
import org.openlmis.web.response.OpenLmisResponse;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.openlmis.core.domain.Right.MANAGE_POD;
import static org.openlmis.order.domain.OrderStatus.*;
import static org.openlmis.web.controller.OrderController.*;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.springframework.http.HttpStatus.CONFLICT;
import static org.springframework.http.HttpStatus.CREATED;

@RunWith(PowerMockRunner.class)
@PrepareForTest(OrderDTO.class)
@Category(UnitTests.class)
public class OrderControllerTest {

  private static final Long USER_ID = 1L;
  private MockHttpServletRequest request;
  private static final String USER = "user";

  @Mock
  private OrderService orderService;

  @SuppressWarnings("unused")
  @InjectMocks
  private OrderController orderController;

  @Before
  public void setUp() throws Exception {
    request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    session.setAttribute(UserAuthenticationSuccessHandler.USER, USER);
    session.setAttribute(UserAuthenticationSuccessHandler.USER_ID, USER_ID);

    request.setSession(session);
  }

  @Test
  public void shouldConvertRequisitionsToOrder() throws Exception {
    RequisitionList rnrList = new RequisitionList();
    ResponseEntity<OpenLmisResponse> response = orderController.convertToOrder(rnrList, request);
    assertThat(response.getStatusCode(), is(CREATED));
    verify(orderService).convertToOrder(rnrList, 1L);
  }

  @Test
  public void shouldReturnErrorMsgIfSomeRequisitionsAreAlreadyConverted() {
    RequisitionList rnrList = new RequisitionList();
    doThrow(new DataException("msg.rnr.already.converted.to.order")).
      when(orderService).convertToOrder(rnrList, 1L);
    ResponseEntity<OpenLmisResponse> response = orderController.convertToOrder(rnrList, request);
    assertThat(response.getStatusCode(), is(CONFLICT));
    assertThat(response.getBody().getErrorMsg(), is("msg.rnr.already.converted.to.order"));
    verify(orderService).convertToOrder(rnrList, 1L);
  }

  @Test
  public void shouldReturnAllOrdersForPage() throws Exception {
    List<Order> orders = new ArrayList<Order>() {{
      new Order();
    }};

    mockStatic(OrderDTO.class);
    when(orderService.getOrdersForPage(2, USER_ID, Right.VIEW_ORDER)).thenReturn(orders);
    List<OrderDTO> orderDTOs = new ArrayList<>();
    when(OrderDTO.getOrdersForView(orders)).thenReturn(orderDTOs);

    ResponseEntity<OpenLmisResponse> fetchedOrders = orderController.getOrdersForPage(2, request);

    verify(orderService).getOrdersForPage(2, USER_ID, Right.VIEW_ORDER);
    assertThat((List<OrderDTO>) fetchedOrders.getBody().getData().get(ORDERS), is(orderDTOs));
  }

  @Test
  public void shouldAddPageInfoForOrders() throws Exception {
    when(orderService.getPageSize()).thenReturn(3);

    ResponseEntity<OpenLmisResponse> fetchedOrders = orderController.getOrdersForPage(2, request);

    assertThat((Integer) fetchedOrders.getBody().getData().get("pageSize"), is(3));
  }

  @Test
  public void shouldAddTotalNumberOfPagesForOrders() throws Exception {
    when(orderService.getNumberOfPages()).thenReturn(5);

    ResponseEntity<OpenLmisResponse> fetchedOrders = orderController.getOrdersForPage(2, request);

    assertThat((Integer) fetchedOrders.getBody().getData().get("numberOfPages"), is(5));
  }

  @Test
  public void shouldDownloadOrderCsv() {
    Long orderId = 1L;
    Order expectedOrder = new Order();
    when(orderService.getOrder(orderId)).thenReturn(expectedOrder);
    OrderFileTemplateDTO expectedOrderFileTemplateDTO = new OrderFileTemplateDTO(new OrderConfiguration(),
      new ArrayList<OrderFileColumn>());
    when(orderService.getOrderFileTemplateDTO()).thenReturn(expectedOrderFileTemplateDTO);
    ModelAndView modelAndView = orderController.downloadOrderCsv(orderId);
    Order order = (Order) modelAndView.getModel().get(ORDER);
    OrderFileTemplateDTO orderFileTemplate = (OrderFileTemplateDTO) modelAndView.getModel().get(ORDER_FILE_TEMPLATE);
    assertThat(order, is(expectedOrder));
    assertThat(orderFileTemplate, is(expectedOrderFileTemplateDTO));
    verify(orderService).getOrder(orderId);
    verify(orderService).getOrderFileTemplateDTO();
  }

  @Test
  public void shouldGetOrderFileTemplateDTO() throws Exception {
    OrderFileTemplateDTO expectedOrderFileTemplateDTO = new OrderFileTemplateDTO(new OrderConfiguration(),
      new ArrayList<OrderFileColumn>());
    when(orderService.getOrderFileTemplateDTO()).thenReturn(expectedOrderFileTemplateDTO);
    ResponseEntity<OpenLmisResponse> fetchedTemplate = orderController.getOrderFileTemplateDTO();
    verify(orderService).getOrderFileTemplateDTO();
    assertThat((OrderFileTemplateDTO) fetchedTemplate.getBody().getData().get(ORDER_FILE_TEMPLATE),
      is(expectedOrderFileTemplateDTO));
  }

  @Test
  public void shouldSaveOrderFileTemplateDTO() throws Exception {
    OrderFileTemplateDTO orderFileTemplateDTO = new OrderFileTemplateDTO(new OrderConfiguration(),
      new ArrayList<OrderFileColumn>());
    ResponseEntity<OpenLmisResponse> response = orderController.saveOrderFileTemplateDTO(orderFileTemplateDTO, request);
    verify(orderService).saveOrderFileTemplate(orderFileTemplateDTO, 1L);
    assertThat(response.getBody().getSuccessMsg(), is("order.file.template.saved.success"));
  }

  @Test
  public void shouldGetAllDateFormats() throws Exception {
    Set<DateFormat> dateFormats = new HashSet<>();
    when(orderService.getAllDateFormats()).thenReturn(dateFormats);
    ResponseEntity<OpenLmisResponse> response = orderController.getAllDateFormats();
    verify(orderService).getAllDateFormats();
    assertThat((Set<DateFormat>) response.getBody().getData().get(DATE_FORMATS), is(dateFormats));
  }

  @Test
  public void shouldGetAllOrdersForPOD() {
    List<Order> ordersForPOD = null;
    when(orderService.searchByStatusAndRight(USER_ID,
      MANAGE_POD,
      asList(RELEASED, PACKED, TRANSFER_FAILED, READY_TO_PACK))).thenReturn(ordersForPOD);
    mockStatic(OrderDTO.class);
    List<OrderDTO> orderDTOs = new ArrayList<>();
    when(OrderDTO.getOrdersForView(ordersForPOD)).thenReturn(orderDTOs);

    ResponseEntity<OpenLmisResponse> response = orderController.getOrdersForPOD(request);

    assertThat((List<OrderDTO>) response.getBody().getData().get(ORDERS_FOR_POD), is(orderDTOs));
    verify(orderService).searchByStatusAndRight(USER_ID,
      MANAGE_POD,
      asList(RELEASED, PACKED, TRANSFER_FAILED, READY_TO_PACK));
  }
}
<commit_msg>Fix failing Tests for order controller<commit_after>/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2013 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
 * You should have received a copy of the GNU Affero General Public License along with this program.  If not, see http://www.gnu.org/licenses.  For additional information contact <EMAIL>. 
 */

package org.openlmis.web.controller;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.openlmis.authentication.web.UserAuthenticationSuccessHandler;
import org.openlmis.core.domain.OrderConfiguration;
import org.openlmis.core.domain.Right;
import org.openlmis.core.exception.DataException;
import org.openlmis.db.categories.UnitTests;
import org.openlmis.order.domain.DateFormat;
import org.openlmis.order.domain.Order;
import org.openlmis.order.domain.OrderFileColumn;
import org.openlmis.order.dto.OrderDTO;
import org.openlmis.order.dto.OrderFileTemplateDTO;
import org.openlmis.order.service.OrderService;
import org.openlmis.web.form.RequisitionList;
import org.openlmis.web.response.OpenLmisResponse;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.openlmis.core.domain.Right.MANAGE_POD;
import static org.openlmis.order.domain.OrderStatus.*;
import static org.openlmis.web.controller.OrderController.*;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.springframework.http.HttpStatus.CONFLICT;
import static org.springframework.http.HttpStatus.CREATED;

@RunWith(PowerMockRunner.class)
@PrepareForTest(OrderDTO.class)
@Category(UnitTests.class)
public class OrderControllerTest {

  private static final Long USER_ID = 1L;
  private MockHttpServletRequest request;
  private static final String USER = "user";

  @Mock
  private OrderService orderService;

  @SuppressWarnings("unused")
  @InjectMocks
  private OrderController orderController;

  @Before
  public void setUp() throws Exception {
    request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    session.setAttribute(UserAuthenticationSuccessHandler.USER, USER);
    session.setAttribute(UserAuthenticationSuccessHandler.USER_ID, USER_ID);

    request.setSession(session);
  }

  @Test
  public void shouldConvertRequisitionsToOrder() throws Exception {
    RequisitionList rnrList = new RequisitionList();
    ResponseEntity<OpenLmisResponse> response = orderController.convertToOrder(rnrList, request);
    assertThat(response.getStatusCode(), is(CREATED));
    verify(orderService).convertToOrder(rnrList, 1L);
  }

  @Test
  public void shouldReturnErrorMsgIfSomeRequisitionsAreAlreadyConverted() {
    RequisitionList rnrList = new RequisitionList();
    doThrow(new DataException("msg.rnr.already.converted.to.order")).
      when(orderService).convertToOrder(rnrList, 1L);
    ResponseEntity<OpenLmisResponse> response = orderController.convertToOrder(rnrList, request);
    assertThat(response.getStatusCode(), is(CONFLICT));
    assertThat(response.getBody().getErrorMsg(), is("msg.rnr.already.converted.to.order"));
    verify(orderService).convertToOrder(rnrList, 1L);
  }

  @Test
  public void shouldReturnAllOrdersForPage() throws Exception {
    List<Order> orders = new ArrayList<Order>() {{
      new Order();
    }};

    mockStatic(OrderDTO.class);
    when(orderService.getOrdersForPage(2, USER_ID, Right.VIEW_ORDER)).thenReturn(orders);
    List<OrderDTO> orderDTOs = new ArrayList<>();
    when(OrderDTO.getOrdersForView(orders)).thenReturn(orderDTOs);

    ResponseEntity<OpenLmisResponse> fetchedOrders = orderController.getOrdersForPage(2,"","", request);

    verify(orderService).getOrdersForPage(2, USER_ID, Right.VIEW_ORDER);
    assertThat((List<OrderDTO>) fetchedOrders.getBody().getData().get(ORDERS), is(orderDTOs));
  }

  @Test
  public void shouldAddPageInfoForOrders() throws Exception {
    when(orderService.getPageSize()).thenReturn(3);

    ResponseEntity<OpenLmisResponse> fetchedOrders = orderController.getOrdersForPage(2,"","", request);

    assertThat((Integer) fetchedOrders.getBody().getData().get("pageSize"), is(3));
  }

  @Test
  public void shouldAddTotalNumberOfPagesForOrders() throws Exception {
    when(orderService.getNumberOfPages()).thenReturn(5);

    ResponseEntity<OpenLmisResponse> fetchedOrders = orderController.getOrdersForPage(2,"","", request);

    assertThat((Integer) fetchedOrders.getBody().getData().get("numberOfPages"), is(5));
  }

  @Test
  public void shouldDownloadOrderCsv() {
    Long orderId = 1L;
    Order expectedOrder = new Order();
    when(orderService.getOrder(orderId)).thenReturn(expectedOrder);
    OrderFileTemplateDTO expectedOrderFileTemplateDTO = new OrderFileTemplateDTO(new OrderConfiguration(),
      new ArrayList<OrderFileColumn>());
    when(orderService.getOrderFileTemplateDTO()).thenReturn(expectedOrderFileTemplateDTO);
    ModelAndView modelAndView = orderController.downloadOrderCsv(orderId);
    Order order = (Order) modelAndView.getModel().get(ORDER);
    OrderFileTemplateDTO orderFileTemplate = (OrderFileTemplateDTO) modelAndView.getModel().get(ORDER_FILE_TEMPLATE);
    assertThat(order, is(expectedOrder));
    assertThat(orderFileTemplate, is(expectedOrderFileTemplateDTO));
    verify(orderService).getOrder(orderId);
    verify(orderService).getOrderFileTemplateDTO();
  }

  @Test
  public void shouldGetOrderFileTemplateDTO() throws Exception {
    OrderFileTemplateDTO expectedOrderFileTemplateDTO = new OrderFileTemplateDTO(new OrderConfiguration(),
      new ArrayList<OrderFileColumn>());
    when(orderService.getOrderFileTemplateDTO()).thenReturn(expectedOrderFileTemplateDTO);
    ResponseEntity<OpenLmisResponse> fetchedTemplate = orderController.getOrderFileTemplateDTO();
    verify(orderService).getOrderFileTemplateDTO();
    assertThat((OrderFileTemplateDTO) fetchedTemplate.getBody().getData().get(ORDER_FILE_TEMPLATE),
      is(expectedOrderFileTemplateDTO));
  }

  @Test
  public void shouldSaveOrderFileTemplateDTO() throws Exception {
    OrderFileTemplateDTO orderFileTemplateDTO = new OrderFileTemplateDTO(new OrderConfiguration(),
      new ArrayList<OrderFileColumn>());
    ResponseEntity<OpenLmisResponse> response = orderController.saveOrderFileTemplateDTO(orderFileTemplateDTO, request);
    verify(orderService).saveOrderFileTemplate(orderFileTemplateDTO, 1L);
    assertThat(response.getBody().getSuccessMsg(), is("order.file.template.saved.success"));
  }

  @Test
  public void shouldGetAllDateFormats() throws Exception {
    Set<DateFormat> dateFormats = new HashSet<>();
    when(orderService.getAllDateFormats()).thenReturn(dateFormats);
    ResponseEntity<OpenLmisResponse> response = orderController.getAllDateFormats();
    verify(orderService).getAllDateFormats();
    assertThat((Set<DateFormat>) response.getBody().getData().get(DATE_FORMATS), is(dateFormats));
  }

  @Test
  public void shouldGetAllOrdersForPOD() {
    List<Order> ordersForPOD = null;
    when(orderService.searchByStatusAndRight(USER_ID,
      MANAGE_POD,
      asList(RELEASED, PACKED, TRANSFER_FAILED, READY_TO_PACK))).thenReturn(ordersForPOD);
    mockStatic(OrderDTO.class);
    List<OrderDTO> orderDTOs = new ArrayList<>();
    when(OrderDTO.getOrdersForView(ordersForPOD)).thenReturn(orderDTOs);

    ResponseEntity<OpenLmisResponse> response = orderController.getOrdersForPOD(request);

    assertThat((List<OrderDTO>) response.getBody().getData().get(ORDERS_FOR_POD), is(orderDTOs));
    verify(orderService).searchByStatusAndRight(USER_ID,
      MANAGE_POD,
      asList(RELEASED, PACKED, TRANSFER_FAILED, READY_TO_PACK));
  }
}

{'fraction_non_alphanumeric': 0.10197091358301982, 'fraction_numerical': 0.0023583581335279913, 'mean_word_length': 6.056259904912837, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6820603', 'n_tokens_mistral': 5146, 'n_tokens_neox': 5183, 'n_words': 997}