PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/jira-project/jira-components/jira-tests-parent/jira-tests-unit/src/test/java/com/atlassian/jira/license/TestLicenseDetailsFactoryImpl.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 319 lines | 265 code | 49 blank | 5 comment | 10 complexity | e9c055eb9e5ef4b16d677b408674d042 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.license;
  2. import com.atlassian.application.api.ApplicationKey;
  3. import com.atlassian.core.util.Clock;
  4. import com.atlassian.extras.api.AtlassianLicense;
  5. import com.atlassian.extras.api.LicenseException;
  6. import com.atlassian.extras.api.LicenseManager;
  7. import com.atlassian.extras.api.LicenseType;
  8. import com.atlassian.extras.api.Product;
  9. import com.atlassian.extras.api.jira.JiraLicense;
  10. import com.atlassian.fugue.Iterables;
  11. import com.atlassian.fugue.Option;
  12. import com.atlassian.jira.cluster.ClusterManager;
  13. import com.atlassian.jira.config.properties.ApplicationProperties;
  14. import com.atlassian.jira.datetime.DateTimeFormatter;
  15. import com.atlassian.jira.test.util.lic.License;
  16. import com.atlassian.jira.util.BuildUtilsInfo;
  17. import com.atlassian.jira.util.ConstantClock;
  18. import com.atlassian.jira.web.bean.I18nBean;
  19. import com.atlassian.jira.web.util.ExternalLinkUtil;
  20. import org.hamcrest.Description;
  21. import org.hamcrest.TypeSafeDiagnosingMatcher;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.junit.runner.RunWith;
  25. import org.mockito.Mock;
  26. import org.mockito.runners.MockitoJUnitRunner;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. import java.util.Objects;
  30. import static com.atlassian.jira.license.Licenses.LICENSE_ELA_4_ROLES;
  31. import static com.atlassian.jira.license.Licenses.LICENSE_NO_APP_ROLES_INVALID;
  32. import static com.atlassian.jira.license.Licenses.LICENSE_NO_ROLES;
  33. import static com.atlassian.jira.license.Licenses.LICENSE_ONE_APP_ROLE_NO_JIRA_ACTIVE;
  34. import static com.atlassian.jira.license.Licenses.LICENSE_ONE_ROLE;
  35. import static com.atlassian.jira.license.Licenses.LICENSE_THREE_ROLES;
  36. import static com.atlassian.jira.test.util.lic.cloud.CloudLicenses.LICENSE_ONDEMAND_6x_SERVICEDESK_TBP;
  37. import static com.atlassian.jira.test.util.lic.sd.ServiceDeskLicenses.LICENSE_SERVICE_DESK_ABP;
  38. import static com.atlassian.jira.test.util.lic.sd.ServiceDeskLicenses.LICENSE_SERVICE_DESK_ABP_EVAL;
  39. import static com.atlassian.jira.test.util.lic.sd.ServiceDeskLicenses.LICENSE_SERVICE_DESK_RBP;
  40. import static com.atlassian.jira.test.util.lic.sd.ServiceDeskLicenses.LICENSE_SERVICE_DESK_RBP_EVAL;
  41. import static com.atlassian.jira.test.util.lic.sd.ServiceDeskLicenses.LICENSE_SERVICE_DESK_TBP_EVAL;
  42. import static com.atlassian.jira.test.util.lic.sd.ServiceDeskLicenses.LICENSE_SERVICE_DESK_TBP_UNLIMITED;
  43. import static java.lang.String.format;
  44. import static org.hamcrest.MatcherAssert.assertThat;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.mockito.Mockito.when;
  48. @RunWith(MockitoJUnitRunner.class)
  49. public class TestLicenseDetailsFactoryImpl {
  50. private static final String BAD_LICENSE = "A Bad License";
  51. @Mock
  52. private ApplicationProperties applicationProperties;
  53. @Mock
  54. private AtlassianLicense atlassianLicense;
  55. @Mock
  56. private BuildUtilsInfo buildUtilsInfo;
  57. @Mock
  58. private DateTimeFormatter dateTimeFormatter;
  59. @Mock
  60. private ExternalLinkUtil externalLinkUtil;
  61. @Mock
  62. private I18nBean.BeanFactory i18Factory;
  63. @Mock
  64. private LicenseManager licenseManager;
  65. @Mock
  66. private ClusterManager clusterManager;
  67. @Mock
  68. private JiraLicense jiraLicense;
  69. private Clock clock = new ConstantClock(10);
  70. private LicenseDetailsFactoryImpl licenseDetailsFactory;
  71. @Before
  72. public void setup() {
  73. licenseDetailsFactory = new LicenseDetailsFactoryImpl(
  74. applicationProperties, externalLinkUtil, buildUtilsInfo,
  75. i18Factory, dateTimeFormatter, clock);
  76. }
  77. @Test
  78. public void testDecodeFailure() {
  79. when(licenseManager.getLicense(BAD_LICENSE)).thenThrow(new LicenseException());
  80. assertFalse(licenseDetailsFactory.isDecodeable(BAD_LICENSE));
  81. }
  82. @Test
  83. public void testDecodeBlank() {
  84. assertFalse(licenseDetailsFactory.isDecodeable(""));
  85. }
  86. @Test
  87. public void testDecodeNull() {
  88. assertFalse(licenseDetailsFactory.isDecodeable(null));
  89. }
  90. @Test(expected = LicenseException.class)
  91. public void testThrowsExceptionForInvalidLicense() {
  92. licenseDetailsFactory.getLicense("*&^%");
  93. }
  94. @Test(expected = LicenseException.class)
  95. public void testThrowsExceptionForNullLicense() {
  96. licenseDetailsFactory.getLicense(null);
  97. }
  98. @Test
  99. public void testNoLicenseRolesCanBeDecoded() {
  100. assertTrue("No license roles is a valid license", canDecode(LICENSE_NO_ROLES));
  101. }
  102. @Test
  103. public void testOneLicenseRoleCanBeDecoded() {
  104. assertTrue("No license roles is a valid license", canDecode(LICENSE_ONE_ROLE));
  105. }
  106. @Test
  107. public void testThreeLicenseRolesCanBeDecoded() {
  108. assertTrue("No license roles is a valid license", canDecode(LICENSE_THREE_ROLES));
  109. }
  110. @Test
  111. public void testEnterpriseLicenseCreation() {
  112. final LicenseDetails license = getLicenseDetails(LICENSE_ELA_4_ROLES);
  113. assertTrue("Returned license is a subscription-based license",
  114. license instanceof SubscriptionLicenseDetails);
  115. assertThat(license, LicenseDetailsMatcher.equals(LICENSE_ELA_4_ROLES));
  116. }
  117. @Test
  118. public void testVersionSevenNoBackwardsCompatible() {
  119. boolean decodable = canDecode(LICENSE_ONE_APP_ROLE_NO_JIRA_ACTIVE);
  120. assertTrue("License with apps and jira.active being omitted is valid", decodable);
  121. }
  122. @Test
  123. public void testVersionSevenWithNoAppsFailsToDecode() {
  124. boolean decodable = canDecode(LICENSE_NO_APP_ROLES_INVALID);
  125. assertFalse("License without an application role and no jira.active is invalid", decodable);
  126. }
  127. // The following Service Desk (SD) tests uses the following acronyms:
  128. // RBP: Role Based Pricing (i.e. JIRA SD 7.x licenses)
  129. // ABP: Agent Based Pricing (i.e. SD 2.x licenses)
  130. // TBP: Tier Base Pricing (i.e. SD 1.x licenses)
  131. // See ServiceDeskLicenses for more details of the different licenses.
  132. @Test
  133. public void testServiceDeskRBPLicenseCanBeDecoded() {
  134. assertTrue(canDecode(LICENSE_SERVICE_DESK_RBP));
  135. }
  136. @Test
  137. public void testServiceDeskRBPLicenseCanBeParsed() {
  138. final LicenseDetails details = getLicenseDetails(LICENSE_SERVICE_DESK_RBP);
  139. assertThat(details, LicenseDetailsMatcher.equals(LICENSE_SERVICE_DESK_RBP));
  140. final Product product = details.getJiraLicense().getProduct();
  141. assertTrue(Product.JIRA.getNamespace().equals(product.getNamespace()));
  142. assertFalse(details.isEvaluation());
  143. }
  144. @Test
  145. public void testServiceDeskRBPEvalLicenseCanBeDecoded() {
  146. assertTrue(canDecode(LICENSE_SERVICE_DESK_RBP_EVAL));
  147. }
  148. @Test
  149. public void testServiceDeskRBPEvalLicenseCanBeParsed() {
  150. final LicenseDetails details = getLicenseDetails(LICENSE_SERVICE_DESK_RBP_EVAL);
  151. assertThat(details, LicenseDetailsMatcher.equals(LICENSE_SERVICE_DESK_RBP_EVAL));
  152. final Product product = details.getJiraLicense().getProduct();
  153. assertTrue(Product.JIRA.getNamespace().equals(product.getNamespace()));
  154. assertTrue(details.isEvaluation());
  155. }
  156. @Test
  157. public void testServiceDeskTBPLicenseCanBeDecoded() {
  158. assertTrue(canDecode(LICENSE_SERVICE_DESK_TBP_UNLIMITED));
  159. }
  160. @Test
  161. public void testServiceDeskTBPLicenseCanBeParsed() {
  162. final LicenseDetails details = getLicenseDetails(LICENSE_SERVICE_DESK_TBP_UNLIMITED);
  163. assertThat(details, LicenseDetailsMatcher.equals(LICENSE_SERVICE_DESK_TBP_UNLIMITED));
  164. assertServiceDeskProduct(details);
  165. }
  166. @Test
  167. public void testServiceDeskTBPEvalLicenseCanBeDecoded() {
  168. assertTrue(canDecode(LICENSE_SERVICE_DESK_TBP_EVAL));
  169. }
  170. @Test
  171. public void testServiceDeskTBPEvalLicenseCanBeParsed() {
  172. final LicenseDetails details = getLicenseDetails(LICENSE_SERVICE_DESK_TBP_EVAL);
  173. assertThat(details, LicenseDetailsMatcher.equals(LICENSE_SERVICE_DESK_TBP_EVAL));
  174. assertServiceDeskProduct(details);
  175. }
  176. @Test
  177. public void testJira6xOnDemandLicenseWithServiceDesk() {
  178. final LicenseDetails details = getLicenseDetails(LICENSE_ONDEMAND_6x_SERVICEDESK_TBP);
  179. assertThat(details, LicenseDetailsMatcher.equals(LICENSE_ONDEMAND_6x_SERVICEDESK_TBP));
  180. assertServiceDeskProduct(details);
  181. }
  182. @Test
  183. public void testServiceDeskABPLicenseCanBeDecoded() {
  184. assertTrue(canDecode(LICENSE_SERVICE_DESK_ABP));
  185. }
  186. @Test
  187. public void testServiceDeskABPLicenseCanBeParsed() {
  188. final LicenseDetails details = getLicenseDetails(LICENSE_SERVICE_DESK_ABP);
  189. assertThat(details, LicenseDetailsMatcher.equals(LICENSE_SERVICE_DESK_ABP));
  190. assertServiceDeskProduct(details);
  191. }
  192. @Test
  193. public void testServiceDeskABPEvalLicenseCanBeDecoded() {
  194. assertTrue(canDecode(LICENSE_SERVICE_DESK_ABP_EVAL));
  195. }
  196. @Test
  197. public void testServiceDeskABPEvalLicenseCanBeParsed() {
  198. final LicenseDetails details = getLicenseDetails(LICENSE_SERVICE_DESK_ABP_EVAL);
  199. assertThat(details, LicenseDetailsMatcher.equals(LICENSE_SERVICE_DESK_ABP_EVAL));
  200. assertServiceDeskProduct(details);
  201. }
  202. private static void assertServiceDeskProduct(final LicenseDetails details) {
  203. final Option<Product> sdProduct = Iterables.findFirst(details.getJiraLicense().getProducts(),
  204. product -> product.getNamespace().contains("servicedesk"));
  205. assertTrue("Is ServiceDesk product?", sdProduct.isDefined());
  206. }
  207. private boolean canDecode(License license) {
  208. return licenseDetailsFactory.isDecodeable(license.getLicenseString());
  209. }
  210. private boolean canDecode(String license) {
  211. return licenseDetailsFactory.isDecodeable(license);
  212. }
  213. private LicenseDetails getLicenseDetails(License license) {
  214. return licenseDetailsFactory.getLicense(license.getLicenseString());
  215. }
  216. private static class LicenseDetailsMatcher extends TypeSafeDiagnosingMatcher<LicenseDetails> {
  217. private String description;
  218. private String organisation;
  219. private String sen;
  220. private boolean evaluation;
  221. private Map<ApplicationKey, Integer> roles = new HashMap<>();
  222. private LicenseType type;
  223. private LicenseDetailsMatcher(License license) {
  224. this.description = license.getDescription();
  225. this.organisation = license.getOrganisation();
  226. this.sen = license.getSen();
  227. this.evaluation = license.isEvaluation();
  228. Map<ApplicationKey, Integer> userCount = new HashMap<>();
  229. for (Map.Entry<String, License.Role> entry : license.getRoles().entrySet()) {
  230. userCount.put(ApplicationKey.valueOf(entry.getKey()), entry.getValue().getNumberOfUsers());
  231. }
  232. this.roles = userCount;
  233. this.type = license.getLicenseType();
  234. }
  235. private static LicenseDetailsMatcher equals(License license) {
  236. return new LicenseDetailsMatcher(license);
  237. }
  238. @Override
  239. protected boolean matchesSafely(final LicenseDetails item, final Description mismatchDescription) {
  240. boolean descriptionEquals = Objects.equals(description, item.getDescription());
  241. boolean organisationEquals = Objects.equals(organisation, item.getOrganisation());
  242. boolean senEquals = Objects.equals(sen, item.getSupportEntitlementNumber());
  243. boolean evaluationEquals = item.isEvaluation() == evaluation;
  244. final Map<ApplicationKey, Integer> rolesMap = toRoleMap(item);
  245. boolean rolesEquals = roles.equals(rolesMap);
  246. boolean typeEquals = type == item.getLicenseType();
  247. boolean result = descriptionEquals && organisationEquals && senEquals && evaluationEquals
  248. && rolesEquals && typeEquals;
  249. if (!result) {
  250. mismatchDescription.appendText(format("[description: %s, organisation: %s, sen: %s, isEval: %s, type: %s, roles: %s]",
  251. item.getDescription(), item.getOrganisation(), item.getSupportEntitlementNumber(),
  252. item.isEvaluation(), type, rolesMap));
  253. }
  254. return result;
  255. }
  256. @Override
  257. public void describeTo(final Description description) {
  258. description.appendText(format("[description: %s, organisation: %s, sen: %s, isEval: %s, type %s, roles: %s]",
  259. this.description, organisation, sen, evaluation, type, roles));
  260. }
  261. private static Map<ApplicationKey, Integer> toRoleMap(LicenseDetails details) {
  262. Map<ApplicationKey, Integer> result = new HashMap<>();
  263. final LicensedApplications licensedApplications = details.getLicensedApplications();
  264. for (ApplicationKey applicationKey : licensedApplications.getKeys()) {
  265. result.put(applicationKey, licensedApplications.getUserLimit(applicationKey));
  266. }
  267. return result;
  268. }
  269. }
  270. }