Search in sources :

Example 1 with JsRunner

use of org.candlepin.policy.js.JsRunner in project candlepin by candlepin.

the class ComplianceRulesTest method additivePropertiesCanStillDeserialize.

/*
     * Make sure additive properties coming back from the javascript do not break when
     * we deserialize.
     */
@Test
public void additivePropertiesCanStillDeserialize() {
    JsRunner mockRunner = mock(JsRunner.class);
    compliance = new ComplianceRules(mockRunner, entCurator, new StatusReasonMessageGenerator(i18n), eventSink, consumerCurator, consumerTypeCurator, new RulesObjectMapper(new ProductCachedSerializationModule(productCurator)), translator);
    when(mockRunner.runJsFunction(any(Class.class), eq("get_status"), any(JsContext.class))).thenReturn("{\"unknown\": \"thing\"}");
    Consumer c = mockConsumerWithTwoProductsAndNoEntitlements();
    // Nothing to assert here, we just need to see this return without error.
    compliance.getStatus(c, TestUtil.createDate(2011, 8, 30));
}
Also used : ProductCachedSerializationModule(org.candlepin.jackson.ProductCachedSerializationModule) Consumer(org.candlepin.model.Consumer) RulesObjectMapper(org.candlepin.policy.js.RulesObjectMapper) JsContext(org.candlepin.policy.js.JsContext) JsRunner(org.candlepin.policy.js.JsRunner) Test(org.junit.Test)

Example 2 with JsRunner

use of org.candlepin.policy.js.JsRunner in project candlepin by candlepin.

the class AutobindRulesTest method createEnforcer.

@Before
public void createEnforcer() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(config.getInt(eq(ConfigProperties.PRODUCT_CACHE_MAX))).thenReturn(100);
    InputStream is = this.getClass().getResourceAsStream(RulesCurator.DEFAULT_RULES_FILE);
    Rules rules = new Rules(Util.readFile(is));
    when(rulesCurator.getRules()).thenReturn(rules);
    when(rulesCurator.getUpdated()).thenReturn(TestDateUtil.date(2010, 1, 1));
    when(cacheProvider.get()).thenReturn(cache);
    JsRunner jsRules = new JsRunnerProvider(rulesCurator, cacheProvider).get();
    translator = new StandardTranslator(consumerTypeCurator, environmentCurator, mockOwnerCurator);
    autobindRules = new AutobindRules(jsRules, mockProductCurator, consumerTypeCurator, mockOwnerCurator, new RulesObjectMapper(new ProductCachedSerializationModule(mockProductCurator)), translator);
    owner = new Owner();
    owner.setId(TestUtil.randomString());
    when(mockOwnerCurator.findOwnerById(eq(owner.getId()))).thenReturn(owner);
    ConsumerType ctype = new ConsumerType(ConsumerTypeEnum.SYSTEM);
    ctype.setId("test-ctype");
    consumer = new Consumer("test consumer", "test user", owner, ctype);
    when(consumerTypeCurator.find(eq(ctype.getId()))).thenReturn(ctype);
    when(consumerTypeCurator.lookupByLabel(eq(ctype.getLabel()))).thenReturn(ctype);
    when(consumerTypeCurator.getConsumerType(eq(consumer))).thenReturn(ctype);
    compliance = new ComplianceStatus();
    activeGuestAttrs = new HashMap<>();
    activeGuestAttrs.put("virtWhoType", "libvirt");
    activeGuestAttrs.put("active", "1");
}
Also used : Owner(org.candlepin.model.Owner) AutobindRules(org.candlepin.policy.js.autobind.AutobindRules) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) InputStream(java.io.InputStream) RulesObjectMapper(org.candlepin.policy.js.RulesObjectMapper) JsRunner(org.candlepin.policy.js.JsRunner) Rules(org.candlepin.model.Rules) AutobindRules(org.candlepin.policy.js.autobind.AutobindRules) StandardTranslator(org.candlepin.dto.StandardTranslator) ProductCachedSerializationModule(org.candlepin.jackson.ProductCachedSerializationModule) Consumer(org.candlepin.model.Consumer) JsRunnerProvider(org.candlepin.policy.js.JsRunnerProvider) ConsumerType(org.candlepin.model.ConsumerType) Before(org.junit.Before)

Example 3 with JsRunner

use of org.candlepin.policy.js.JsRunner in project candlepin by candlepin.

the class EnforcerTest method createEnforcer.

@Before
public void createEnforcer() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(config.getInt(eq(ConfigProperties.PRODUCT_CACHE_MAX))).thenReturn(100);
    owner = createOwner();
    ownerCurator.create(owner);
    consumer = this.createConsumer(owner);
    BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/rules/test-rules.js")));
    StringBuilder builder = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        builder.append(line + "\n");
    }
    reader.close();
    Rules rules = mock(Rules.class);
    when(rules.getRules()).thenReturn(builder.toString());
    when(rulesCurator.getRules()).thenReturn(rules);
    when(rulesCurator.getUpdated()).thenReturn(TestDateUtil.date(2010, 1, 1));
    when(cacheProvider.get()).thenReturn(cache);
    JsRunner jsRules = new JsRunnerProvider(rulesCurator, cacheProvider).get();
    translator = new StandardTranslator(consumerTypeCurator, mockEnvironmentCurator, mockOwnerCurator);
    enforcer = new EntitlementRules(new DateSourceForTesting(2010, 1, 1), jsRules, i18n, config, consumerCurator, consumerTypeCurator, mockProductCurator, new RulesObjectMapper(new ProductCachedSerializationModule(mockProductCurator)), mockOwnerCurator, mockOwnerProductCurator, mockProductShareCurator, mockProductManager, mockEventSink, mockEventFactory, translator);
}
Also used : ProductCachedSerializationModule(org.candlepin.jackson.ProductCachedSerializationModule) DateSourceForTesting(org.candlepin.test.DateSourceForTesting) InputStreamReader(java.io.InputStreamReader) EntitlementRules(org.candlepin.policy.js.entitlement.EntitlementRules) BufferedReader(java.io.BufferedReader) JsRunnerProvider(org.candlepin.policy.js.JsRunnerProvider) RulesObjectMapper(org.candlepin.policy.js.RulesObjectMapper) JsRunner(org.candlepin.policy.js.JsRunner) Rules(org.candlepin.model.Rules) EntitlementRules(org.candlepin.policy.js.entitlement.EntitlementRules) StandardTranslator(org.candlepin.dto.StandardTranslator) Before(org.junit.Before)

Example 4 with JsRunner

use of org.candlepin.policy.js.JsRunner in project candlepin by candlepin.

the class EntitlementRulesTestFixture method createEnforcer.

@Before
public void createEnforcer() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(config.getInt(eq(ConfigProperties.PRODUCT_CACHE_MAX))).thenReturn(100);
    InputStream is = this.getClass().getResourceAsStream(RulesCurator.DEFAULT_RULES_FILE);
    Rules rules = new Rules(Util.readFile(is));
    when(rulesCurator.getRules()).thenReturn(rules);
    when(rulesCurator.getUpdated()).thenReturn(TestDateUtil.date(2010, 1, 1));
    when(cacheProvider.get()).thenReturn(cache);
    JsRunner jsRules = new JsRunnerProvider(rulesCurator, cacheProvider).get();
    translator = new StandardTranslator(consumerTypeCurator, environmentCurator, ownerCurator);
    enforcer = new EntitlementRules(new DateSourceImpl(), jsRules, I18nFactory.getI18n(getClass(), Locale.US, I18nFactory.FALLBACK), config, consumerCurator, consumerTypeCurator, productCurator, new RulesObjectMapper(new ProductCachedSerializationModule(productCurator)), ownerCurator, ownerProductCuratorMock, productShareCurator, productManager, eventSink, eventFactory, translator);
    owner = new Owner();
    consumerType = this.mockConsumerType(new ConsumerType(ConsumerTypeEnum.SYSTEM));
    consumer = new Consumer("test consumer", "test user", owner, consumerType);
    poolRules = new PoolRules(poolManagerMock, config, entCurMock, ownerProductCuratorMock, productCurator);
}
Also used : Owner(org.candlepin.model.Owner) PoolRules(org.candlepin.policy.js.pool.PoolRules) InputStream(java.io.InputStream) RulesObjectMapper(org.candlepin.policy.js.RulesObjectMapper) JsRunner(org.candlepin.policy.js.JsRunner) Rules(org.candlepin.model.Rules) PoolRules(org.candlepin.policy.js.pool.PoolRules) StandardTranslator(org.candlepin.dto.StandardTranslator) ProductCachedSerializationModule(org.candlepin.jackson.ProductCachedSerializationModule) Consumer(org.candlepin.model.Consumer) JsRunnerProvider(org.candlepin.policy.js.JsRunnerProvider) ConsumerType(org.candlepin.model.ConsumerType) DateSourceImpl(org.candlepin.util.DateSourceImpl) Before(org.junit.Before)

Aggregations

ProductCachedSerializationModule (org.candlepin.jackson.ProductCachedSerializationModule)4 JsRunner (org.candlepin.policy.js.JsRunner)4 RulesObjectMapper (org.candlepin.policy.js.RulesObjectMapper)4 StandardTranslator (org.candlepin.dto.StandardTranslator)3 Consumer (org.candlepin.model.Consumer)3 Rules (org.candlepin.model.Rules)3 JsRunnerProvider (org.candlepin.policy.js.JsRunnerProvider)3 Before (org.junit.Before)3 InputStream (java.io.InputStream)2 ConsumerType (org.candlepin.model.ConsumerType)2 Owner (org.candlepin.model.Owner)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 JsContext (org.candlepin.policy.js.JsContext)1 AutobindRules (org.candlepin.policy.js.autobind.AutobindRules)1 ComplianceStatus (org.candlepin.policy.js.compliance.ComplianceStatus)1 EntitlementRules (org.candlepin.policy.js.entitlement.EntitlementRules)1 PoolRules (org.candlepin.policy.js.pool.PoolRules)1 DateSourceForTesting (org.candlepin.test.DateSourceForTesting)1 DateSourceImpl (org.candlepin.util.DateSourceImpl)1