@Test(expected = BadRequestException.class) public void testCreatePersonConsumerWithActivationKey() { Consumer c = mock(Consumer.class); Owner o = mock(Owner.class); ActivationKey ak = mock(ActivationKey.class); NoAuthPrincipal nap = mock(NoAuthPrincipal.class); ActivationKeyCurator akc = mock(ActivationKeyCurator.class); OwnerCurator oc = mock(OwnerCurator.class); ConsumerTypeCurator ctc = mock(ConsumerTypeCurator.class); ConsumerContentOverrideCurator ccoc = mock(ConsumerContentOverrideCurator.class); ConsumerType cType = new ConsumerType(ConsumerTypeEnum.PERSON); when(ak.getId()).thenReturn("testKey"); when(o.getKey()).thenReturn("testOwner"); when(akc.lookupForOwner(eq("testKey"), eq(o))).thenReturn(ak); when(oc.lookupByKey(eq("testOwner"))).thenReturn(o); when(c.getType()).thenReturn(cType); when(c.getName()).thenReturn("testConsumer"); when(ctc.lookupByLabel(eq("person"))).thenReturn(cType); ConsumerResource cr = new ConsumerResource( null, ctc, null, null, null, null, null, i18n, null, null, null, null, null, null, null, oc, akc, null, null, null, null, null, new CandlepinCommonTestConfig(), null, null, null, consumerBindUtil, productCurator, null); cr.create(c, nap, null, "testOwner", "testKey", true); }
@Test(expected = NotFoundException.class) public void testNullPerson() { Consumer c = mock(Consumer.class); Owner o = mock(Owner.class); UserServiceAdapter usa = mock(UserServiceAdapter.class); UserPrincipal up = mock(UserPrincipal.class); OwnerCurator oc = mock(OwnerCurator.class); ConsumerTypeCurator ctc = mock(ConsumerTypeCurator.class); ConsumerType cType = new ConsumerType(ConsumerTypeEnum.PERSON); when(o.getKey()).thenReturn("testOwner"); when(oc.lookupByKey(eq("testOwner"))).thenReturn(o); when(c.getType()).thenReturn(cType); when(c.getName()).thenReturn("testConsumer"); when(ctc.lookupByLabel(eq("person"))).thenReturn(cType); when(up.canAccess(eq(o), eq(SubResource.CONSUMERS), eq(Access.CREATE))).thenReturn(true); // usa.findByLogin() will return null by default no need for a when ConsumerResource cr = new ConsumerResource( null, ctc, null, null, null, null, null, i18n, null, null, null, null, usa, null, null, oc, null, null, null, null, null, null, new CandlepinCommonTestConfig(), null, null, null, consumerBindUtil, productCurator, null); cr.create(c, up, null, "testOwner", null, true); }
@Test public void hypervisorCheckInCreatesNewConsumer() throws Exception { Owner owner = new Owner("admin"); Map<String, List<GuestId>> hostGuestMap = new HashMap<String, List<GuestId>>(); hostGuestMap.put("test-host", Arrays.asList(new GuestId("GUEST_A"), new GuestId("GUEST_B"))); when(consumerCurator.findByUuid(eq("test-host"))).thenReturn(null); when(ownerCurator.lookupByKey(eq(owner.getKey()))).thenReturn(owner); when(principal.canAccess(eq(owner), eq(Access.ALL))).thenReturn(true); when(consumerTypeCurator.lookupByLabel(eq(ConsumerTypeEnum.HYPERVISOR.getLabel()))) .thenReturn(hypervisorType); when(idCertService.generateIdentityCert(any(Consumer.class))) .thenReturn(new IdentityCertificate()); HypervisorCheckInResult result = hypervisorResource.hypervisorCheckIn(hostGuestMap, principal, owner.getKey()); Set<Consumer> created = result.getCreated(); assertEquals(1, created.size()); Consumer c1 = created.iterator().next(); assertEquals("test-host", c1.getUuid()); assertEquals(2, c1.getGuestIds().size()); assertEquals("GUEST_A", c1.getGuestIds().get(0).getGuestId()); assertEquals("GUEST_B", c1.getGuestIds().get(1).getGuestId()); assertEquals("x86_64", c1.getFact("uname.machine")); assertEquals("hypervisor", c1.getType().getLabel()); }
private void exportConsumerTypes(File baseDir) throws IOException { File typeDir = new File(baseDir.getCanonicalPath(), "consumer_types"); typeDir.mkdir(); for (ConsumerType type : consumerTypeCurator.listAll()) { File file = new File(typeDir.getCanonicalPath(), type.getLabel() + ".json"); FileWriter writer = new FileWriter(file); consumerType.export(mapper, writer, type); writer.close(); } }
@Before public void setUp() { owner = ownerCurator.create(new Owner(OWNER_NAME)); Role ownerAdminRole = createAdminRole(owner); roleCurator.create(ownerAdminRole); User user = new User("testing user", "pass"); principal = new UserPrincipal( "testing user", new ArrayList<Permission>( permFactory.createPermissions(user, ownerAdminRole.getPermissions())), false); setupPrincipal(principal); ConsumerType ueberCertType = new ConsumerType(ConsumerTypeEnum.UEBER_CERT); consumerTypeCurator.create(ueberCertType); or = new OwnerResource( ownerCurator, null, consumerCurator, i18n, null, null, null, null, null, poolManager, null, null, null, null, consumerTypeCurator, entCertCurator, entitlementCurator, ueberCertGenerator, null, null, contentOverrideValidator, serviceLevelValidator, null, null, null, productManager, contentManager); }
@Before public void createEnforcer() throws Exception { MockitoAnnotations.initMocks(this); when(config.getInt(eq(ConfigProperties.PRODUCT_CACHE_MAX))).thenReturn(100); productCache = new ProductCache(config, productAdapter); owner = createOwner(); ownerCurator.create(owner); consumer = TestUtil.createConsumer(owner); consumerTypeCurator.create(consumer.getType()); consumerCurator.create(consumer); 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)); JsRunner jsRules = new JsRunnerProvider(rulesCurator).get(); enforcer = new EntitlementRules( new DateSourceForTesting(2010, 1, 1), jsRules, productCache, i18n, config, consumerCurator, poolCurator); }