public void testGuestimateInvalidPackage() throws Exception {
   // guestimatePackageBySystem should return null if it
   // can't find a package, not throw an exception.
   try {
     assertNull(PackageManager.guestimatePackageBySystem(10000L, 100L, 100L, 0L, null));
   } catch (Exception e) {
     fail("method should return null");
   }
 }
  /** {@inheritDoc} */
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm formIn,
      HttpServletRequest request,
      HttpServletResponse response) {

    RequestContext requestContext = new RequestContext(request);
    User user = requestContext.getLoggedInUser();

    // If this is an easy one and we have the pid
    if (request.getParameter("pid") != null) {
      long pid = requestContext.getRequiredParam("pid");
      Package pkg = PackageFactory.lookupByIdAndUser(pid, user);

      // show permission error if pid is invalid like we did before
      if (pkg == null) {
        throw new PermissionException("Invalid pid");
      }

      if (pkg instanceof Patch) {
        request.setAttribute("type", "patch");
        request.setAttribute(PACKAGE_NAME, pkg.getPackageName().getName());
        request.setAttribute(
            "readme_url", DownloadManager.getPatchReadmeDownloadPath((Patch) pkg, user));
      } else if (pkg instanceof PatchSet) {
        request.setAttribute("type", "patchset");
        request.setAttribute(PACKAGE_NAME, pkg.getNameEvra());
        request.setAttribute(
            "readme_url", DownloadManager.getPatchSetReadmeDownloadPath((PatchSet) pkg, user));
      } else {
        request.setAttribute("type", "rpm");
        request.setAttribute(PACKAGE_NAME, pkg.getFilename());
        if (!pkg.getPackageKeys().isEmpty()) {
          request.setAttribute(PACKAGE_KEY, pkg.getPackageKeys().iterator().next().getKey());
        }
        boolean isDebug = pkg.getPackageName().getName().contains("debuginfo");

        request.setAttribute("isDebuginfo", isDebug);
        if (!isDebug) {
          Package debugPkg = PackageManager.findDebugInfo(user, pkg);
          String ftpUrl = PackageManager.generateFtpDebugPath(pkg);
          if (debugPkg != null) {
            request.setAttribute(
                "debugUrl", DownloadManager.getPackageDownloadPath(debugPkg, user));
          } else if (ftpUrl != null) {
            request.setAttribute("debugUrl", ftpUrl);
            request.setAttribute("debugFtp", true);
          }
        }
      }

      if (DownloadManager.isFileAvailable(pkg.getPath())) {
        request.setAttribute("url", DownloadManager.getPackageDownloadPath(pkg, user));
      }

      List<PackageSource> src = PackageFactory.lookupPackageSources(pkg);

      if (!src.isEmpty() && DownloadManager.isFileAvailable(src.get(0).getPath())) {
        request.setAttribute(
            "srpm_url", DownloadManager.getPackageSourceDownloadPath(pkg, src.get(0), user));
        request.setAttribute("srpm_path", src.get(0).getFile());
      }

      request.setAttribute("pack", pkg);
      // description can be null.
      if (pkg.getDescription() != null) {
        request.setAttribute("description", pkg.getDescription().replace("\n", "<BR>\n"));
      } else {
        request.setAttribute("description", pkg.getDescription());
      }
      request.setAttribute("packArches", PackageFactory.findPackagesWithDifferentArch(pkg));
      request.setAttribute("pid", pid);

      return mapping.findForward("default");
    } else { // we have to guess
      PackageListItem item = PackageListItem.parse(request.getParameter("id_combo"));
      Package pkg;
      long nameId = item.getIdOne();
      long evrId = item.getIdTwo();
      long archId = 0;
      if (item.getIdThree() != null) {
        archId = item.getIdThree();
      }

      Long cid = requestContext.getParamAsLong("cid");
      Long sid = requestContext.getParamAsLong("sid");
      if (cid != null) {
        pkg = PackageManager.guestimatePackageByChannel(cid, nameId, evrId, user.getOrg());

      } else if (sid != null) {
        pkg = PackageManager.guestimatePackageBySystem(sid, nameId, evrId, archId, user.getOrg());

      } else {
        throw new BadParameterException("pid, cid, or sid");
      }

      // show permission error if pid is invalid like we did before
      if (pkg == null) {
        throw new NoSuchPackageException();
      }

      Map params = new HashMap();
      params.put("pid", pkg.getId());
      return getStrutsDelegate().forwardParams(mapping.findForward("package"), params);
    }
  }
 public void testGuestimateHandlesNullArchId() throws Exception {
   PackageListItem pli = PackageListItem.parse("10000|1000");
   assertNull(pli.getIdThree());
   assertNull(
       PackageManager.guestimatePackageBySystem(10000L, 100L, 100L, pli.getIdThree(), null));
 }