QuotaChecker { void checkServiceIdentityQuota(ObjectStoreConnection con, String domainName, ServiceIdentity service, String caller) { if (!quotaCheckEnabled) { return; } if (service == null) { return; } final Quota quota = getDomainQuota(con, domainName); int objectCount = getListSize(service.getHosts()); if (quota.getServiceHost() < objectCount) { throw ZMSUtils.quotaLimitError("service host quota exceeded - limit: " + quota.getServiceHost() + " actual: " + objectCount, caller); } objectCount = getListSize(service.getPublicKeys()); if (quota.getPublicKey() < objectCount) { throw ZMSUtils.quotaLimitError("service public key quota exceeded - limit: " + quota.getPublicKey() + " actual: " + objectCount, caller); } objectCount = con.countServiceIdentities(domainName) + 1; if (quota.getService() < objectCount) { throw ZMSUtils.quotaLimitError("service quota exceeded - limit: " + quota.getService() + " actual: " + objectCount, caller); } } QuotaChecker(); Quota getDomainQuota(ObjectStoreConnection con, String domainName);  }
@Test public void testCheckServiceQuotaNull() { QuotaChecker quotaCheck = new QuotaChecker(); quotaCheck.checkServiceIdentityQuota(null, "athenz", null, "caller"); }
@Test public void testCheckServiceQuota() { QuotaChecker quotaCheck = new QuotaChecker(); Quota mockQuota = new Quota().setName("athenz") .setService(2).setServiceHost(2).setPublicKey(2); ObjectStoreConnection con = Mockito.mock(ObjectStoreConnection.class); Mockito.when(con.getQuota("athenz")).thenReturn(mockQuota); Mockito.when(con.countServiceIdentities("athenz")).thenReturn(1); ArrayList<String> hosts = new ArrayList<>(); hosts.add("host1"); ArrayList<PublicKeyEntry> publicKeys = new ArrayList<>(); publicKeys.add(new PublicKeyEntry().setId("id1").setKey("key")); ServiceIdentity service = new ServiceIdentity(); service.setHosts(hosts); service.setPublicKeys(publicKeys); quotaCheck.checkServiceIdentityQuota(con, "athenz", service, "caller"); }