Skip to content

Commit

Permalink
SONAR-10515 Add method to delete entry in ALM_APP_INSTALLS
Browse files Browse the repository at this point in the history
  • Loading branch information
henryju authored and SonarTech committed Apr 27, 2018
1 parent 9cd551c commit 5ebd9b9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ public Optional<String> getInstallId(DbSession dbSession, ALM alm, String ownerI
return Optional.ofNullable(mapper.selectInstallId(alm.getId(), ownerId));
}

public void delete(DbSession dbSession, ALM alm, String ownerId) {
checkAlm(alm);
checkOwnerId(ownerId);

AlmAppInstallMapper mapper = getMapper(dbSession);
mapper.delete(alm.getId(), ownerId);
}

private static void checkAlm(@Nullable ALM alm) {
Objects.requireNonNull(alm, "alm can't be null");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ public interface AlmAppInstallMapper {

int update(@Param("almId") String almId, @Param("ownerId") String ownerId, @Param("installId") String installId, @Param("now") long now);

void delete(@Param("almId") String almId, @Param("ownerId") String ownerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
and owner_id = #{ownerId, jdbcType=VARCHAR}
</update>

<delete id="deleteByOwnerId" parameterType="Map">
<delete id="delete" parameterType="Map">
delete from alm_app_installs
where
alm_id = #{almId, jdbcType=VARCHAR}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.db.alm;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.CheckForNull;
Expand Down Expand Up @@ -107,6 +108,28 @@ public void insert() {
.hasUpdatedAt(DATE);
}

@Test
public void delete() {
when(uuidFactory.create()).thenReturn(A_UUID);
when(system2.now()).thenReturn(DATE);
underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, AN_INSTALL);

assertThatAlmAppInstall(GITHUB, A_OWNER)
.hasInstallId(AN_INSTALL)
.hasCreatedAt(DATE)
.hasUpdatedAt(DATE);

underTest.delete(dbSession, GITHUB, A_OWNER);
assertThatAlmAppInstall(GITHUB, A_OWNER).doesNotExist();
}

@Test
public void delete_doesn_t_fail() {
assertThatAlmAppInstall(GITHUB, A_OWNER).doesNotExist();

underTest.delete(dbSession, GITHUB, A_OWNER);
}

@Test
public void update() {
when(uuidFactory.create()).thenReturn(A_UUID);
Expand Down Expand Up @@ -202,16 +225,22 @@ private AlmAppInstallAssert(DbTester dbTester, DbSession dbSession, AlmAppInstal
}

private static AlmAppInstall asAlmAppInstall(DbTester dbTester, DbSession dbSession, AlmAppInstallDao.ALM alm, String ownerId) {
Map<String, Object> row = dbTester.selectFirst(
List<Map<String, Object>> rows = dbTester.select(
dbSession,
"select" +
" install_id as \"installId\", created_at as \"createdAt\", updated_at as \"updatedAt\"" +
" from alm_app_installs" +
" where alm_id='" + alm.getId() + "' and owner_id='" + ownerId + "'");
if (rows.isEmpty()) {
return null;
}
if (rows.size() > 1) {
throw new IllegalStateException("Unique index violation");
}
return new AlmAppInstall(
(String) row.get("installId"),
(Long) row.get("createdAt"),
(Long) row.get("updatedAt"));
(String) rows.get(0).get("installId"),
(Long) rows.get(0).get("createdAt"),
(Long) rows.get(0).get("updatedAt"));
}

public void doesNotExist() {
Expand Down

0 comments on commit 5ebd9b9

Please sign in to comment.