Summary Table

Categories Total Count
PII 0
URL 0
DNS 0
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 0
VPD 0
PL 0
Other 0

File Content

package gov.va.med.ars.controller;

import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import gov.va.med.ars.constants.ErrorMessages;
import gov.va.med.ars.exceptions.GenericException;
import gov.va.med.ars.filter.CORSFilter;
import gov.va.med.ars.model.response.MetaDataResponse;
import gov.va.med.ars.service.IDashboardService;

@RunWith(MockitoJUnitRunner.class)
public class DashboardControllerTest {
private MockMvc mockMvc;
public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));

@Mock
private IDashboardService dashboardService;
@Mock
private GenericException genericException = new GenericException(ErrorMessages.INVALID_REQUEST,
"At least one field information needs to be added", HttpStatus.NOT_FOUND);

@InjectMocks
private DashboardController dashboardController;

@Before
public void init() throws Exception {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(dashboardController).addFilters(new CORSFilter()).build();
}

@Test
public void testgetAttachmentStorageInfo() throws Exception {

List<BigDecimal> usedStorageStatistic = new ArrayList<>();
List<BigDecimal> totalStorageStatistic = new ArrayList<>();
usedStorageStatistic.add(new BigDecimal(0));
usedStorageStatistic.add(new BigDecimal(32));
usedStorageStatistic.add(new BigDecimal(121));
usedStorageStatistic.add(new BigDecimal(120));
usedStorageStatistic.add(new BigDecimal(121));
usedStorageStatistic.add(new BigDecimal(121));
usedStorageStatistic.add(new BigDecimal(121));

totalStorageStatistic.add(new BigDecimal(0));
totalStorageStatistic.add(new BigDecimal(222));
totalStorageStatistic.add(new BigDecimal(141));
totalStorageStatistic.add(new BigDecimal(321));
totalStorageStatistic.add(new BigDecimal(232));
totalStorageStatistic.add(new BigDecimal(220));
totalStorageStatistic.add(new BigDecimal(121));

MetaDataResponse response = new MetaDataResponse();
response.setTotalStorage(new BigDecimal(232));
response.setAvailableStorage(new BigDecimal(200));
response.setUsedStorage(new BigDecimal(32));
response.setAverage(new BigDecimal(300));
response.setFromDate("03282018");
response.setToDate("04032018");
response.setTotalStorageStatistic(totalStorageStatistic);
response.setUsedStorageStatistic(usedStorageStatistic);
response.setTotalAttachments(new BigDecimal(232));
response.setAverageAttachmentSize(new BigDecimal(300));

when(dashboardService.getAttachmentsDetails()).thenReturn(response);

mockMvc.perform(get("/api/v1/dashboard/attachmentDetails").contentType(MediaType.APPLICATION_JSON))
.andDo(print()).andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.totalStorage", is(232))).andExpect(jsonPath("$.availableStorage", is(200)))
.andExpect(jsonPath("$.usedStorage", is(32))).andExpect(jsonPath("$.average", is(300)))
.andExpect(jsonPath("$.totalAttachments", is(232)));

verify(dashboardService, times(1)).getAttachmentsDetails();
verifyNoMoreInteractions(dashboardService);
}

@Test
public void testMetadataStorageInfo() throws Exception {

List<BigDecimal> usedStorageStatistic = new ArrayList<>();
List<BigDecimal> totalStorageStatistic = new ArrayList<>();
usedStorageStatistic.add(new BigDecimal(0));
usedStorageStatistic.add(new BigDecimal(32));
usedStorageStatistic.add(new BigDecimal(121));
usedStorageStatistic.add(new BigDecimal(120));
usedStorageStatistic.add(new BigDecimal(121));
usedStorageStatistic.add(new BigDecimal(121));
usedStorageStatistic.add(new BigDecimal(121));

totalStorageStatistic.add(new BigDecimal(0));
totalStorageStatistic.add(new BigDecimal(222));
totalStorageStatistic.add(new BigDecimal(141));
totalStorageStatistic.add(new BigDecimal(321));
totalStorageStatistic.add(new BigDecimal(232));
totalStorageStatistic.add(new BigDecimal(220));
totalStorageStatistic.add(new BigDecimal(121));

MetaDataResponse response = new MetaDataResponse();
response.setTotalStorage(new BigDecimal(232));
response.setAvailableStorage(new BigDecimal(200));
response.setUsedStorage(new BigDecimal(32));
response.setAverage(new BigDecimal(300));
response.setFromDate("03282018");
response.setToDate("04032018");
response.setTotalStorageStatistic(totalStorageStatistic);
response.setUsedStorageStatistic(usedStorageStatistic);
response.setTotalAttachments(new BigDecimal(232));
response.setAverageAttachmentSize(new BigDecimal(300));

when(dashboardService.getMetaDataDetails()).thenReturn(response);

mockMvc.perform(get("/api/v1/dashboard/metaDataDetails").contentType(MediaType.APPLICATION_JSON)).andDo(print())
.andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.totalStorage", is(232))).andExpect(jsonPath("$.availableStorage", is(200)))
.andExpect(jsonPath("$.usedStorage", is(32))).andExpect(jsonPath("$.average", is(300)))
.andExpect(jsonPath("$.totalAttachments", is(232)));

verify(dashboardService, times(1)).getMetaDataDetails();
verifyNoMoreInteractions(dashboardService);
}
}