Skip to content

Commit

Permalink
Issue 1811: Improve Deposit Location controller test and add other co…
Browse files Browse the repository at this point in the history
…ntroller unit tests.

Add Deposit Location controller unit test for 'test-connection' end point.
Add non-exhaustive easy to implement additional controller tests to bump up the coverage.
  • Loading branch information
kaladay committed Jun 27, 2023
1 parent 6d2eff4 commit b5f8408
Show file tree
Hide file tree
Showing 16 changed files with 1,445 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package org.tdl.vireo.controller;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import edu.tamu.weaver.response.ApiResponse;
import edu.tamu.weaver.response.ApiStatus;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.context.ActiveProfiles;
import org.tdl.vireo.model.ControlledVocabulary;
import org.tdl.vireo.model.repo.ControlledVocabularyRepo;

@ActiveProfiles("test")
@ExtendWith(MockitoExtension.class)
public class ControlledVocabularyControllerTest extends AbstractControllerTest {

@Mock
private ControlledVocabularyRepo controlledVocabularyRepo;

@InjectMocks
private ControlledVocabularyController controlledVocabularyController;

private ControlledVocabulary mockControlledVocabulary1;
private ControlledVocabulary mockControlledVocabulary2;

private static List<ControlledVocabulary> mockControlledVocabularys;

@BeforeEach
public void setup() {
mockControlledVocabulary1 = new ControlledVocabulary("ControlledVocabulary 1");
mockControlledVocabulary1.setId(1L);

mockControlledVocabulary2 = new ControlledVocabulary("ControlledVocabulary 2");
mockControlledVocabulary2.setId(2L);

mockControlledVocabularys = new ArrayList<ControlledVocabulary>(Arrays.asList(new ControlledVocabulary[] { mockControlledVocabulary1 }));
}

@Test
public void testAllControlledVocabularys() {
when(controlledVocabularyRepo.findAllByOrderByPositionAsc()).thenReturn(mockControlledVocabularys);

ApiResponse response = controlledVocabularyController.getAllControlledVocabulary();
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

List<?> list = (ArrayList<?>) response.getPayload().get("ArrayList<ControlledVocabulary>");
assertEquals(mockControlledVocabularys.size(), list.size());
}

@Test
public void testGetControlledVocabulary() {
when(controlledVocabularyRepo.findByName(any(String.class))).thenReturn(mockControlledVocabulary1);

ApiResponse response = controlledVocabularyController.getControlledVocabularyByName("name");
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

ControlledVocabulary controlledVocabulary = (ControlledVocabulary) response.getPayload().get("ControlledVocabulary");
assertEquals(mockControlledVocabulary1.getId(), controlledVocabulary.getId());
}

@Test
public void testCreateControlledVocabulary() {
when(controlledVocabularyRepo.create(any(String.class))).thenReturn(mockControlledVocabulary2);

ApiResponse response = controlledVocabularyController.createControlledVocabulary(mockControlledVocabulary1);
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

ControlledVocabulary controlledVocabulary = (ControlledVocabulary) response.getPayload().get("ControlledVocabulary");
assertEquals(mockControlledVocabulary2.getId(), controlledVocabulary.getId());
}

@Test
public void testUpdateControlledVocabulary() {
when(controlledVocabularyRepo.findById(any(Long.class))).thenReturn(Optional.of(mockControlledVocabulary2));
when(controlledVocabularyRepo.update(any(ControlledVocabulary.class))).thenReturn(mockControlledVocabulary2);

ApiResponse response = controlledVocabularyController.updateControlledVocabulary(mockControlledVocabulary1);
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

ControlledVocabulary controlledVocabulary = (ControlledVocabulary) response.getPayload().get("ControlledVocabulary");
assertEquals(mockControlledVocabulary2.getId(), controlledVocabulary.getId());
}

@Test
public void testRemoveControlledVocabulary() {
doNothing().when(controlledVocabularyRepo).remove(any(ControlledVocabulary.class));

ApiResponse response = controlledVocabularyController.removeControlledVocabulary(mockControlledVocabulary1);
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

verify(controlledVocabularyRepo, times(1)).remove(any(ControlledVocabulary.class));
}

@Test
public void testReorderControlledVocabularys() {
doNothing().when(controlledVocabularyRepo).reorder(any(Long.class), any(Long.class));

ApiResponse response = controlledVocabularyController.reorderControlledVocabulary(1L, 2L);
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

verify(controlledVocabularyRepo, times(1)).reorder(any(Long.class), any(Long.class));
}

@Test
public void testSortControlledVocabularys() {
doNothing().when(controlledVocabularyRepo).sort(any(String.class));

ApiResponse response = controlledVocabularyController.sortControlledVocabulary("column");
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

verify(controlledVocabularyRepo, times(1)).sort(any(String.class));
}

}
104 changes: 104 additions & 0 deletions src/test/java/org/tdl/vireo/controller/CustomActionControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.tdl.vireo.controller;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import edu.tamu.weaver.response.ApiResponse;
import edu.tamu.weaver.response.ApiStatus;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.context.ActiveProfiles;
import org.tdl.vireo.model.CustomActionDefinition;
import org.tdl.vireo.model.repo.CustomActionDefinitionRepo;

@ActiveProfiles("test")
@ExtendWith(MockitoExtension.class)
public class CustomActionControllerTest extends AbstractControllerTest {

@Mock
private CustomActionDefinitionRepo customActionDefinitionRepo;

@InjectMocks
private CustomActionSettingsController customActionDefinitionController;

private CustomActionDefinition mockCustomActionDefinition1;
private CustomActionDefinition mockCustomActionDefinition2;

private static List<CustomActionDefinition> mockCustomActionDefinitions;

@BeforeEach
public void setup() {
mockCustomActionDefinition1 = new CustomActionDefinition("CustomActionDefinition 1", true);
mockCustomActionDefinition1.setId(1L);

mockCustomActionDefinition2 = new CustomActionDefinition("CustomActionDefinition 2", false);
mockCustomActionDefinition2.setId(2L);

mockCustomActionDefinitions = new ArrayList<CustomActionDefinition>(Arrays.asList(new CustomActionDefinition[] { mockCustomActionDefinition1 }));
}

@Test
public void testAllCustomActionDefinitions() {
when(customActionDefinitionRepo.findAllByOrderByPositionAsc()).thenReturn(mockCustomActionDefinitions);

ApiResponse response = customActionDefinitionController.getCustomActions();
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

List<?> list = (ArrayList<?>) response.getPayload().get("ArrayList<CustomActionDefinition>");
assertEquals(mockCustomActionDefinitions.size(), list.size());
}

@Test
public void testCreateCustomActionDefinition() {
when(customActionDefinitionRepo.create(any(String.class), any(Boolean.class))).thenReturn(mockCustomActionDefinition2);

ApiResponse response = customActionDefinitionController.createCustomAction(mockCustomActionDefinition1);
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

CustomActionDefinition customActionDefinition = (CustomActionDefinition) response.getPayload().get("CustomActionDefinition");
assertEquals(mockCustomActionDefinition2.getId(), customActionDefinition.getId());
}

@Test
public void testUpdateCustomActionDefinition() {
when(customActionDefinitionRepo.update(any(CustomActionDefinition.class))).thenReturn(mockCustomActionDefinition2);

ApiResponse response = customActionDefinitionController.updateCustomAction(mockCustomActionDefinition1);
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

CustomActionDefinition customActionDefinition = (CustomActionDefinition) response.getPayload().get("CustomActionDefinition");
assertEquals(mockCustomActionDefinition2.getId(), customActionDefinition.getId());
}

@Test
public void testRemoveCustomActionDefinition() {
doNothing().when(customActionDefinitionRepo).remove(any(CustomActionDefinition.class));

ApiResponse response = customActionDefinitionController.removeCustomAction(mockCustomActionDefinition1);
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

verify(customActionDefinitionRepo, times(1)).remove(any(CustomActionDefinition.class));
}

@Test
public void testReorderDegrees() {
doNothing().when(customActionDefinitionRepo).reorder(any(Long.class), any(Long.class));

ApiResponse response = customActionDefinitionController.reorderCustomActions(1L, 2L);
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

verify(customActionDefinitionRepo, times(1)).reorder(any(Long.class), any(Long.class));
}

}
147 changes: 147 additions & 0 deletions src/test/java/org/tdl/vireo/controller/DegreeControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package org.tdl.vireo.controller;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import edu.tamu.weaver.response.ApiResponse;
import edu.tamu.weaver.response.ApiStatus;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.context.ActiveProfiles;
import org.tdl.vireo.model.Degree;
import org.tdl.vireo.model.DegreeLevel;
import org.tdl.vireo.model.repo.DegreeRepo;
import org.tdl.vireo.service.ProquestCodesService;

@ActiveProfiles("test")
@ExtendWith(MockitoExtension.class)
public class DegreeControllerTest extends AbstractControllerTest {

@Mock
private DegreeRepo degreeRepo;

@Mock
private ProquestCodesService proquestCodesService;

@InjectMocks
private DegreeController degreeController;

private Degree mockDegree1;
private Degree mockDegree2;

private DegreeLevel mockDegreeLevel1;
private DegreeLevel mockDegreeLevel2;

private static List<Degree> mockDegrees;

@BeforeEach
public void setup() {
mockDegreeLevel1 = new DegreeLevel("1");
mockDegreeLevel1.setId(1L);

mockDegreeLevel2 = new DegreeLevel("2");
mockDegreeLevel2.setId(2L);

mockDegree1 = new Degree("Degree 1", mockDegreeLevel1, "Proquest 1");
mockDegree1.setId(1L);
mockDegree1.setPosition(1L);

mockDegree2 = new Degree("Degree 2", mockDegreeLevel2, "Proquest 2");
mockDegree2.setId(2L);
mockDegree2.setPosition(2L);

mockDegrees = new ArrayList<Degree>(Arrays.asList(new Degree[] { mockDegree1 }));
}

@Test
public void testAllDegrees() {
when(degreeRepo.findAllByOrderByPositionAsc()).thenReturn(mockDegrees);

ApiResponse response = degreeController.allDegrees();
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

List<?> list = (ArrayList<?>) response.getPayload().get("ArrayList<Degree>");
assertEquals(mockDegrees.size(), list.size());
}

@Test
public void testCreateDegree() {
when(degreeRepo.create(any(String.class), any(DegreeLevel.class))).thenReturn(mockDegree2);

ApiResponse response = degreeController.createDegree(mockDegree1);
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

Degree degree = (Degree) response.getPayload().get("Degree");
assertEquals(mockDegree2.getId(), degree.getId());
}

@Test
public void testUpdateDegree() {
when(degreeRepo.update(any(Degree.class))).thenReturn(mockDegree2);

ApiResponse response = degreeController.updateDegree(mockDegree1);
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

Degree degree = (Degree) response.getPayload().get("Degree");
assertEquals(mockDegree2.getId(), degree.getId());
}

@Test
public void testRemoveDegree() {
doNothing().when(degreeRepo).remove(any(Degree.class));

ApiResponse response = degreeController.removeDegree(mockDegree1);
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

verify(degreeRepo, times(1)).remove(any(Degree.class));
}

@Test
public void testReorderDegrees() {
doNothing().when(degreeRepo).reorder(any(Long.class), any(Long.class));

ApiResponse response = degreeController.reorderDegrees(1L, 2L);
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

verify(degreeRepo, times(1)).reorder(any(Long.class), any(Long.class));
}

@Test
public void testSortDegrees() {
doNothing().when(degreeRepo).sort(any(String.class));

ApiResponse response = degreeController.sortDegrees("column");
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

verify(degreeRepo, times(1)).sort(any(String.class));
}

@Test
public void testGetProquestLanguageCodes() {
Map<String, String> map = new HashMap<>();
map.put("a", "b");

when(proquestCodesService.getCodes(any(String.class))).thenReturn(map);

ApiResponse response = degreeController.getProquestLanguageCodes();
assertEquals(ApiStatus.SUCCESS, response.getMeta().getStatus());

@SuppressWarnings("unchecked")
Map<String, String> mapReturned = (HashMap<String, String>) response.getPayload().get("HashMap");
assertEquals(mapReturned.get("a"), map.get("a"));
}

}
Loading

0 comments on commit b5f8408

Please sign in to comment.