package foam.nanos.crunch.crunchtest;

import foam.dao.*;
import foam.core.*;
import foam.nanos.auth.*;
import foam.nanos.crunch.*;

public class CapabilityReopenTest extends foam.nanos.test.Test {
  public void runTest(X x) {
    DAO capabilityDAO = (DAO) x.get("localCapabilityDAO");
    DAO userCapabilityJunctionDAO = (DAO) x.get("bareUserCapabilityJunctionDAO");
    DAO prerequisiteCapabilityJunctionDAO = (DAO) x.get("prerequisiteCapabilityJunctionDAO");
    DAO userDAO = (DAO) x.get("bareUserDAO");

    User user = new User.Builder(x).setId(888L).build();
    user = (User) userDAO.put(user);

    Capability c = new Capability.Builder(x).setId("c").build();
    Capability prereq1 = new Capability.Builder(x).setId("prereq1").build();
    Capability prereq2 = new Capability.Builder(x).setId("prereq2").build();
    c = (Capability) capabilityDAO.put(c);
    prereq1 = (Capability) capabilityDAO.put(prereq1);
    prereq2 = (Capability) capabilityDAO.put(prereq2);

    MinMaxCapability mmc = new MinMaxCapability.Builder(x).setId("mmc").setMin(1).setMax(2).build();
    MinMaxCapability mmp1 = new MinMaxCapability.Builder(x).setId("mmp1").build();
    MinMaxCapability mmp2 = new MinMaxCapability.Builder(x).setId("mmp2").build();
    mmc = (MinMaxCapability) capabilityDAO.put(mmc);
    mmp1 = (MinMaxCapability) capabilityDAO.put(mmp1);
    mmp2 = (MinMaxCapability) capabilityDAO.put(mmp2);

    CapabilityCapabilityJunction cprereq1 = new CapabilityCapabilityJunction.Builder(x).setSourceId("c").setTargetId("prereq1").build();
    CapabilityCapabilityJunction cprereq2 = new CapabilityCapabilityJunction.Builder(x).setSourceId("c").setTargetId("prereq2").build();
    CapabilityCapabilityJunction mmcmmp1 = new CapabilityCapabilityJunction.Builder(x).setSourceId("mmc").setTargetId("mmp1").build();
    CapabilityCapabilityJunction mmcmmp2 = new CapabilityCapabilityJunction.Builder(x).setSourceId("mmc").setTargetId("mmp2").build();
    prerequisiteCapabilityJunctionDAO.put(cprereq1);
    prerequisiteCapabilityJunctionDAO.put(cprereq2);
    prerequisiteCapabilityJunctionDAO.put(mmcmmp1);
    prerequisiteCapabilityJunctionDAO.put(mmcmmp2);

    UserCapabilityJunction ucjC = new UserCapabilityJunction.Builder(x)
      .setSourceId(user.getId())
      .setTargetId(c.getId())
      .setStatus(CapabilityJunctionStatus.GRANTED)
      .build();
    UserCapabilityJunction ucjPrereq1 = new UserCapabilityJunction.Builder(x)
      .setSourceId(user.getId())
      .setTargetId(prereq1.getId())
      .setStatus(CapabilityJunctionStatus.GRANTED)
      .build();
    UserCapabilityJunction ucjPrereq2 = new UserCapabilityJunction.Builder(x)
      .setSourceId(user.getId())
      .setTargetId(prereq2.getId())
      .setStatus(CapabilityJunctionStatus.GRANTED)
      .build();
    UserCapabilityJunction ucjMmc = new UserCapabilityJunction.Builder(x)
    .setSourceId(user.getId())
      .setTargetId(mmc.getId())
      .setStatus(CapabilityJunctionStatus.GRANTED)
      .build();
    UserCapabilityJunction ucjMmp1 = new UserCapabilityJunction.Builder(x)
      .setSourceId(user.getId())
      .setTargetId(mmp1.getId())
      .setStatus(CapabilityJunctionStatus.GRANTED)
      .build();
    UserCapabilityJunction ucjMmp2 = new UserCapabilityJunction.Builder(x)
      .setSourceId(user.getId())
      .setTargetId(mmp2.getId())
      .setStatus(CapabilityJunctionStatus.GRANTED)
      .build();
    ucjC = (UserCapabilityJunction) userCapabilityJunctionDAO.put(ucjC);
    ucjPrereq1 = (UserCapabilityJunction) userCapabilityJunctionDAO.put(ucjPrereq1);
    ucjPrereq2 = (UserCapabilityJunction) userCapabilityJunctionDAO.put(ucjPrereq2);
    ucjMmc = (UserCapabilityJunction) userCapabilityJunctionDAO.put(ucjMmc);
    ucjMmp1 = (UserCapabilityJunction) userCapabilityJunctionDAO.put(ucjMmp1);
    ucjMmp2 = (UserCapabilityJunction) userCapabilityJunctionDAO.put(ucjMmp2);

    CrunchService crunchService = (CrunchService) x.get("crunchService");
    Subject subject = new Subject.Builder(x).setUser(user).build();
    X userX = (X) x.put("subject", subject);

    boolean shouldReopenC = crunchService.maybeReopen(userX, "c");
    boolean shouldReopenMmc = crunchService.maybeReopen(userX, "mmc");
    test( ! shouldReopenC, "should not be able to reopen granted Capability");
    test( ! shouldReopenMmc, "should not be able to reopen granted MinmaxCapability");

    // try setting isrenewable on prereqs so c can be reopened but mmc should not be since it still has
    // one granted capability
    ((Renewable) ucjPrereq1).setIsInRenewablePeriod(true);
    ucjPrereq1 = (UserCapabilityJunction) userCapabilityJunctionDAO.put(ucjPrereq1);
    ((Renewable) ucjMmp1).setIsInRenewablePeriod(true);
    ucjMmp1 = (UserCapabilityJunction) userCapabilityJunctionDAO.put(ucjMmp1);

    System.out.println(ucjC);System.out.println(ucjPrereq1);System.out.println(ucjPrereq2);
    System.out.println(ucjMmc);System.out.println(ucjMmp1);System.out.println(ucjMmp2);

    shouldReopenC = crunchService.maybeReopen(userX, "c");
    shouldReopenMmc = crunchService.maybeReopen(userX, "mmc");
    test( shouldReopenC, "should be able to reopen capability missing prereq");
    test( ! shouldReopenMmc, "should still not be able to reopen granted MinmaxCapability");

    ((Renewable) ucjMmp2).setIsInRenewablePeriod(true);
    ucjMmp2 = (UserCapabilityJunction) userCapabilityJunctionDAO.put(ucjMmp2);
    shouldReopenMmc = crunchService.maybeReopen(userX, "mmc");
    test( shouldReopenMmc, "should be able to reopen granted MinmaxCapability");




  }
}
@@ -4796,189 +4796,8 @@
 );%0A%0A
-    System.out.println(ucjC);System.out.println(ucjPrereq1);System.out.println(ucjPrereq2);%0A    System.out.println(ucjMmc);System.out.println(ucjMmp1);System.out.println(ucjMmp2);%0A%0A
     

remove logs