PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/hudson/plugins/blazemeter/TestJobUtility.java

https://gitlab.com/CORP-RESELLER/blazemeter-plugin
Java | 181 lines | 145 code | 23 blank | 13 comment | 0 complexity | f2eff65c6f818ffc12f43cdf6e798d8a MD5 | raw file
  1. /**
  2. Copyright 2016 BlazeMeter Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package hudson.plugins.blazemeter;
  14. import hudson.plugins.blazemeter.api.Api;
  15. import hudson.plugins.blazemeter.api.ApiV3Impl;
  16. import hudson.plugins.blazemeter.entities.CIStatus;
  17. import hudson.plugins.blazemeter.utils.JobUtility;
  18. import hudson.plugins.blazemeter.utils.Constants;
  19. import hudson.util.FormValidation;
  20. import org.apache.commons.io.FileUtils;
  21. import org.eclipse.jetty.util.log.StdErrLog;
  22. import org.json.JSONObject;
  23. import org.junit.*;
  24. import org.json.JSONException;
  25. import org.mockito.Mockito;
  26. import java.io.File;
  27. import java.io.IOException;
  28. public class TestJobUtility {
  29. private static StdErrLog stdErrLog= Mockito.mock(StdErrLog.class);
  30. @BeforeClass
  31. public static void setUp()throws IOException{
  32. MockedAPI.startAPI();
  33. MockedAPI.userProfile();
  34. MockedAPI.stopTestSession();
  35. MockedAPI.getMasterStatus();
  36. MockedAPI.getCIStatus();
  37. MockedAPI.autoDetectVersion();
  38. MockedAPI.getReportUrl();
  39. MockedAPI.getTestConfig();
  40. MockedAPI.putTestInfo();
  41. }
  42. @AfterClass
  43. public static void tearDown()throws IOException{
  44. MockedAPI.stopAPI();
  45. }
  46. @Test
  47. public void getUserEmail_positive() throws IOException,JSONException{
  48. String email= JobUtility.getUserEmail(TestConstants.MOCKED_USER_KEY_VALID, TestConstants.mockedApiUrl);
  49. Assert.assertEquals(email, "dzmitry.kashlach@blazemeter.com");
  50. }
  51. @Test
  52. public void getUserEmail_negative() throws IOException,JSONException{
  53. String email= JobUtility.getUserEmail(TestConstants.MOCKED_USER_KEY_INVALID, TestConstants.mockedApiUrl);
  54. Assert.assertEquals(email,"");
  55. }
  56. @Test
  57. public void getUserEmail_exception() throws IOException,JSONException{
  58. String email= JobUtility.getUserEmail(TestConstants.MOCKED_USER_KEY_EXCEPTION, TestConstants.mockedApiUrl);
  59. Assert.assertEquals(email,"");
  60. }
  61. @Test
  62. public void validateUserKey_positive() throws IOException,JSONException{
  63. FormValidation validation= JobUtility.validateUserKey(TestConstants.MOCKED_USER_KEY_VALID,
  64. TestConstants.mockedApiUrl);
  65. Assert.assertEquals(validation.kind, FormValidation.Kind.OK);
  66. Assert.assertEquals(validation.getMessage(), Constants.API_KEY_VALID+"dzmitry.kashlach@blazemeter.com");
  67. }
  68. @Test
  69. public void validateUserKey_negative() throws IOException,JSONException{
  70. FormValidation validation= JobUtility.validateUserKey(TestConstants.MOCKED_USER_KEY_INVALID,
  71. TestConstants.mockedApiUrl);
  72. Assert.assertEquals(validation.kind, FormValidation.Kind.ERROR);
  73. Assert.assertEquals(validation.getMessage(),
  74. "API key is not valid: unexpected exception=JSONObject[\"mail\"] not found.");
  75. }
  76. @Test
  77. public void validateUserKey_exception() throws IOException,JSONException{
  78. FormValidation validation= JobUtility.validateUserKey(TestConstants.MOCKED_USER_KEY_EXCEPTION,
  79. TestConstants.mockedApiUrl);
  80. Assert.assertEquals(validation.kind, FormValidation.Kind.ERROR);
  81. Assert.assertEquals(validation.getMessage(),
  82. "API key is not valid: API key=mock...tion blazemeterUrl="+TestConstants.mockedApiUrl+". Please, check proxy settings, serverUrl and userKey.");
  83. }
  84. @Test
  85. public void validateUserKey_empty() throws IOException,JSONException{
  86. FormValidation validation= JobUtility.validateUserKey("", TestConstants.mockedApiUrl);
  87. Assert.assertEquals(validation.kind, FormValidation.Kind.ERROR);
  88. Assert.assertEquals(validation.getMessage(), Constants.API_KEY_EMPTY);
  89. }
  90. @Test
  91. public void getVersion() throws IOException,JSONException{
  92. String version= JobUtility.getVersion();
  93. Assert.assertTrue(version.matches("^(\\d{1,}\\.+\\d{1,2}\\S*)$"));
  94. }
  95. @Test
  96. public void stopMaster(){
  97. Api api = new ApiV3Impl(TestConstants.MOCKED_USER_KEY_VALID, TestConstants.mockedApiUrl);
  98. boolean terminate = JobUtility.stopTestSession(api, TestConstants.TEST_MASTER_25, stdErrLog);
  99. Assert.assertEquals(terminate, true);
  100. terminate = JobUtility.stopTestSession(api, TestConstants.TEST_MASTER_70, stdErrLog);
  101. Assert.assertEquals(terminate, true);
  102. terminate = JobUtility.stopTestSession(api, TestConstants.TEST_MASTER_100, stdErrLog);
  103. Assert.assertEquals(terminate, false);
  104. terminate = JobUtility.stopTestSession(api, TestConstants.TEST_MASTER_140, stdErrLog);
  105. Assert.assertEquals(terminate, false);
  106. }
  107. @Test
  108. public void getReportUrl_pos(){
  109. String expectedReportUrl=TestConstants.mockedApiUrl+"/app/?public-token=ohImO6c8xstG4qBFqgRnsMSAluCBambtrqsTvAEYEXItmrCfgO#masters/testMasterId/summary";
  110. Api api = new ApiV3Impl(TestConstants.MOCKED_USER_KEY_VALID, TestConstants.mockedApiUrl);
  111. String actReportUrl= null;
  112. try {
  113. actReportUrl = JobUtility.getReportUrl(api, TestConstants.TEST_MASTER_ID, stdErrLog);
  114. } catch (Exception e) {
  115. Assert.fail();
  116. }
  117. Assert.assertEquals(expectedReportUrl,actReportUrl);
  118. }
  119. @Test
  120. public void getReportUrl_neg(){
  121. String expectedReportUrl=TestConstants.mockedApiUrl+"/app/#masters/testMasterId/summary";
  122. Api api = new ApiV3Impl(TestConstants.MOCKED_USER_KEY_INVALID, TestConstants.mockedApiUrl);
  123. String actReportUrl= null;
  124. try {
  125. actReportUrl = JobUtility.getReportUrl(api, TestConstants.TEST_MASTER_ID, stdErrLog);
  126. } catch (Exception e) {
  127. Assert.fail();
  128. }
  129. Assert.assertEquals(expectedReportUrl,actReportUrl);
  130. }
  131. @Test
  132. public void getSessionId() throws JSONException, IOException {
  133. File getSessionId_v3=new File(TestConstants.RESOURCES+"/getSessionId_v3.json");
  134. String getSessionId_v3_str=FileUtils.readFileToString(getSessionId_v3);
  135. JSONObject getSession_json=new JSONObject(getSessionId_v3_str);
  136. String session= JobUtility.getSessionId(getSession_json, stdErrLog);
  137. Assert.assertEquals(session,"r-v3-55a6136b314bd");
  138. }
  139. @Test
  140. public void getCIStatus_success(){
  141. Api api = new ApiV3Impl(TestConstants.MOCKED_USER_KEY_VALID, TestConstants.mockedApiUrl);
  142. CIStatus ciStatus= JobUtility.validateCIStatus(api, TestConstants.TEST_MASTER_SUCCESS, stdErrLog);
  143. Assert.assertEquals(CIStatus.success,ciStatus);
  144. }
  145. @Test
  146. public void getCIStatus_failure(){
  147. Api api = new ApiV3Impl(TestConstants.MOCKED_USER_KEY_VALID, TestConstants.mockedApiUrl);
  148. CIStatus ciStatus= JobUtility.validateCIStatus(api, TestConstants.TEST_MASTER_FAILURE, stdErrLog);
  149. Assert.assertEquals(CIStatus.failures,ciStatus);
  150. }
  151. @Test
  152. public void getCIStatus_error(){
  153. Api api = new ApiV3Impl(TestConstants.MOCKED_USER_KEY_VALID, TestConstants.mockedApiUrl);
  154. CIStatus ciStatus= JobUtility.validateCIStatus(api, TestConstants.TEST_MASTER_ERROR, stdErrLog);
  155. Assert.assertEquals(CIStatus.errors,ciStatus);
  156. }
  157. }