Search in sources :

Example 26 with Rules

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

the class RulesResource method upload.

@ApiOperation(notes = "Uploads the Rules Returns a copy of the uploaded rules.", value = "upload")
@ApiResponses({ @ApiResponse(code = 400, message = "") })
@POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN })
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
public String upload(String rulesBuffer) {
    if (rulesBuffer == null || rulesBuffer.isEmpty()) {
        throw new WebApplicationException(Response.Status.BAD_REQUEST);
    }
    Rules rules = null;
    try {
        String decoded = new String(Base64.decodeBase64(rulesBuffer));
        rules = new Rules(decoded);
    } catch (Throwable t) {
        log.error("Exception in rules upload", t);
        throw new BadRequestException(i18n.tr("Error decoding the rules. The text should be base 64 encoded"));
    }
    Rules oldRules = rulesCurator.getRules();
    rulesCurator.update(rules);
    sink.emitRulesModified(oldRules, rules);
    // Trigger a recompile of the JS rules so version/source are set correctly:
    jsProvider.compileRules(true);
    return rulesBuffer;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Rules(org.candlepin.model.Rules) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 27 with Rules

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

the class QuantityRulesTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    // 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);
    translator = new StandardTranslator(consumerTypeCurator, environmentCurator, ownerCuratorMock);
    quantityRules = new QuantityRules(provider.get(), new RulesObjectMapper(new ProductCachedSerializationModule(productCurator)), translator);
    owner = new Owner("Test Owner " + TestUtil.randomInt());
    product = TestUtil.createProduct();
    pool = TestUtil.createPool(owner, product);
    pool.setId("fakepoolid");
    ctype = TestUtil.createConsumerType();
    consumer = TestUtil.createConsumer(owner);
    when(consumerTypeCurator.find(eq(ctype.getId()))).thenReturn(ctype);
    when(consumerTypeCurator.getConsumerType(eq(consumer))).thenReturn(ctype);
    Entitlement e = TestUtil.createEntitlement(owner, consumer, pool, new EntitlementCertificate());
    Set<Entitlement> entSet = new HashSet<>();
    entSet.add(e);
    pool.setEntitlements(entSet);
    pool.getProduct().setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes");
    pool.getProduct().setAttribute(Product.Attributes.STACKING_ID, "1");
}
Also used : Owner(org.candlepin.model.Owner) EntitlementCertificate(org.candlepin.model.EntitlementCertificate) InputStream(java.io.InputStream) RulesObjectMapper(org.candlepin.policy.js.RulesObjectMapper) Rules(org.candlepin.model.Rules) StandardTranslator(org.candlepin.dto.StandardTranslator) Date(java.util.Date) ProductCachedSerializationModule(org.candlepin.jackson.ProductCachedSerializationModule) JsRunnerProvider(org.candlepin.policy.js.JsRunnerProvider) Entitlement(org.candlepin.model.Entitlement) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 28 with Rules

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

the class RulesVersionMatchingTest method ensureWorking.

@Test
public void ensureWorking() {
    Rules rules = null;
    try {
        rules = new Rules(version);
        if (!expectedToBeValid) {
            throw new RuntimeException("Expected rule parsing to have failed.");
        }
        assertEquals(expectedVersion, rules.getVersion());
    } catch (RuleParseException e) {
        if (expectedToBeValid) {
            throw e;
        }
    }
}
Also used : RuleParseException(org.candlepin.policy.js.RuleParseException) Rules(org.candlepin.model.Rules) Test(org.junit.Test)

Example 29 with Rules

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

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

the class PoolRulesInstanceTest method setUp.

@Before
public void setUp() {
    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);
}
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)

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