Search in sources :

Example 11 with Rules

use of org.candlepin.model.Rules in project candlepin by candlepin.

the class ActivationKeyRulesTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    Locale locale = new Locale("en_US");
    i18n = I18nFactory.getI18n(getClass(), "org.candlepin.i18n.Messages", locale, I18nFactory.FALLBACK);
    // Load the default production rules:
    InputStream is = this.getClass().getResourceAsStream(RulesCurator.DEFAULT_RULES_FILE);
    Rules rules = new Rules(Util.readFile(is));
    when(rulesCuratorMock.getUpdated()).thenReturn(new Date());
    when(rulesCuratorMock.getRules()).thenReturn(rules);
    when(cacheProvider.get()).thenReturn(cache);
    provider = new JsRunnerProvider(rulesCuratorMock, cacheProvider);
    ProductCurator productCurator = mock(ProductCurator.class);
    translator = new StandardTranslator(mockConsumerTypeCurator, environmentCurator, mockOwnerCurator);
    actKeyRules = new ActivationKeyRules(provider.get(), i18n, new RulesObjectMapper(new ProductCachedSerializationModule(productCurator)), translator);
}
Also used : Locale(java.util.Locale) ProductCachedSerializationModule(org.candlepin.jackson.ProductCachedSerializationModule) InputStream(java.io.InputStream) ProductCurator(org.candlepin.model.ProductCurator) JsRunnerProvider(org.candlepin.policy.js.JsRunnerProvider) RulesObjectMapper(org.candlepin.policy.js.RulesObjectMapper) Rules(org.candlepin.model.Rules) StandardTranslator(org.candlepin.dto.StandardTranslator) Date(java.util.Date) Before(org.junit.Before)

Example 12 with Rules

use of org.candlepin.model.Rules in project candlepin by candlepin.

the class PoolRulesTest method setUp.

@Before
public void setUp() {
    // Load the default production rules:
    InputStream is = this.getClass().getResourceAsStream(RulesCurator.DEFAULT_RULES_FILE);
    Rules rules = new Rules(Util.readFile(is));
    when(rulesCuratorMock.getUpdated()).thenReturn(new Date());
    when(rulesCuratorMock.getRules()).thenReturn(rules);
    when(configMock.getInt(eq(ConfigProperties.PRODUCT_CACHE_MAX))).thenReturn(100);
    poolRules = new PoolRules(poolManagerMock, configMock, entCurMock, ownerProdCuratorMock, productCurator);
    principal = TestUtil.createOwnerPrincipal();
    owner = principal.getOwners().get(0);
}
Also used : PoolRules(org.candlepin.policy.js.pool.PoolRules) InputStream(java.io.InputStream) PoolRules(org.candlepin.policy.js.pool.PoolRules) Rules(org.candlepin.model.Rules) Date(java.util.Date) Before(org.junit.Before)

Example 13 with Rules

use of org.candlepin.model.Rules 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 14 with Rules

use of org.candlepin.model.Rules in project candlepin by candlepin.

the class RulesResource method delete.

@ApiOperation(notes = "Removes the Rules  Deletes any uploaded rules, uses bundled rules instead", value = "delete")
@DELETE
@Produces(MediaType.APPLICATION_JSON)
public void delete() {
    Rules deleteRules = rulesCurator.getRules();
    rulesCurator.delete(deleteRules);
    log.warn("Deleting rules version: " + deleteRules.getVersion());
    sink.emitRulesDeleted(deleteRules);
    // Trigger a recompile of the JS rules so version/source are set correctly:
    jsProvider.compileRules(true);
}
Also used : Rules(org.candlepin.model.Rules) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation)

Example 15 with Rules

use of org.candlepin.model.Rules in project candlepin by candlepin.

the class JsRunnerProvider method compileRules.

public void compileRules(boolean forceRefresh) {
    scriptLock.writeLock().lock();
    try {
        // Check to see if we need to recompile. we do this inside the write lock
        // just to avoid race conditions where we might double compile
        Date newUpdated = rulesCurator.getUpdated();
        if (!forceRefresh && newUpdated.equals(this.currentRulesUpdated)) {
            return;
        }
        log.info("Recompiling rules with timestamp: {}", newUpdated);
        Context context = Context.enter();
        context.setOptimizationLevel(9);
        scope = context.initStandardObjects(null, true);
        try {
            Rules rules = rulesCurator.getRules();
            rulesVersion = rules.getVersion();
            rulesSource = rules.getRulesSource();
            script = context.compileString(rules.getRules(), "rules", 1, null);
            script.exec(context, scope);
            ((ScriptableObject) scope).sealObject();
            this.currentRulesUpdated = newUpdated;
        } finally {
            Context.exit();
        }
    } finally {
        scriptLock.writeLock().unlock();
    }
}
Also used : Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) Rules(org.candlepin.model.Rules) Date(java.util.Date)

Aggregations

Rules (org.candlepin.model.Rules)34 InputStream (java.io.InputStream)18 Date (java.util.Date)18 Test (org.junit.Test)15 Before (org.junit.Before)14 ProductCachedSerializationModule (org.candlepin.jackson.ProductCachedSerializationModule)9 Consumer (org.candlepin.model.Consumer)9 JsRunnerProvider (org.candlepin.policy.js.JsRunnerProvider)9 RulesObjectMapper (org.candlepin.policy.js.RulesObjectMapper)9 CandlepinQuery (org.candlepin.model.CandlepinQuery)8 ConsumerType (org.candlepin.model.ConsumerType)8 File (java.io.File)7 StandardTranslator (org.candlepin.dto.StandardTranslator)7 ExportRules (org.candlepin.policy.js.export.ExportRules)7 FileInputStream (java.io.FileInputStream)6 ZipInputStream (java.util.zip.ZipInputStream)6 Principal (org.candlepin.auth.Principal)6 CertificateSerial (org.candlepin.model.CertificateSerial)6 IdentityCertificate (org.candlepin.model.IdentityCertificate)6 MockResultIterator (org.candlepin.test.MockResultIterator)6