Search in sources :

Example 26 with ComplianceStatus

use of org.candlepin.policy.js.compliance.ComplianceStatus in project candlepin by candlepin.

the class ConsumerResourceCreationTest method init.

@Before
public void init() throws Exception {
    this.i18n = I18nFactory.getI18n(getClass(), Locale.US, I18nFactory.FALLBACK);
    this.modelTranslator = new StandardTranslator(this.consumerTypeCurator, this.environmentCurator, this.ownerCurator);
    testMigration = new GuestMigration(consumerCurator);
    migrationProvider = Providers.of(testMigration);
    this.config = initConfig();
    this.resource = new ConsumerResource(this.consumerCurator, this.consumerTypeCurator, null, this.subscriptionService, this.ownerService, null, this.idCertService, null, this.i18n, this.sink, null, null, null, this.userService, null, null, this.ownerCurator, this.activationKeyCurator, null, this.complianceRules, this.deletedConsumerCurator, null, null, this.config, null, null, null, this.consumerBindUtil, null, null, new FactValidator(this.config, this.i18n), null, consumerEnricher, migrationProvider, modelTranslator);
    this.system = this.initConsumerType();
    this.mockConsumerType(this.system);
    this.systemDto = this.modelTranslator.translate(this.system, ConsumerTypeDTO.class);
    owner = new Owner("test_owner");
    owner.setId(TestUtil.randomString());
    user = new User(USER, "");
    PermissionBlueprint p = new PermissionBlueprint(PermissionType.OWNER, owner, Access.ALL);
    role = new Role();
    role.addPermission(p);
    role.addUser(user);
    when(consumerCurator.create(any(Consumer.class))).thenAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArguments()[0];
        }
    });
    when(consumerCurator.create(any(Consumer.class), any(Boolean.class))).thenAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArguments()[0];
        }
    });
    when(userService.findByLogin(USER)).thenReturn(user);
    IdentityCertificate cert = new IdentityCertificate();
    cert.setKey("testKey");
    cert.setCert("testCert");
    cert.setId("testId");
    cert.setSerial(new CertificateSerial(new Date()));
    when(idCertService.generateIdentityCert(any(Consumer.class))).thenReturn(cert);
    when(ownerCurator.lookupByKey(owner.getKey())).thenReturn(owner);
    when(complianceRules.getStatus(any(Consumer.class), any(Date.class), any(Boolean.class), any(Boolean.class))).thenReturn(new ComplianceStatus(new Date()));
}
Also used : FactValidator(org.candlepin.util.FactValidator) Owner(org.candlepin.model.Owner) User(org.candlepin.model.User) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) CertificateSerial(org.candlepin.model.CertificateSerial) StandardTranslator(org.candlepin.dto.StandardTranslator) ConsumerTypeDTO(org.candlepin.dto.api.v1.ConsumerTypeDTO) Date(java.util.Date) Role(org.candlepin.model.Role) GuestMigration(org.candlepin.resource.util.GuestMigration) Answer(org.mockito.stubbing.Answer) Consumer(org.candlepin.model.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PermissionBlueprint(org.candlepin.model.PermissionBlueprint) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) IdentityCertificate(org.candlepin.model.IdentityCertificate) Before(org.junit.Before)

Example 27 with ComplianceStatus

use of org.candlepin.policy.js.compliance.ComplianceStatus in project candlepin by candlepin.

the class RulesObjectMapperTest method testComplianceStatusWithSourceConsumerInEnv.

/*
     * Tests a bug found where consumer environment content is serialized without
     * an environment (as it would be a circular dep), resulting in a null environment
     * on the object and a very upset hashCode method.
     */
@Test
public void testComplianceStatusWithSourceConsumerInEnv() {
    InputStream is = this.getClass().getResourceAsStream("/json/compliancestatus-with-env.json");
    String json = Util.readFile(is);
    // Just need this to parse without error:
    ComplianceStatus cs = objMapper.toObject(json, ComplianceStatus.class);
}
Also used : ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 28 with ComplianceStatus

use of org.candlepin.policy.js.compliance.ComplianceStatus in project candlepin by candlepin.

the class ComplianceStatusHasherTest method ensureDifferentHashWhenReasonAttributeChanges.

@Test
public void ensureDifferentHashWhenReasonAttributeChanges() {
    Consumer consumer = createConsumer(owner);
    ComplianceStatus testStatus = createInitialStatus(consumer);
    assertEquals(initialHash, generateHash(testStatus, consumer));
    ComplianceReason reason = testStatus.getReasons().iterator().next();
    // Test new attribute map same values
    Map<String, String> newAttrs = new HashMap<>();
    newAttrs.putAll(reason.getAttributes());
    reason.setAttributes(newAttrs);
    assertEquals(initialHash, generateHash(testStatus, consumer));
    // Test new value
    newAttrs.put(reason.getKey() + "-attr", reason.getKey() + "-value");
    assertEquals(initialHash, generateHash(testStatus, consumer));
    newAttrs.put(reason.getKey() + "-attr", "new value");
    assertNotEquals(initialHash, generateHash(testStatus, consumer));
    // Test new attribute.
    newAttrs.put("test-key", "test-value");
    assertNotEquals(initialHash, generateHash(testStatus, consumer));
    // Test attribute count.
    newAttrs.clear();
    assertNotEquals(initialHash, generateHash(testStatus, consumer));
}
Also used : Consumer(org.candlepin.model.Consumer) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) HashMap(java.util.HashMap) ComplianceReason(org.candlepin.policy.js.compliance.ComplianceReason) Test(org.junit.Test)

Example 29 with ComplianceStatus

use of org.candlepin.policy.js.compliance.ComplianceStatus in project candlepin by candlepin.

the class ComplianceStatusHasherTest method enssureDifferentHashWhenConsumerInstalledProductsChange.

@Test
public void enssureDifferentHashWhenConsumerInstalledProductsChange() {
    Consumer consumer = createConsumer(owner);
    Product product = TestUtil.createProduct("Test Product");
    ComplianceStatus testStatus = createInitialStatus(consumer);
    assertEquals(initialHash, generateHash(testStatus, consumer));
    Set<ConsumerInstalledProduct> initialInstalled = consumer.getInstalledProducts();
    consumer.setInstalledProducts(new HashSet<>(initialInstalled));
    assertEquals(initialHash, generateHash(testStatus, consumer));
    consumer.setInstalledProducts(new HashSet<>(initialInstalled));
    assertEquals(initialHash, generateHash(testStatus, consumer));
    ConsumerInstalledProduct installed = new ConsumerInstalledProduct(product.getUuid(), product.getName());
    consumer.addInstalledProduct(installed);
    String updatedHash = generateHash(testStatus, consumer);
    assertNotEquals(initialHash, updatedHash);
    // Test arch change
    installed.setArch("test-arch");
    assertNotEquals(updatedHash, generateHash(testStatus, consumer));
    installed.setArch(null);
    assertEquals(updatedHash, generateHash(testStatus, consumer));
    // Test version change
    installed.setVersion("1.2.3.4");
    assertNotEquals(updatedHash, generateHash(testStatus, consumer));
    installed.setVersion(null);
    assertEquals(updatedHash, generateHash(testStatus, consumer));
    consumer.getInstalledProducts().remove(installed);
    assertEquals(initialHash, generateHash(testStatus, consumer));
    consumer.getInstalledProducts().clear();
    assertNotEquals(initialHash, generateHash(testStatus, consumer));
}
Also used : Consumer(org.candlepin.model.Consumer) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Test(org.junit.Test)

Example 30 with ComplianceStatus

use of org.candlepin.policy.js.compliance.ComplianceStatus in project candlepin by candlepin.

the class ComplianceStatusHasherTest method ensureDifferentHashWhenConsumerEntitlementCountsChange.

@Test
public void ensureDifferentHashWhenConsumerEntitlementCountsChange() {
    Consumer consumer = createConsumer(owner);
    ComplianceStatus testStatus = createInitialStatus(consumer);
    assertEquals(initialHash, generateHash(testStatus, consumer));
    Set<Entitlement> initialEnts = consumer.getEntitlements();
    consumer.setEntitlements(new HashSet<>(initialEnts));
    assertEquals(initialHash, generateHash(testStatus, consumer));
    // Create and add an entitlement to the consumer.
    Entitlement ent = createEntitlement(Calendar.getInstance(), owner, consumer, "tp");
    assertNotEquals(initialHash, generateHash(testStatus, consumer));
    consumer.removeEntitlement(ent);
    assertEquals(initialHash, generateHash(testStatus, consumer));
    consumer.getEntitlements().clear();
    assertNotEquals(initialHash, generateHash(testStatus, consumer));
}
Also used : Consumer(org.candlepin.model.Consumer) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) Entitlement(org.candlepin.model.Entitlement) Test(org.junit.Test)

Aggregations

ComplianceStatus (org.candlepin.policy.js.compliance.ComplianceStatus)32 Consumer (org.candlepin.model.Consumer)26 Test (org.junit.Test)21 Date (java.util.Date)13 Entitlement (org.candlepin.model.Entitlement)8 HashSet (java.util.HashSet)7 ConsumerType (org.candlepin.model.ConsumerType)7 HashMap (java.util.HashMap)6 Owner (org.candlepin.model.Owner)5 Product (org.candlepin.model.Product)5 Before (org.junit.Before)5 StandardTranslator (org.candlepin.dto.StandardTranslator)4 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)4 Pool (org.candlepin.model.Pool)4 ComplianceReason (org.candlepin.policy.js.compliance.ComplianceReason)4 InputStream (java.io.InputStream)3 LinkedList (java.util.LinkedList)3 Set (java.util.Set)3 Target (org.candlepin.audit.Event.Target)3 Type (org.candlepin.audit.Event.Type)3