EventFactory { 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); 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.", e); } } @Inject  EventFactory(PrincipalProvider principalProvider,
        @Named("EventFactoryObjectMapper") ObjectMapper objectMapper); EventBuilder getEventBuilder(Target target, Type type); Event consumerCreated(Consumer newConsumer); Event rulesUpdated(Rules oldRules, Rules newRules); Event rulesDeleted(Rules deletedRules); Event activationKeyCreated(ActivationKey key); Event consumerDeleted(Consumer oldConsumer); Event entitlementCreated(Entitlement e); Event entitlementDeleted(Entitlement e); Event entitlementExpired(Entitlement e); Event entitlementChanged(Entitlement e); Event ownerCreated(Owner newOwner); Event ownerModified(Owner newOwner); Event ownerDeleted(Owner owner); Event poolCreated(Pool newPool); Event poolDeleted(Pool pool); Event exportCreated(Consumer consumer); Event importCreated(Owner owner); Event guestIdCreated(GuestId guestId); Event guestIdDeleted(GuestId guestId); Event subscriptionExpired(SubscriptionDTO subscription); Event ownerContentAccessModeChanged(Owner owner); Event complianceCreated(Consumer consumer, ComplianceStatus compliance); Event complianceCreated(Consumer consumer, SystemPurposeComplianceStatus compliance);  }
@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()); }
@Test public void testSyspurposeComplianceCreatedSetsEventData() { Consumer consumer = mock(Consumer.class); SystemPurposeComplianceStatus status = mock(SystemPurposeComplianceStatus.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("mismatched"); String reason1 = "unsatisfied usage: Production"; String reason2 = "unsatisfied sla: Premium"; String reason3 = "unsatisfied role: Red Hat Enterprise Linux Server"; when(status.getReasons()).thenReturn(ImmutableSet.of(reason1, reason2, reason3)); when(status.getNonCompliantRole()).thenReturn("Red Hat Enterprise Linux Server"); when(status.getNonCompliantAddOns()).thenReturn(new HashSet<>()); when(status.getNonCompliantSLA()).thenReturn("Premium"); when(status.getNonCompliantUsage()).thenReturn("Production"); Map<String, Set<Entitlement>> entitlements = new HashMap<>(); when(status.getCompliantRole()).thenReturn(entitlements); when(status.getCompliantAddOns()).thenReturn(entitlements); when(status.getCompliantSLA()).thenReturn(entitlements); when(status.getCompliantUsage()).thenReturn(entitlements); String expectedEventData = "{" + "\"nonCompliantUsage\":\"Production\"," + "\"compliantAddOns\":{}," + "\"nonCompliantRole\":\"Red Hat Enterprise Linux Server\"," + "\"reasons\":[" + "\"unsatisfied usage: Production\"," + "\"unsatisfied sla: Premium\"," + "\"unsatisfied role: Red Hat Enterprise Linux Server\"" + "]," + "\"compliantSLA\":{}," + "\"nonCompliantAddOns\":[]," + "\"compliantRole\":{}," + "\"nonCompliantSLA\":\"Premium\"," + "\"compliantUsage\":{}," + "\"status\":\"mismatched\"" + "}"; Event event = eventFactory.complianceCreated(consumer, status); assertEquals(expectedEventData, event.getEventData()); }