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.oneconsult.seoc.api.controller;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.HashSet;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import gov.va.oneconsult.seoc.api.Application;
import gov.va.oneconsult.seoc.api.model.CategoryOfCare;
import gov.va.oneconsult.seoc.api.model.Hptc;
import gov.va.oneconsult.seoc.api.model.ServiceLine;
import gov.va.oneconsult.seoc.api.service.GenericService;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = Application.class)
public class LookupControllerTest
{

private MockMvc mockMvc;

@Mock
private GenericService genericService;

@InjectMocks
private LookUpController lookUpController;

public static final String ROOT = "/v1/lookup";

@Before
public void setup()
{

MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(lookUpController).build();
}

@Test
public void testCOCForhtmlEncoding() throws Exception
{

Set<CategoryOfCare> cocSet = new HashSet<CategoryOfCare>();
CategoryOfCare coc = SeocObjectGenerator.getCategoryOfCare(1, "<script>coc</script>");
ServiceLine sl = SeocObjectGenerator.getServiceLine(1, "<script>sl</script>","SL");
coc.setServiceLine(sl);
cocSet.add(coc);
sl.setCocs(cocSet);
Mockito.when(genericService.getAllCategoryOfCare()).thenReturn(cocSet);
RequestBuilder request = MockMvcRequestBuilders.get(ROOT + "/categoryofcare" )
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON_UTF8);

MvcResult result = mockMvc.perform(request)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content()
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andReturn();

assertThat(result.getResponse().getContentAsString()).doesNotContain("<");
Mockito.verify(genericService, Mockito.times(1)).getAllCategoryOfCare();
Mockito.verifyNoMoreInteractions(genericService);
}

@Test
public void testServiceLineHtmlEncoding() throws Exception
{

Set<CategoryOfCare> cocSet = new HashSet<CategoryOfCare>();
CategoryOfCare coc = SeocObjectGenerator.getCategoryOfCare(1, "<script>coc</script>");
ServiceLine sl = SeocObjectGenerator.getServiceLine(1, "<script>sl</script>","SL");
coc.setServiceLine(sl);
cocSet.add(coc);
sl.setCocs(cocSet);
Set<ServiceLine> slSet = new HashSet<ServiceLine>();
slSet.add(sl);

Mockito.when(genericService.getAllServiceLine()).thenReturn(slSet);
RequestBuilder request = MockMvcRequestBuilders.get(ROOT + "/serviceline" )
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON_UTF8);

MvcResult result = mockMvc.perform(request)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content()
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andReturn();

assertThat(result.getResponse().getContentAsString()).doesNotContain("<");
Mockito.verify(genericService, Mockito.times(1)).getAllServiceLine();
Mockito.verifyNoMoreInteractions(genericService);
}

@Test
public void testServiceLineByDescHtmlEncoding() throws Exception
{

ServiceLine sl1 = SeocObjectGenerator.getServiceLine(1, "\"This is a <p>special charcater</p> <script>test</script> =, ~,', @, ^, #, &, $, *, $, %, -, (, ), + \"","SL1");
CategoryOfCare coc = SeocObjectGenerator.getCategoryOfCare(10, "<script>coc</script>");
Set<CategoryOfCare> cocSet = new HashSet<CategoryOfCare>();
coc.setServiceLine(sl1);
cocSet.add(coc);
sl1.setCocs(cocSet);

ServiceLine sl2 = SeocObjectGenerator.getServiceLine(1, "<script>sl2</script>","SL2");
CategoryOfCare coc1 = SeocObjectGenerator.getCategoryOfCare(1, "<script>coc1</script>");
CategoryOfCare coc2 = SeocObjectGenerator.getCategoryOfCare(2, "<script>coc2</script>");
coc1.setServiceLine(sl2);
coc2.setServiceLine(sl2);
cocSet = new HashSet<CategoryOfCare>();
cocSet.add(coc1);
cocSet.add(coc2);
sl2.setCocs(cocSet);

Set<ServiceLine> slSet = new HashSet<ServiceLine>();
slSet.add(sl1);
slSet.add(sl2);

Mockito.when(genericService.getServiceLineByDescription("sl")).thenReturn(slSet);
RequestBuilder request = MockMvcRequestBuilders.get(ROOT + "/serviceline/sl" )
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON_UTF8);

MvcResult result = mockMvc.perform(request)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content()
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andReturn();

assertThat(result.getResponse().getContentAsString()).doesNotContain("<");
Mockito.verify(genericService, Mockito.times(1)).getServiceLineByDescription(Mockito.anyString());
Mockito.verifyNoMoreInteractions(genericService);
}

@Test
public void testGetAllHptcEndPointReturnsData() throws Exception
{
RequestBuilder request = MockMvcRequestBuilders.get(ROOT + "/hptc" )
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON_UTF8);

Hptc hptc1 = SeocObjectGenerator.getHptc("hptcCode1", "classification1", "spec1", "grouping1");
Hptc hptc2 = SeocObjectGenerator.getHptc("hptcCode2", "classification2", "spec2", "grouping2");

Set<Hptc> hptcExpected = new HashSet<Hptc>();
hptcExpected.add(hptc1);
hptcExpected.add(hptc2);

String hptcJsonExpected = SeocObjectGenerator.asJsonString(hptcExpected);

Mockito.when(genericService.getAllHptc()).thenReturn(hptcExpected);

MvcResult result = mockMvc.perform(request)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content()
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andReturn();

assertThat(result.getResponse().getContentAsString()).isEqualTo(hptcJsonExpected);

Mockito.verify(genericService, Mockito.times(1)).getAllHptc();
Mockito.verifyNoMoreInteractions(genericService);
}
}