Skip to content

Commit

Permalink
Add location tag recourse tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rkareko committed May 11, 2023
1 parent 2329a37 commit 88cc6be
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/test/java/org/opensrp/web/rest/LocationTagResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.mockito.Captor;
import org.opensrp.service.LocationTagService;
import org.smartregister.domain.LocationTag;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.test.web.server.result.MockMvcResultMatchers;

import java.util.ArrayList;
Expand All @@ -23,6 +24,8 @@
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.doThrow;

public class LocationTagResourceTest extends BaseResourceTest<LocationTag> {

Expand Down Expand Up @@ -94,6 +97,49 @@ public void testShouldCreateNewLocationTagResource() throws Exception {

}

@Test
public void testCreateNewLocationTagDoesNotAddLocationTagWhenJsonProcessingExceptionIsThrown() throws Exception {

postRequestWithJsonContentAndReturnString(BASE_URL, "{\"ids\":\"0\"}", MockMvcResultMatchers.status().isBadRequest());

verifyNoInteractions(locationTagService);

}

@Test
public void testCreateNewLocationTagCallsAddOrUpdateLocationTagAndReturnsABadHTTPResponseStatusWhenIllegalArgumentExceptionIsThrown() throws Exception {

doThrow(new IllegalArgumentException()).when(locationTagService).addOrUpdateLocationTag(any());
LocationTag expectedLocationTag = initTestLocationTag1();

postRequestWithJsonContentAndReturnString(BASE_URL, locationTagJson, MockMvcResultMatchers.status().isBadRequest());

verify(locationTagService).addOrUpdateLocationTag(argumentCaptor.capture());
assertEquals(argumentCaptor.getValue().getName(), expectedLocationTag.getName());
}

@Test
public void testCreateNewLocationTagCallsAddOrUpdateLocationTagAndReturnsAConflictHTTPResponseStatusWhenDuplicateKeyExceptionIsThrown() throws Exception {

doThrow(new DuplicateKeyException("Id is duplicated")).when(locationTagService).addOrUpdateLocationTag(any());
LocationTag expectedLocationTag = initTestLocationTag1();

postRequestWithJsonContentAndReturnString(BASE_URL, locationTagJson, MockMvcResultMatchers.status().isConflict());

verify(locationTagService).addOrUpdateLocationTag(argumentCaptor.capture());
assertEquals(argumentCaptor.getValue().getName(), expectedLocationTag.getName());
}

@Test
public void testCreateNewLocationTagDoesNotCallAddOrUpdateLocationTagWhenJsonProcessingException() throws Exception {

postRequestWithJsonContentAndReturnString(BASE_URL, "{\"ids\":\"0\"}", MockMvcResultMatchers.status().isBadRequest());

verifyNoInteractions(locationTagService);

}


@Test
public void testShouldUpdateExistingLocationTagResource() throws Exception {
LocationTag expectedLocationTag = initTestLocationTag1();
Expand Down

0 comments on commit 88cc6be

Please sign in to comment.