use of org.candlepin.policy.js.compliance.ComplianceStatus in project candlepin by candlepin.
the class HypervisorResourceTest method setupTest.
@Before
public void setupTest() {
Configuration config = new CandlepinCommonTestConfig();
testMigration = new GuestMigration(consumerCurator);
migrationProvider = Providers.of(testMigration);
this.i18n = I18nFactory.getI18n(getClass(), Locale.US, I18nFactory.FALLBACK);
this.hypervisorType = new ConsumerType(ConsumerTypeEnum.HYPERVISOR);
this.hypervisorType.setId("test-hypervisor-ctype");
this.mockConsumerType(this.hypervisorType);
this.modelTranslator = new StandardTranslator(this.consumerTypeCurator, this.environmentCurator, this.ownerCurator);
this.consumerResource = new ConsumerResource(this.consumerCurator, this.consumerTypeCurator, null, this.subscriptionService, this.ownerService, null, this.idCertService, null, this.i18n, this.sink, this.eventFactory, null, null, this.userService, null, null, this.ownerCurator, this.activationKeyCurator, null, this.complianceRules, this.deletedConsumerCurator, null, null, config, null, null, null, this.consumerBindUtil, null, null, new FactValidator(config, this.i18n), null, consumerEnricher, migrationProvider, modelTranslator);
this.guestIdResource = new GuestIdResource(this.guestIdCurator, this.consumerCurator, this.consumerTypeCurator, this.consumerResource, this.i18n, this.eventFactory, this.sink, migrationProvider, modelTranslator);
this.hypervisorResource = new HypervisorResource(consumerResource, consumerCurator, consumerTypeCurator, i18n, ownerCurator, migrationProvider, modelTranslator, guestIdResource);
// Ensure that we get the consumer that was passed in back from the create call.
when(consumerCurator.create(any(Consumer.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArguments()[0];
}
});
when(consumerCurator.create(any(Consumer.class), any(Boolean.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArguments()[0];
}
});
when(complianceRules.getStatus(any(Consumer.class), any(Date.class), any(Boolean.class))).thenReturn(new ComplianceStatus(new Date()));
when(ownerCurator.lookupByKey(any(String.class))).thenReturn(new Owner());
when(eventFactory.getEventBuilder(any(Target.class), any(Type.class))).thenReturn(consumerEventBuilder);
when(consumerEventBuilder.setEventData(any(Consumer.class))).thenReturn(consumerEventBuilder);
}
use of org.candlepin.policy.js.compliance.ComplianceStatus in project candlepin by candlepin.
the class InstalledProductStatusCalculatorTest method cannotStackFutureSubs.
@Test
public void cannotStackFutureSubs() {
Date now = new Date();
Owner owner = TestUtil.createOwner();
Product product = TestUtil.createProduct("p1", "product1");
Consumer consumer = this.mockConsumer(owner, product);
consumer.setCreated(now);
DateRange range1 = this.rangeRelativeToDate(now, -4, 12);
DateRange range2 = this.rangeRelativeToDate(range1.getEndDate(), 5, 6);
consumer.addEntitlement(this.mockStackedEntitlement(owner, consumer, "stack_id_1", product, 1, range1, product));
consumer.addEntitlement(this.mockStackedEntitlement(owner, consumer, "stack_id_1", product, 1, range2, product));
this.mockConsumerEntitlements(consumer, consumer.getEntitlements());
this.mockOwnerProducts(owner, Arrays.asList(product));
ComplianceStatus status = complianceRules.getStatus(consumer, now);
assertEquals("partial", status.getStatus());
assertTrue(status.getPartialStacks().containsKey("stack_id_1"));
}
use of org.candlepin.policy.js.compliance.ComplianceStatus in project candlepin by candlepin.
the class PoolManagerTest method testEntitleByProductsEmptyArray.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testEntitleByProductsEmptyArray() throws Exception {
Product product = TestUtil.createProduct();
List<Pool> pools = new ArrayList<>();
Pool pool1 = TestUtil.createPool(product);
pools.add(pool1);
Date now = new Date();
ValidationResult result = mock(ValidationResult.class);
// Setup an installed product for the consumer, we'll make the bind request
// with no products specified, so this should get used instead:
String[] installedPids = new String[] { product.getUuid() };
ComplianceStatus mockCompliance = new ComplianceStatus(now);
mockCompliance.addNonCompliantProduct(installedPids[0]);
when(complianceRules.getStatus(any(Consumer.class), any(Date.class), any(Boolean.class))).thenReturn(mockCompliance);
Page page = mock(Page.class);
when(page.getPageData()).thenReturn(pools);
when(mockPoolCurator.listAvailableEntitlementPools(any(Consumer.class), any(String.class), anyString(), anyString(), eq(now), any(PoolFilterBuilder.class), any(PageRequest.class), anyBoolean(), anyBoolean(), anyBoolean(), any(Date.class))).thenReturn(page);
CandlepinQuery mockQuery = mock(CandlepinQuery.class);
when(mockPoolCurator.listAllByIds(any(List.class))).thenReturn(mockQuery);
when(mockQuery.iterator()).thenReturn(Arrays.asList(pool1).listIterator());
when(enforcerMock.preEntitlement(any(Consumer.class), any(Pool.class), anyInt(), any(CallerType.class))).thenReturn(result);
when(result.isSuccessful()).thenReturn(true);
List<PoolQuantity> bestPools = new ArrayList<>();
bestPools.add(new PoolQuantity(pool1, 1));
when(autobindRules.selectBestPools(any(Consumer.class), any(String[].class), any(List.class), any(ComplianceStatus.class), any(String.class), any(Set.class), eq(false))).thenReturn(bestPools);
// Make the call but provide a null array of product IDs (simulates healing):
ConsumerType ctype = this.mockConsumerType(TestUtil.createConsumerType());
Consumer consumer = TestUtil.createConsumer(ctype, owner);
AutobindData data = AutobindData.create(consumer, owner).on(now);
manager.entitleByProducts(data);
verify(autobindRules).selectBestPools(any(Consumer.class), eq(installedPids), any(List.class), eq(mockCompliance), any(String.class), any(Set.class), eq(false));
}
use of org.candlepin.policy.js.compliance.ComplianceStatus in project candlepin by candlepin.
the class ConsumerResource method getComplianceStatusList.
@ApiOperation(notes = "Retrieves a Compliance Status list for a list of Consumers", value = "getComplianceStatusList")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/compliance")
@Transactional
public Map<String, ComplianceStatus> getComplianceStatusList(@QueryParam("uuid") @Verify(value = Consumer.class, nullable = true) List<String> uuids) {
Map<String, ComplianceStatus> results = new HashMap<>();
if (uuids != null && !uuids.isEmpty()) {
for (Consumer consumer : consumerCurator.findByUuids(uuids)) {
ConsumerType ctype = this.consumerTypeCurator.getConsumerType(consumer);
ComplianceStatus status;
if (ctype.isType(ConsumerTypeEnum.SHARE)) {
logShareConsumerRequestWarning("fetch compliance", consumer);
status = new ComplianceStatus(null);
} else {
status = complianceRules.getStatus(consumer, null);
}
results.put(consumer.getUuid(), status);
}
}
return results;
}
use of org.candlepin.policy.js.compliance.ComplianceStatus in project candlepin by candlepin.
the class ConsumerEnricher method enrich.
public void enrich(Consumer consumer) {
if (consumer == null || CollectionUtils.isEmpty(consumer.getInstalledProducts())) {
// No consumer or the consumer doesn't have any installed products -- nothing to do here.
return;
}
ComplianceStatus status = this.complianceRules.getStatus(consumer, null, null, false, true, true, true);
Map<String, DateRange> ranges = status.getProductComplianceDateRanges();
// Compile the product IDs for the products we're going to be enriching
Set<String> productIds = new HashSet<>();
Map<String, Product> productMap = new HashMap<>();
for (ConsumerInstalledProduct cip : consumer.getInstalledProducts()) {
productIds.add(cip.getProductId());
}
for (Product product : this.ownerProductCurator.getProductsByIds(consumer.getOwnerId(), productIds)) {
productMap.put(product.getId(), product);
}
// Perform enrichment of the consumer's installed products
for (ConsumerInstalledProduct cip : consumer.getInstalledProducts()) {
String pid = cip.getProductId();
DateRange range = ranges != null ? ranges.get(pid) : null;
// do those first.
if (status.getCompliantProducts().containsKey(pid)) {
cip.setStatus(GREEN_STATUS);
} else if (status.getPartiallyCompliantProducts().containsKey(pid)) {
cip.setStatus(YELLOW_STATUS);
} else if (status.getNonCompliantProducts().contains(pid)) {
cip.setStatus(RED_STATUS);
}
// Set the compliance date range if we have it
if (range != null) {
cip.setStartDate(range.getStartDate());
cip.setEndDate(range.getEndDate());
}
// Fetch missing product information from the actual product
Product product = productMap.get(pid);
if (product != null) {
if (cip.getVersion() == null) {
cip.setVersion(product.getAttributeValue(Product.Attributes.VERSION));
}
if (cip.getArch() == null) {
cip.setArch(product.getAttributeValue(Product.Attributes.ARCHITECTURE));
}
}
}
}
Aggregations