@Test(expectedExceptions = IllegalArgumentException.class) public void testAddServiceWithMapHandlesNullValues() { Map<Class<?>, Object> services = new HashMap<>(); services.put(null, new Object()); ServiceContext context = ServiceContext.of(ConfigSource.class, MOCK_CONFIG_SOURCE); context.with(services); }
@Override public final O invoke(I input, ServiceContext context) throws ServiceException { if (!context.exist(getServiceName())) { throw new ServiceNotFoundException(this); } IData in = prepareInput(input); IData out = context.invoke(getServiceName(), in); return prepareOutput(out); }
public void testCreateServiceWithMapWorks() { final Map<Class<?>, Object> services = ImmutableMap.<Class<?>, Object>of( ConfigSource.class, MOCK_CONFIG_SOURCE, SecuritySource.class, MOCK_SECURITY_SOURCE); ServiceContext context = ServiceContext.of(services); assertThat(context.get(ConfigSource.class), is(MOCK_CONFIG_SOURCE)); assertThat(context.get(SecuritySource.class), is(MOCK_SECURITY_SOURCE)); }
public String executeService(String name, String request) throws Exception { String result = null; Action action = null; System.out.println("executando servico " + name); ServiceDefinition pd = getService(name); String actionName = pd.getClasses().get(0).getName(); ; ServiceContext ctx = new ServiceContext(repository); // carrega a classe e cria uma nova instancia Object obj = null; try { obj = ctx.getClassLoader().loadClass(actionName).newInstance(); } catch (InstantiationException iEx) { throw new Exception(actionName, iEx); } catch (IllegalAccessException iaEx) { throw new Exception(actionName, iaEx); } catch (ClassNotFoundException cnfEx) { throw new Exception(actionName, cnfEx); } catch (NoClassDefFoundError ncdfErr) { throw new Exception(actionName, ncdfErr); } catch (Exception ncdfErr) { throw new Exception(actionName, ncdfErr); } action = (Action) obj; Object response = action.execute(request); if (pd.getResponse() != null && pd.getResponse().equals("json")) { // JSONArray jsonArray = JSONArray.fromObject( response ); // result = jsonArray.toString(); StringWriter out = new StringWriter(); JSONValue.writeJSONString(response, out); result = out.toString(); } else { result = String.valueOf(response); } return result; }
@Override @CoreDataModificationStatus( modificationType = ModificationType.UPDATE, entityClass = OrgPartDO.class) public void deleteIfExists(ServiceContext context, Long orgId) { OrgPartDO orgPartDO = null; ScopeDO scopeDO = null; try { orgPartDO = orgPartDAO.findOrgPart(orgId, context.getScopeId()); scopeDO = scopeDAO.getById(context.getScopeId()); if (orgPartDO != null) { List<OrgPartDO> descendants = orgPartDAO.findParticipatingDescendantOrgParts( orgPartDO.getScope().getScopeId(), orgPartDO.getOrg().getOrgId()); if (descendants != null && descendants.size() > 0) { if (!configService.isBooleanActive( context, scopeDO.getScopeId(), ConfigService.ORG_PART_DESCENDANT_CASCADE_DELETE)) { FaultInfo faultInfo = new FaultInfo(); String errorMessage = messageSource.getMessage("validation.orgPart.childOrgsParticipating", null, null); faultInfo.setMessage(errorMessage); faultInfo.setAttributeErrors(Lists.<ValidationError>newArrayList()); throw new ValidationServiceException(errorMessage, faultInfo); } List<Long> orgPartIds = Lists.newArrayList(); for (OrgPartDO descendantDO : descendants) { orgPartIds.add(descendantDO.getOrgPartId()); } try { orgPartDAO.deleteOrgParts(orgPartIds); } catch (Exception e) { // safe to catch all exceptions // here because any failure is // treated the same. FaultInfo faultInfo = new FaultInfo(); String errorMessage = messageSource.getMessage("validation.orgPart.childOrgsParticipating", null, null); faultInfo.setMessage(errorMessage); faultInfo.setAttributeErrors(Lists.<ValidationError>newArrayList()); throw new ValidationServiceException(errorMessage, faultInfo); } } } } catch (EmptyResultDataAccessException e) { // no action to take. } }
@Test public void dataModelCanRegisterActionsAndObservableValues() throws Exception { ServiceContext context = new ServiceContext(); { final IntValueProperty count = new IntValueProperty(); Runnable incCount = new Runnable() { @Override public void run() { count.increment(); } }; final StringValueProperty title = new StringValueProperty("Title"); final BoolValueProperty enabled = new BoolValueProperty(true); Runnable toggleEnabled = new Runnable() { @Override public void run() { enabled.invert(); } }; context.putValue("count", count); context.putValue("title", title); context.putValue("enabled", enabled); context.putAction("incCount", incCount); context.putAction("toggleEnabled", toggleEnabled); } IntValueProperty count = (IntValueProperty) context.getValue("count"); StringValueProperty title = (StringValueProperty) context.getValue("title"); BoolValueProperty enabled = (BoolValueProperty) context.getValue("enabled"); assertThat(title.get()).isEqualTo("Title"); assertThat(count.get()).isEqualTo(0); context.getAction("incCount").run(); assertThat(count.get()).isEqualTo(1); assertThat(enabled.get()).isTrue(); context.getAction("toggleEnabled").run(); assertThat(enabled.get()).isFalse(); }
@Override @CoreDataModificationStatus( modificationType = ModificationType.UPDATE, entityClass = OrgPartDO.class) public OrgPart createIfNotExists(ServiceContext context, Long orgId, Map<String, String> exts) { ScopeDO scopeDO = scopeDAO.getById(context.getScopeId()); return addOrUpdate(context, scopeDO, orgId, exts); }
@Test(expectedExceptions = ClassCastException.class) public void testCreateServiceWithIncorrectTypeIsDetected() { // Generics prevent ServiceContext.of(ConfigSource.class, MOCK_SECURITY_SOURCE) // from compiling. This test ensures we get equivalent safety when we configure // via a Map (where the generics can't help). Map<Class<?>, Object> services = new HashMap<>(); services.put(ConfigSource.class, MOCK_SECURITY_SOURCE); ServiceContext.of(services); }
private void copySessionAttributes(HttpServletRequest request, ServiceContext ctx) { if (copySessionAttributes == null) { return; // nothing to copy } HttpSession session = request.getSession(); for (int i = 0; i < copySessionAttributes.length; i++) { Object value = session.getAttribute(copySessionAttributes[i]); if (value instanceof Serializable) { ctx.setProperty(copySessionAttributes[i], (Serializable) value); } } }
@Test(expected = ValidationServiceException.class) public void testMergeAddNotDelegatable() { RoleDO delegatable = new RoleDO(); delegatable.setRoleId(Long.valueOf(2)); delegatable.setName("delgatable"); Mockito.when(roleDAO.findDelegatableRoles(any(Long.class), any(Long.class))) .thenReturn(Lists.<RoleDO>newArrayList()); Mockito.when(roleDAO.findRolesByCode(any(Long.class), any(List.class))) .thenReturn(Lists.newArrayList(delegatable)); Mockito.when(userDAO.getById(Long.valueOf(5))).thenReturn(new UserDO()); try { ServiceContext context = new ServiceContext(); User user = new User(); user.setUserId(Long.valueOf(5)); context.setUser(user); userRoleService.mergeUserRoles(context, Long.valueOf(1), Lists.<String>newArrayList("foo")); } finally { Mockito.verify(userRoleDAO, Mockito.never()).persist(any(UserRoleDO.class)); } }
/** intercept the call */ public void setInexactRepeating( int type, long triggerAtTime, long interval, PendingIntent operation) { // stub out // mAlarmManager.setInexactRepeating(type, triggerAtTime, interval, operation); Log.d(TAG, " alarm : setInexactRepeating : " + interval); ServiceContext.waitFor(5000); // wait for 5 seconds try { operation.send(); } catch (Exception e) { Log.e(TAG, e.toString()); } }
public void testAddingServiceWorks() { ServiceContext context = ServiceContext.of(ConfigSource.class, MOCK_CONFIG_SOURCE); ServiceContext context2 = context.with(SecuritySource.class, MOCK_SECURITY_SOURCE); assertThat(context2.get(ConfigSource.class), is(MOCK_CONFIG_SOURCE)); assertThat(context2.get(SecuritySource.class), is(MOCK_SECURITY_SOURCE)); }
@SuppressWarnings("unchecked") public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(4096); factory.setRepository(tempPathFile); ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(4194304); Map map = new HashMap(); List<FileItem> items = upload.parseRequest(request); Iterator<FileItem> i = items.iterator(); String destFilePath = null; String fileName = "TEMP"; String fieldName = null; FileItem fi = null; while (i.hasNext()) { fi = i.next(); fieldName = fi.getFieldName(); log.info("fieldName:" + fi.getFieldName()); if (fieldName.matches("serialno")) { fileName = fieldName; } if (fieldName.matches("ufile.*")) { File saveFile = new File(uploadPath, fileName); log.info("Saved FAX FILE to " + saveFile.getAbsolutePath()); fi.write(saveFile); if (Constant.isTraFaxServerSend()) { destFilePath = Constant.getFaxDirectory() + "/" + map.get("serialno"); if (map.get("fileType") != null && "png".equalsIgnoreCase(map.get("fileType").toString())) { destFilePath += ".png"; int width = NumberUtils.toInt(map.get("width").toString()); ImageRenderer.renderToImage(saveFile, destFilePath, width); map.put("filename", map.get("serialno") + ".png"); if (MagickImageOp.isWinOS()) { String tiffImage = null; log.debug("width:" + width); if (width > 700) { tiffImage = MagickImageOp.getInstance().buildHTiff(destFilePath); } else { tiffImage = MagickImageOp.getInstance().buildVTiff(destFilePath); } map.put("filename", tiffImage.substring(tiffImage.lastIndexOf("/") + 1)); new File(destFilePath).delete(); } saveFile.delete(); } else { destFilePath += ".xls"; File dest = new File(destFilePath); saveFile.renameTo(dest); map.put("filename", dest.getName()); } log.info("destFilePath:" + destFilePath); TrafaxStatusDao statusDao = (TrafaxStatusDao) ServiceContext.getBean("trafaxStatusDao"); statusDao.addFaxOut(map); } else { FaxWriter faxWriter = new FaxWriter(); faxWriter.write(map, saveFile); } } else { log.info(fi.getFieldName() + "=" + fi.getString()); map.put(fieldName, fi.getString()); } } } catch (Exception e) { e.printStackTrace(); } }
public TestAlarmManager(ServiceContext context) { super(context.getAndroidContext()); context.setAlarmMan(this); }
public void testUpdatingServiceWorks() { ServiceContext context = ServiceContext.of(ConfigSource.class, MOCK_CONFIG_SOURCE); ServiceContext context2 = context.with(ConfigSource.class, MOCK_CONFIG_SOURCE2); assertThat(context2.get(ConfigSource.class), is(MOCK_CONFIG_SOURCE2)); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testCreateServiceHandlesNullObject() { ServiceContext.of(ConfigSource.class, null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testAddingServiceWithNullClassIsHandled() { ServiceContext context = ServiceContext.of(ConfigSource.class, MOCK_CONFIG_SOURCE); context.with(null, new Object()); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testCreateServiceWithMapHandlesNullClass() { ServiceContext.of(null); }
public void testCreateServiceWorks() { ServiceContext context = ServiceContext.of(ConfigSource.class, MOCK_CONFIG_SOURCE); assertThat(context.get(ConfigSource.class), is(MOCK_CONFIG_SOURCE)); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testCreateServiceWithMapHandlesNullKeys() { Map<Class<?>, Object> services = new HashMap<>(); services.put(ConfigSource.class, null); ServiceContext.of(services); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testCreateServiceWithMapHandlesNullValues() { Map<Class<?>, Object> services = new HashMap<>(); services.put(null, new Object()); ServiceContext.of(services); }