-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from martaswiatkowska/coverage-tests
Added unit tests
- Loading branch information
Showing
4 changed files
with
175 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/test/java/pl/cyfronet/ltos/rest/EngineExtensionControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package pl.cyfronet.ltos.rest; | ||
|
||
import org.hibernate.SessionFactory; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
|
||
import org.springframework.transaction.annotation.Transactional; | ||
import pl.cyfronet.ltos.bean.*; | ||
import pl.cyfronet.ltos.repository.DocumentWeightRepository; | ||
import pl.cyfronet.ltos.repository.MockMvcSecurityTest; | ||
import pl.cyfronet.ltos.repository.UserRepository; | ||
import pl.cyfronet.ltos.rest.controller.EngineExtensionController; | ||
import pl.cyfronet.ltos.security.PortalUser; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* Created by marta on 7/19/17. | ||
*/ | ||
|
||
|
||
public class EngineExtensionControllerTest extends MockMvcSecurityTest { | ||
|
||
|
||
@InjectMocks | ||
private EngineExtensionController controller; | ||
|
||
@Mock | ||
private DocumentWeightRepository documentWeightRepository; | ||
|
||
@Mock | ||
private UserRepository userRepository; | ||
|
||
@Mock | ||
private SessionFactory hibernateSessionFactory; | ||
|
||
@Before | ||
public void setUp(){ | ||
MockitoAnnotations.initMocks(this); | ||
|
||
} | ||
|
||
@Test | ||
public void getDocumentWeightsTest() throws Exception { | ||
|
||
User user = mock(User.class); | ||
PortalUser portalUser = mock(PortalUser.class); | ||
|
||
List<DocumentWeight> documentsWeights = getDocumentWeightList(); | ||
|
||
when(portalUser.getUserBean()).thenReturn(user); | ||
when(user.getDocuments()).thenReturn(documentsWeights); | ||
|
||
List<DocumentWeight> result = controller.getDocumentWeights(portalUser); | ||
|
||
Assert.assertEquals(documentsWeights, result); | ||
} | ||
|
||
|
||
@Test | ||
@Transactional | ||
public void setDocumentWeightsTest() throws Exception { | ||
|
||
|
||
PortalUser portalUserMock = mock(PortalUser.class); | ||
Long id = 11111L; | ||
User user = mock(User.class); | ||
|
||
List<DocumentWeight> documentWeights = getDocumentWeightList(); | ||
when(user.getDocuments()).thenReturn(documentWeights); | ||
when(portalUserMock.getUserBean()).thenReturn(user); | ||
when(user.getId()).thenReturn(id); | ||
|
||
user.setDocuments(documentWeights); | ||
|
||
when(userRepository.findOne(user.getId())).thenReturn(user); | ||
controller.setDocumentWeights(portalUserMock, documentWeights); | ||
|
||
} | ||
|
||
|
||
private List<DocumentWeight> getDocumentWeightList(){ | ||
|
||
DocumentWeight document = mock(DocumentWeight.class); | ||
List<DocumentWeight> docs = new ArrayList<>(); | ||
docs.add(document); | ||
return docs; | ||
} | ||
|
||
|
||
|
||
|
||
} |
126 changes: 71 additions & 55 deletions
126
src/test/java/pl/cyfronet/ltos/rest/RestControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,107 @@ | ||
package pl.cyfronet.ltos.rest; | ||
|
||
import com.agreemount.EngineFacade; | ||
import com.agreemount.bean.document.Document; | ||
import com.agreemount.bean.identity.Identity; | ||
import com.agreemount.bean.identity.provider.IdentityProvider; | ||
import com.agreemount.slaneg.action.ActionContext; | ||
import com.agreemount.slaneg.action.ActionContextFactory; | ||
import com.agreemount.slaneg.db.DocumentOperations; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
import pl.cyfronet.bazaar.engine.extension.bean.IndigoDocument; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import pl.cyfronet.ltos.repository.MockMvcSecurityTest; | ||
import pl.cyfronet.ltos.repository.UserRepository; | ||
import pl.cyfronet.ltos.rest.bean.IndigoWrapper; | ||
import pl.cyfronet.ltos.rest.bean.sla.Sla; | ||
import pl.cyfronet.ltos.rest.controller.RestController; | ||
import pl.cyfronet.ltos.rest.logic.IndigoRestLogic; | ||
import pl.cyfronet.ltos.rest.util.IndigoConverter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.*; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import static org.mockito.Matchers.anyString; | ||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* Created by piotr on 18.07.16. | ||
*/ | ||
|
||
public class RestControllerTest extends MockMvcSecurityTest { | ||
|
||
@Test | ||
public void testGetUsers() throws Exception { | ||
// MvcResult result = mockMvc.perform(get("/rest/slam/preferences/test").session(user())) | ||
// .andExpect(status().isOk()).andReturn(); | ||
// | ||
// String content = result.getResponse().getContentAsString(); | ||
// Assert.assertTrue(content.contains("\"customer\":\"test\"")); | ||
} | ||
|
||
@Autowired | ||
private ActionContextFactory actionContextFactory; | ||
|
||
@Autowired | ||
private EngineFacade engineFacade; | ||
@InjectMocks | ||
private RestController restController; | ||
|
||
@Mock | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
public IdentityProvider identityProvider; | ||
@Mock | ||
private IndigoRestLogic indigoRestLogic; | ||
|
||
@Mock | ||
private IndigoConverter converter; | ||
|
||
@Mock | ||
private DocumentOperations documentOperations; | ||
|
||
private void addSLAEST() throws Exception { | ||
|
||
// HashMap<String, String> urlVariables = new HashMap<String, String>(); | ||
// urlVariables.put("site", "BARI"); | ||
// urlVariables.put("id", "test_document"); | ||
// mockMvc.perform(put("/api/sla", urlVariables).session(user())).andExpect(status().isOk()).andReturn(); | ||
@Before | ||
public void setup(){ | ||
MockitoAnnotations.initMocks(this); | ||
|
||
// mockMvc.perform(put("/api/sla", urlVariables).session(user())).andExpect(status().isOk()).andReturn(); | ||
} | ||
|
||
@Test | ||
public void GetSLAs() throws Exception { | ||
|
||
Sla sla = mock(Sla.class); | ||
when(sla.getId()).thenReturn("1111L"); | ||
when(indigoRestLogic.getSLAs(null, null, null, null, null)).thenReturn(Arrays.asList(sla)); | ||
|
||
// mockMvc.perform(get("/invoke?actionId=editPublicDragt-second-step-Buy+resources&documentId="+doc.getId()).session(user())) | ||
// .andExpect(status().isOk()).andReturn() | ||
HashMap<String, String> urlVariables = new HashMap<String, String>(); | ||
urlVariables.put("site", "BARI"); | ||
urlVariables.put("id", "test_document"); | ||
|
||
List<Sla> result = restController.getSLAs(null, null, null, null, null); | ||
Assert.assertEquals(Arrays.asList(sla), result); | ||
} | ||
|
||
// @Test | ||
// public void testGetSLA() throws Exception { | ||
// add sla offer | ||
// MvcResult result = null; | ||
// addSLAEST(); | ||
// result = mockMvc.perform(put("/pool/create", {id: "testPool", site: "VAL"}).session(user())) | ||
// .andExpect(status().isOk()).andReturn(); | ||
// | ||
// result = mockMvc.perform(get("/rest/slam/sla/"+sla.getId()).session(user())) | ||
// .andExpect(status().isOk()).andReturn(); | ||
// | ||
// String content = result.getResponse().getContentAsString(); | ||
// } | ||
@Test | ||
public void getSLAsTest() throws Exception { | ||
Sla sla = mock(Sla.class); | ||
String id = anyString(); | ||
|
||
when(indigoRestLogic.getSLA(id)).thenReturn(sla); | ||
Sla sla2 = restController.getSLA(id); | ||
|
||
Assert.assertEquals(sla2, sla); | ||
|
||
} | ||
|
||
@Test | ||
public void testPreferences() { | ||
public void getUserTest() throws Exception { | ||
IndigoWrapper userWrapper = mock(IndigoWrapper.class); | ||
|
||
String login = anyString(); | ||
|
||
when(indigoRestLogic.getDataForLogin(login)).thenReturn(userWrapper); | ||
|
||
IndigoWrapper result = restController.getUser(login); | ||
|
||
Assert.assertEquals(userWrapper, result); | ||
|
||
|
||
} | ||
|
||
@Test | ||
public void testGetSLAs() throws Exception { | ||
MvcResult result = mockMvc.perform(get("/rest/slam/sla").session(user())) | ||
.andExpect(status().isOk()).andReturn(); | ||
public void getUserInfoTest() throws Exception { | ||
|
||
List<IndigoWrapper> listUsers = new ArrayList<IndigoWrapper>(); | ||
String login = "aaa"; | ||
|
||
List<IndigoWrapper> result = restController.getUsers(login); | ||
|
||
Assert.assertEquals(listUsers, result); | ||
|
||
|
||
String content = result.getResponse().getContentAsString(); | ||
} | ||
} |