Exemplo n.º 1
0
  @Test
  public void updateProfileServiceTest() throws IOException {
    byte[] profile = baseAvroConverter.encode(ENDPOINT_PROFILE);
    RegisterProfileRequest request =
        new RegisterProfileRequest(
            application.getApplicationToken(), ENDPOINT_KEY, sdkToken, profile);

    EndpointProfileDto oldDto = profileService.registerProfile(request);

    Assert.assertEquals(
        baseAvroConverter.encodeToJson(ENDPOINT_PROFILE),
        oldDto.getClientProfileBody().replaceAll(" ", ""));

    byte[] newProfile = newAvroConverter.encode(NEW_ENDPOINT_PROFILE);
    UpdateProfileRequest updateRequest =
        new UpdateProfileRequest(
            application.getApplicationToken(),
            EndpointObjectHash.fromSha1(ENDPOINT_KEY),
            null,
            newProfile,
            newSdkToken);
    EndpointProfileDto newDto = profileService.updateProfile(updateRequest);

    Assert.assertNotNull(newDto);
    Assert.assertNotNull(newDto.getId());
    Assert.assertEquals(oldDto.getId(), newDto.getId());
    Assert.assertEquals(
        newAvroConverter.encodeToJson(NEW_ENDPOINT_PROFILE),
        newDto.getClientProfileBody().replaceAll(" ", ""));
    Assert.assertTrue(
        Arrays.equals(EndpointObjectHash.fromSha1(newProfile).getData(), newDto.getProfileHash()));
  }
  @Override
  public Long update(JUserDetails domain) {
    final long userId = Long.parseLong(domain.getId());
    Object userKey = profileService.getDao().getPrimaryKey(null, userId);
    DProfile profile = profileService.getDao().findByPrimaryKey(userId);

    profile.setDisplayName(domain.getDisplayName());
    profile.setEmail(domain.getEmail());
    profile.setPhoneNumber(domain.getPhoneNumber());
    profileService.update(profile);

    return userId;
  }
Exemplo n.º 3
0
 /**
  * Returns a client model from a ResultSet
  *
  * @param result
  * @return
  * @throws Exception
  */
 private Client getClientFromResultSet(ResultSet result) throws Exception {
   Client client = new Client();
   client.setId(result.getInt(Constants.GENERIC_ID));
   client.setUUID(result.getString(Constants.CLIENT_CLIENT_UUID));
   client.setFriendlyName(result.getString(Constants.CLIENT_FRIENDLY_NAME));
   client.setProfile(
       ProfileService.getInstance().findProfile(result.getInt(Constants.GENERIC_PROFILE_ID)));
   client.setIsActive(result.getBoolean(Constants.CLIENT_IS_ACTIVE));
   client.setActiveServerGroup(result.getInt(Constants.CLIENT_ACTIVESERVERGROUP));
   return client;
 }
  @Override
  public JUserDetails get(String parentKeyString, Long userId) {
    final Object userKey = profileService.getDao().getPrimaryKey(null, userId);
    Map userDetails = profileService.getDao().queryByAncestorKey(userKey);
    if (userDetails.isEmpty()) {
      throw new NotFoundException();
    }

    Iterator<Entry> entries = userDetails.entrySet().iterator();
    final JUserDetails body = new JUserDetails();

    Entry entry;
    while (entries.hasNext()) {
      entry = entries.next();
      LOG.debug("merging {}", entry);
      if (entry.getValue() instanceof DProfile) {
        ProfileLeaf.CONVERTER.convertDomain((DProfile) entry.getValue(), body);
      } else if (entry.getValue() instanceof DDonation) {
        convertDonation((DDonation) entry.getValue(), body);
      }
    }

    return body;
  }
Exemplo n.º 5
0
 @Test
 public void registerProfileServiceTest() throws IOException {
   byte[] profile = baseAvroConverter.encode(ENDPOINT_PROFILE);
   RegisterProfileRequest request =
       new RegisterProfileRequest(
           application.getApplicationToken(), ENDPOINT_KEY, sdkToken, profile);
   EndpointProfileDto dto = profileService.registerProfile(request);
   Assert.assertNotNull(dto);
   Assert.assertNotNull(dto.getId());
   Assert.assertTrue(Arrays.equals(ENDPOINT_KEY, dto.getEndpointKey()));
   Assert.assertTrue(
       Arrays.equals(
           EndpointObjectHash.fromSha1(ENDPOINT_KEY).getData(), dto.getEndpointKeyHash()));
   Assert.assertEquals(
       baseAvroConverter.encodeToJson(ENDPOINT_PROFILE),
       dto.getClientProfileBody().replaceAll(" ", ""));
   Assert.assertTrue(
       Arrays.equals(EndpointObjectHash.fromSha1(profile).getData(), dto.getProfileHash()));
 }
Exemplo n.º 6
0
  /**
   * Create a new client for profile There is a limit of Constants.CLIENT_CLIENTS_PER_PROFILE_LIMIT
   * If this limit is reached an exception is thrown back to the caller
   *
   * @param profileId
   * @return
   * @throws Exception
   */
  public Client add(int profileId) throws Exception {
    Client client = null;
    ArrayList<Integer> pathsToCopy = new ArrayList<Integer>();
    String clientUUID = getUniqueClientUUID();

    // get profile for profileId
    Connection sqlConnection = null;
    Profile profile = ProfileService.getInstance().findProfile(profileId);
    PreparedStatement statement = null;
    ResultSet rs = null;

    try {
      sqlConnection = sqlService.getConnection();

      // get the current count of clients
      statement =
          sqlConnection.prepareStatement(
              "SELECT COUNT("
                  + Constants.GENERIC_ID
                  + ") FROM "
                  + Constants.DB_TABLE_CLIENT
                  + " WHERE "
                  + Constants.GENERIC_PROFILE_ID
                  + "=?");
      statement.setInt(1, profileId);
      int clientCount = -1;
      rs = statement.executeQuery();
      if (rs.next()) {
        clientCount = rs.getInt(1);
      }
      statement.close();
      rs.close();

      // check count
      if (clientCount == -1) {
        throw new Exception("Error querying clients for profileId=" + profileId);
      }
      if (clientCount >= Constants.CLIENT_CLIENTS_PER_PROFILE_LIMIT) {
        throw new Exception(
            "Profile("
                + profileId
                + ") already contains 50 clients.  Please remove clients before adding new ones.");
      }

      statement =
          sqlConnection.prepareStatement(
              "INSERT INTO "
                  + Constants.DB_TABLE_CLIENT
                  + " ("
                  + Constants.CLIENT_CLIENT_UUID
                  + ", "
                  + Constants.CLIENT_IS_ACTIVE
                  + ", "
                  + Constants.CLIENT_PROFILE_ID
                  + ")"
                  + " VALUES (?, ?, ?)",
              Statement.RETURN_GENERATED_KEYS);
      statement.setString(1, clientUUID);
      statement.setBoolean(2, false);
      statement.setInt(3, profile.getId());
      statement.executeUpdate();
      rs = statement.getGeneratedKeys();
      int clientId = -1;
      if (rs.next()) {
        clientId = rs.getInt(1);
      } else {
        // something went wrong
        throw new Exception("Could not add client");
      }
      rs.close();
      statement.close();

      // adding entries into request response table for this new client for every path
      // basically a copy of what happens when a path gets created
      statement =
          sqlConnection.prepareStatement(
              "SELECT * FROM "
                  + Constants.DB_TABLE_REQUEST_RESPONSE
                  + " WHERE "
                  + Constants.GENERIC_PROFILE_ID
                  + " = ?"
                  + " AND "
                  + Constants.GENERIC_CLIENT_UUID
                  + " = ?");
      statement.setInt(1, profile.getId());
      statement.setString(2, Constants.PROFILE_CLIENT_DEFAULT_ID);
      rs = statement.executeQuery();
      while (rs.next()) {
        // collect up the pathIds we need to copy
        pathsToCopy.add(rs.getInt(Constants.REQUEST_RESPONSE_PATH_ID));
      }
      client = new Client();
      client.setIsActive(false);
      client.setUUID(clientUUID);
      client.setId(clientId);
      client.setProfile(profile);
    } catch (SQLException e) {
      throw e;
    } finally {
      try {
        if (rs != null) rs.close();
      } catch (Exception e) {
      }
      try {
        if (statement != null) statement.close();
      } catch (Exception e) {
      }
    }

    // add all of the request response items
    for (Integer pathId : pathsToCopy) {
      PathOverrideService.getInstance()
          .addPathToRequestResponseTable(profile.getId(), client.getUUID(), pathId);
    }

    return client;
  }