ConsumerJPADao extends AbstractPLCEntityDao<Consumer> implements ConsumerDao { @SuppressWarnings("unchecked") @Override public Consumer getByKey(String key) { StringBuilder builder = new StringBuilder(); builder.append("select c from ").append(getEntityType().getName()).append(" c where c.key = :key"); Query query = getEntityManager().createQuery(builder.toString()); query.setParameter("key", key); Consumer consumer = null; List<Consumer> results = query.getResultList(); if (CollectionUtils.isNotEmpty(results)) { consumer = results.get(0); } return consumer; } @Inject  ConsumerJPADao(EntityManager em); @SuppressWarnings("unchecked") @Override Consumer getByKey(String key);  }
@Test public void getByKey() { Consumer c = persistTestEntity(); Consumer retrievedConsumer = getTestDao().getByKey(c.getKey()); assertNotNull(retrievedConsumer); assertEquals(c.getKey(), retrievedConsumer.getKey()); assertEquals(c.getSecret(), retrievedConsumer.getSecret()); assertEquals(c.getPrincipal(), retrievedConsumer.getPrincipal()); assertEquals(c.isInRole("plcuser"), c.isInRole("plcuser")); retrievedConsumer = getTestDao().getByKey("wrongKey"); assertNull(retrievedConsumer); }