Reformat CommentManagementAPITest class

feature/appm-store/pbac
nishadi 7 years ago
parent 37f595d382
commit 87d48dca98

@ -3,7 +3,6 @@ package org.wso2.carbon.device.application.mgt.store.api.services;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.exceptions.base.MockitoException;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
@ -13,8 +12,6 @@ import org.testng.IObjectFactory;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.ObjectFactory; import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.Comment; import org.wso2.carbon.device.application.mgt.common.Comment;
import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException;
import org.wso2.carbon.device.application.mgt.common.services.CommentsManager; import org.wso2.carbon.device.application.mgt.common.services.CommentsManager;
@ -27,20 +24,24 @@ import javax.ws.rs.core.Response;
import static org.mockito.MockitoAnnotations.initMocks; import static org.mockito.MockitoAnnotations.initMocks;
@PowerMockIgnore("javax.ws.rs.*") @SuppressStaticInitializationFor({ @PowerMockIgnore("javax.ws.rs.*")
"org.wso2.carbon.device.application.mgt.api.APIUtil" }) @PrepareForTest({ APIUtil.class, CommentsManager.class, @SuppressStaticInitializationFor({
CommentManagementAPITest.class, MultitenantUtils.class }) public class CommentManagementAPITest { "org.wso2.carbon.device.application.mgt.api.APIUtil" })
@PrepareForTest({ APIUtil.class, CommentsManager.class,
CommentManagementAPITest.class, MultitenantUtils.class })
public class CommentManagementAPITest {
private static final Log log = LogFactory.getLog(CommentManagementAPI.class); private static final Log log = LogFactory.getLog(CommentManagementAPI.class);
private CommentManagementAPI commentManagementAPI; private CommentManagementAPI commentManagementAPI;
private CommentsManager commentsManager; private CommentsManager commentsManager;
private int tenantId;
@ObjectFactory public IObjectFactory getObjectFactory() { @ObjectFactory
public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory(); return new org.powermock.modules.testng.PowerMockObjectFactory();
} }
@BeforeClass void init() throws CommentManagementException { @BeforeClass
void init() throws CommentManagementException {
log.info("Initializing CommentManagementAPI tests"); log.info("Initializing CommentManagementAPI tests");
initMocks(this); initMocks(this);
@ -48,7 +49,8 @@ import static org.mockito.MockitoAnnotations.initMocks;
this.commentManagementAPI = new CommentManagementAPIImpl(); this.commentManagementAPI = new CommentManagementAPIImpl();
} }
@Test public void testGetAllCommentsWithValidDetails() throws Exception { @Test
public void testGetAllCommentsWithValidDetails() throws Exception {
PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager);
Response response = this.commentManagementAPI.getAllComments("a", 1, 2); Response response = this.commentManagementAPI.getAllComments("a", 1, 2);
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
@ -57,34 +59,29 @@ import static org.mockito.MockitoAnnotations.initMocks;
Mockito.reset(commentsManager); Mockito.reset(commentsManager);
} }
@Test public void testAddComments() throws Exception { @Test
public void testAddComments() throws Exception {
Comment comment = CommentMgtTestHelper.getDummyComment("a", "a"); Comment comment = CommentMgtTestHelper.getDummyComment("a", "a");
PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager);
// PowerMockito.stub(PowerMockito.method(PrivilegedCarbonContext.class, "getThreadLocalCarbonContext"))
// .toReturn(Mockito.mock(PrivilegedCarbonContext.class, Mockito.RETURNS_MOCKS));
// PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getTenantId"))
// .toReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS));
Response response = this.commentManagementAPI.addComments(comment, null); Response response = this.commentManagementAPI.addComments(comment, null);
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode(),
"The response status should be 201."); "The response status should be 201.");
Mockito.reset(commentsManager); Mockito.reset(commentsManager);
} }
@Test public void testAddNullComment() throws Exception {
Comment comment=null; @Test
public void testAddNullComment() throws Exception {
PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager);
// PowerMockito.stub(PowerMockito.method(PrivilegedCarbonContext.class, "getThreadLocalCarbonContext")) Response response = this.commentManagementAPI.addComments(null, "a");
// .toReturn(Mockito.mock(PrivilegedCarbonContext.class, Mockito.RETURNS_MOCKS));
// PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getTenantId"))
// .toReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS));
Response response = this.commentManagementAPI.addComments(comment, null);
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(),
"The response status should be 400."); "The response status should be 400.");
Mockito.reset(commentsManager); Mockito.reset(commentsManager);
} }
@Test public void testUpdateComment() throws Exception { @Test
public void testUpdateComment() throws Exception {
Comment comment = CommentMgtTestHelper.getDummyComment("a", "a"); Comment comment = CommentMgtTestHelper.getDummyComment("a", "a");
PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager);
Response response = this.commentManagementAPI.updateComment(comment, 1); Response response = this.commentManagementAPI.updateComment(comment, 1);
@ -92,15 +89,18 @@ import static org.mockito.MockitoAnnotations.initMocks;
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
"The response status should be 200."); "The response status should be 200.");
} }
@Test public void testUpdateNullComment() throws Exception {
Comment comment=null; @Test
public void testUpdateNullComment() throws Exception {
PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager);
Response response = this.commentManagementAPI.updateComment(comment, 1); Response response = this.commentManagementAPI.updateComment(null, 1);
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(),
"The response status should be 400."); "The response status should be 400.");
} }
@Test public void testUpdateCommentWhenNullCommentId() throws Exception {
@Test
public void testUpdateCommentWhenNullCommentId() throws Exception {
Comment comment = CommentMgtTestHelper.getDummyComment("a", "a"); Comment comment = CommentMgtTestHelper.getDummyComment("a", "a");
PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager);
Response response = this.commentManagementAPI.updateComment(comment, 0); Response response = this.commentManagementAPI.updateComment(comment, 0);
@ -109,7 +109,8 @@ import static org.mockito.MockitoAnnotations.initMocks;
"The response status should be 404."); "The response status should be 404.");
} }
@Test public void testGetStars() throws Exception { @Test
public void testGetStars() throws Exception {
PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager);
Response response = this.commentManagementAPI.getStars("a"); Response response = this.commentManagementAPI.getStars("a");
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
@ -118,7 +119,8 @@ import static org.mockito.MockitoAnnotations.initMocks;
Mockito.reset(commentsManager); Mockito.reset(commentsManager);
} }
@Test public void testGetRatedUser() throws Exception { @Test
public void testGetRatedUser() throws Exception {
PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager);
Response response = this.commentManagementAPI.getRatedUser("a"); Response response = this.commentManagementAPI.getRatedUser("a");
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
@ -127,18 +129,20 @@ import static org.mockito.MockitoAnnotations.initMocks;
Mockito.reset(commentsManager); Mockito.reset(commentsManager);
} }
@Test public void testUpdateStars() throws Exception { @Test
public void testUpdateStars() throws Exception {
PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager);
Response response = this.commentManagementAPI.updateStars(3,"a"); Response response = this.commentManagementAPI.updateStars(3, "a");
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode(),
"The response status should be 200."); "The response status should be 200.");
Mockito.reset(commentsManager); Mockito.reset(commentsManager);
} }
@Test public void testUpdateInvalideStars() throws Exception { @Test
public void testUpdateInvalideStars() throws Exception {
PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager);
Response response = this.commentManagementAPI.updateStars(0,"a"); Response response = this.commentManagementAPI.updateStars(0, "a");
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(),
"The response status should be 400."); "The response status should be 400.");

Loading…
Cancel
Save