Search in sources :

Example 1 with ComplianceReason

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

the class EventFactoryTest method testComplianceCreatedSetsEventData.

@Test
public void testComplianceCreatedSetsEventData() {
    Consumer consumer = mock(Consumer.class);
    ComplianceStatus status = mock(ComplianceStatus.class);
    when(consumer.getName()).thenReturn("consumer-name");
    when(consumer.getOwnerId()).thenReturn("owner-id");
    when(consumer.getUuid()).thenReturn("48b09f4e-f18c-4765-9c41-9aed6f122739");
    when(status.getStatus()).thenReturn("invalid");
    ComplianceReason reason1 = new ComplianceReason();
    reason1.setKey(ComplianceReason.ReasonKeys.SOCKETS);
    reason1.setMessage("Only supports 2 of 12 sockets.");
    reason1.setAttributes(ImmutableMap.of(ComplianceReason.Attributes.MARKETING_NAME, "Awesome OS"));
    ComplianceReason reason2 = new ComplianceReason();
    reason2.setKey(ComplianceReason.ReasonKeys.ARCHITECTURE);
    reason2.setMessage("Supports architecture ppc64 but the system is x86_64.");
    reason2.setAttributes(ImmutableMap.of(ComplianceReason.Attributes.MARKETING_NAME, "Awesome Middleware"));
    when(status.getReasons()).thenReturn(ImmutableSet.of(reason1, reason2));
    String expectedEventData = "{\"reasons\":[" + "{\"productName\":\"Awesome OS\"," + "\"message\":\"Only supports 2 of 12 sockets.\"}," + "{\"productName\":\"Awesome Middleware\"," + "\"message\":\"Supports architecture ppc64 but the system is x86_64.\"}]," + "\"status\":\"invalid\"}";
    Event event = eventFactory.complianceCreated(consumer, status);
    assertEquals(expectedEventData, event.getEventData());
}
Also used : Consumer(org.candlepin.model.Consumer) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) ComplianceReason(org.candlepin.policy.js.compliance.ComplianceReason) Test(org.junit.Test)

Example 2 with ComplianceReason

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

the class ComplianceStatusHasherTest method ensureDifferentHashWhenReasonKeyChange.

@Test
public void ensureDifferentHashWhenReasonKeyChange() {
    Consumer consumer = createConsumer(owner);
    ComplianceStatus testStatus = createInitialStatus(consumer);
    assertEquals(initialHash, generateHash(testStatus, consumer));
    ComplianceReason reason = testStatus.getReasons().iterator().next();
    reason.setKey("FOOF");
    assertNotEquals(initialHash, generateHash(testStatus, consumer));
}
Also used : Consumer(org.candlepin.model.Consumer) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) ComplianceReason(org.candlepin.policy.js.compliance.ComplianceReason) Test(org.junit.Test)

Example 3 with ComplianceReason

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

the class ComplianceStatusHasherTest method createInitialStatus.

private ComplianceStatus createInitialStatus(Consumer consumer) {
    // Need to make sure that dates are exactly the same
    // as this method will be called more than once.
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, 2001);
    cal.set(Calendar.MONTH, 4);
    cal.set(Calendar.DATE, 12);
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    ComplianceStatus initialStatus = new ComplianceStatus(cal.getTime());
    addMonths(cal, 4);
    initialStatus.addNonCompliantProduct("p1");
    initialStatus.addNonCompliantProduct("p2");
    initialStatus.addCompliantProduct("p3", createEntitlement(cal, owner, consumer, "p3"));
    initialStatus.addCompliantProduct("p4", createEntitlement(cal, owner, consumer, "p4"));
    initialStatus.addPartiallyCompliantProduct("p5", createEntitlement(cal, owner, consumer, "p5"));
    initialStatus.addPartialStack("p-stack", createEntitlement(cal, owner, consumer, "p6"));
    ComplianceReason reason1 = createReason("TEST-REASON-1");
    initialStatus.getReasons().add(reason1);
    ComplianceReason reason2 = createReason("TEST-REASON-2");
    initialStatus.getReasons().add(reason2);
    return initialStatus;
}
Also used : ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) Calendar(java.util.Calendar) ComplianceReason(org.candlepin.policy.js.compliance.ComplianceReason)

Example 4 with ComplianceReason

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

the class ComplianceStatusHasherTest method ensureDifferentHashWhenReasonsChange.

@Test
public void ensureDifferentHashWhenReasonsChange() {
    Consumer consumer = createConsumer(owner);
    ComplianceStatus testStatus = createInitialStatus(consumer);
    assertEquals(initialHash, generateHash(testStatus, consumer));
    ComplianceReason reason = new ComplianceReason();
    reason.setKey("TEST-REASON-KEY");
    reason.setMessage("This is a test!");
    testStatus.getReasons().add(reason);
    assertNotEquals(initialHash, generateHash(testStatus, consumer));
}
Also used : Consumer(org.candlepin.model.Consumer) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) ComplianceReason(org.candlepin.policy.js.compliance.ComplianceReason) Test(org.junit.Test)

Example 5 with ComplianceReason

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

the class EventFactory method complianceCreated.

public Event complianceCreated(Consumer consumer, ComplianceStatus compliance) {
    Map<String, Object> eventData = new HashMap<>();
    eventData.put("status", compliance.getStatus());
    List<Map<String, String>> reasons = new ArrayList<>(compliance.getReasons().size());
    for (ComplianceReason reason : compliance.getReasons()) {
        reasons.add(ImmutableMap.of("productName", reason.getAttributes().get(ComplianceReason.Attributes.MARKETING_NAME), "message", reason.getMessage()));
    }
    eventData.put("reasons", reasons);
    try {
        String eventDataJson = mapper.writeValueAsString(eventData);
        // is concerned only with the consumer UUID field.
        return new Event(Event.Type.CREATED, Event.Target.COMPLIANCE, consumer.getName(), principalProvider.get(), consumer.getOwnerId(), consumer.getUuid(), consumer.getUuid(), eventDataJson, null, null);
    } catch (JsonProcessingException e) {
        log.error("Error while building JSON for compliance.created event.", e);
        throw new IseException("Error while building JSON for compliance.created event.");
    }
}
Also used : IseException(org.candlepin.common.exceptions.IseException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ComplianceReason(org.candlepin.policy.js.compliance.ComplianceReason)

Aggregations

ComplianceReason (org.candlepin.policy.js.compliance.ComplianceReason)7 ComplianceStatus (org.candlepin.policy.js.compliance.ComplianceStatus)5 Consumer (org.candlepin.model.Consumer)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Map (java.util.Map)1 IseException (org.candlepin.common.exceptions.IseException)1