Skip to content

Commit

Permalink
Merge pull request #11 from kids-first/fix/empty_spaces_ES
Browse files Browse the repository at this point in the history
Fix/empty spaces es
  • Loading branch information
adipaul1981 authored Nov 24, 2020
2 parents 99b3ec2 + 91a987a commit 3409156
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
31 changes: 21 additions & 10 deletions src/main/java/bio/overture/rollcall/repository/IndexRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package bio.overture.rollcall.repository;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.val;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
Expand All @@ -31,6 +33,7 @@
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions.add;
import static org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions.remove;
Expand Down Expand Up @@ -59,19 +62,27 @@ public String[] getIndices() {

@SneakyThrows
public Map<String, Set<AliasMetaData>> getAliasState() {
InputStream inputStream = this.client.getLowLevelClient()
.performRequest("GET", "/_cat/aliases?h=i,a")
InputStream inputStreamFromCatApi = this.client.getLowLevelClient()
.performRequest("GET", "/_cat/aliases?format=json")
.getEntity()
.getContent();

return new BufferedReader(new InputStreamReader(inputStream))
.lines()
.map(s -> s.split(" "))
.collect(
Collectors.groupingBy(a -> a[0],
Collectors.mapping(a-> AliasMetaData.builder(a[1]).build(), Collectors.toSet())
)
);
TypeReference<List<Map<String, String>>> typeRef = new TypeReference<>() {
};
ObjectMapper mapper = new ObjectMapper();
List<Map<String, String>> rawAliasesMetaData = mapper.readValue(inputStreamFromCatApi, typeRef);

Map<String, Set<AliasMetaData>> indexNameToAliasesMetaData = new HashMap<>();
for (Map<String, String> rawAliasMetaData : rawAliasesMetaData) {
String keyIndexName = rawAliasMetaData.get("index");
AliasMetaData aliasMetaData = AliasMetaData
.builder(rawAliasMetaData.get("alias"))
.build();
Set<AliasMetaData> newSetOfAliasesMetaData = Stream.of(aliasMetaData)
.collect(Collectors.toCollection(HashSet::new));
indexNameToAliasesMetaData.put(keyIndexName,newSetOfAliasesMetaData);
}
return indexNameToAliasesMetaData;
}

@SneakyThrows
Expand Down
38 changes: 27 additions & 11 deletions src/test/java/bio/overture/rollcall/service/AliasServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
import org.junit.Test;
import org.testcontainers.elasticsearch.ElasticsearchContainer;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -65,6 +63,7 @@ public class AliasServiceTest {
private IndexRepository repository;
private AliasService service;


@Before
@SneakyThrows
public void setUp() {
Expand Down Expand Up @@ -121,23 +120,31 @@ private static AliasMetaData firstAlias(Map<String, Set<AliasMetaData>> state1,
return listAliases(state1, index1).get(0);
}

private Set<AliasMetaData> createAliasStream (String alias){
return Stream.of(AliasMetaData.builder(alias).build()).collect(Collectors.toSet());
}

@Test
public void releaseTest() {
val requestFC1 = new AliasRequest("file_centric", "RE_foobar1", Lists.list("SD_preasa7s", "sd_ygva0e1c"));
service.release(requestFC1);
val state1 = repository.getAliasState();
//file_centric_sd_ygva0e1c_re_foobar1
//file_centric_sd_preasa7s_re_foobar1
val result1 = Stream.of(INDEX1, INDEX2).collect(Collectors.toSet());
assertThat(state1.keySet()).isEqualTo(result1);
val result1 = new HashMap<String, Set<AliasMetaData>>();
result1.put(INDEX1 , createAliasStream("file_centric"));
result1.put(INDEX2 , createAliasStream("file_centric"));
assertThat(state1).isEqualTo(result1);

val requestFC2 = new AliasRequest("file_centric", "RE_foobar2", Lists.list("SD_preasa7s"));
service.release(requestFC2);
val state2 = repository.getAliasState();
//file_centric_sd_ygva0e1c_re_foobar1
//file_centric_sd_preasa7s_re_foobar2
val result2 = Stream.of(INDEX1, INDEX3).collect(Collectors.toSet());
assertThat(state2.keySet()).isEqualTo(result2);
val result2 = new HashMap<String, Set<AliasMetaData>>();
result2.put(INDEX1 , createAliasStream("file_centric"));
result2.put(INDEX3 , createAliasStream("file_centric"));
assertThat(state2).isEqualTo(result2);

val requestPC1 = new AliasRequest("participant_centric", "RE_foobar1", Lists.list("SD_preasa7s", "sd_ygva0e1c"));
service.release(requestPC1);
Expand All @@ -146,8 +153,13 @@ public void releaseTest() {
//file_centric_sd_preasa7s_re_foobar2
//participant_centric_sd_ygva0e1c_re_foobar1
//participant_centric_sd_preasa7s_re_foobar1
val result3 = Stream.of(INDEX1, INDEX3, INDEX4, INDEX5).collect(Collectors.toSet());
assertThat(state3.keySet()).isEqualTo(result3);
val result3 = new HashMap<>();
result3.put(INDEX1 , createAliasStream("file_centric"));
result3.put(INDEX3 , createAliasStream("file_centric"));
result3.put(INDEX4 , createAliasStream("participant_centric"));
result3.put(INDEX5 , createAliasStream("participant_centric"));
assertThat(state3).isEqualTo(result3);


val requestPC2 = new AliasRequest("participant_centric", "RE_foobar2", Lists.list("SD_preasa7s"));
service.release(requestPC2);
Expand All @@ -156,8 +168,12 @@ public void releaseTest() {
//file_centric_sd_preasa7s_re_foobar2
//participant_centric_sd_ygva0e1c_re_foobar1
//participant_centric_sd_preasa7s_re_foobar2
val result4 = Stream.of(INDEX1, INDEX3, INDEX4, INDEX6).collect(Collectors.toSet());
assertThat(state4.keySet()).isEqualTo(result4);
val result4 = new HashMap<>();
result4.put(INDEX1 , createAliasStream("file_centric"));
result4.put(INDEX3 , createAliasStream("file_centric"));
result4.put(INDEX4 , createAliasStream("participant_centric"));
result4.put(INDEX6 , createAliasStream("participant_centric"));
assertThat(state4).isEqualTo(result4);
}

@Test
Expand Down

0 comments on commit 3409156

Please sign in to comment.