IndexRealizer implements ConfigurationRealizer<Index, IndexInfo> { @Override public RealizationResult create(Index config, InternalCache cache) { QueryService queryService = cache.getQueryService(); String indexName = config.getName(); String indexedExpression = config.getExpression(); String fromClause = config.getRegionPath(); RealizationResult realizationResult = new RealizationResult(); try { if (config.getIndexType() == IndexType.KEY) { queryService.createKeyIndex(indexName, indexedExpression, fromClause); } else { queryService.createIndex(indexName, indexedExpression, fromClause); } realizationResult.setSuccess(true); realizationResult.setMessage("Index " + indexName + " successfully created"); return realizationResult; } catch (IndexNameConflictException | IndexExistsException e) { realizationResult.setSuccess(true); realizationResult.setMessage(e.getMessage()); } catch (RegionNotFoundException e) { realizationResult.setSuccess(false); realizationResult.setMessage(e.getMessage()); } catch (RuntimeException r) { logger.error(r.getMessage(), r); realizationResult.setSuccess(false); realizationResult.setMessage(r.getMessage()); } return realizationResult; }  @Override RealizationResult create(Index config, InternalCache cache); @Override IndexInfo get(Index config, InternalCache cache); @Override RealizationResult update(Index config, InternalCache cache); @Override RealizationResult delete(Index config, InternalCache cache);  }
@Test public void createIndexThrowIndexExistsException() throws Exception { when(queryService.createKeyIndex(any(), any(), any())).thenThrow(IndexExistsException.class); index.setIndexType(IndexType.KEY); RealizationResult realizationResult = indexRealizer.create(index, cache); assertThat(realizationResult.isSuccess()).isTrue(); }
@Test public void create_succeeds_with_key() throws Exception { index.setIndexType(IndexType.KEY); RealizationResult realizationResult = indexRealizer.create(index, cache); assertSoftly(softly -> { softly.assertThat(realizationResult.isSuccess()).isTrue(); softly.assertThat(realizationResult.getMessage()).contains("testIndex successfully created"); }); verify(queryService).createKeyIndex("testIndex", "test Expression", "testRegion"); }
@Test public void create_succeeds_with_functional() throws Exception { index.setIndexType(IndexType.RANGE); RealizationResult realizationResult = indexRealizer.create(index, cache); assertSoftly(softly -> { softly.assertThat(realizationResult.isSuccess()).isTrue(); softly.assertThat(realizationResult.getMessage()).contains("testIndex successfully created"); }); verify(queryService).createIndex("testIndex", "test Expression", "testRegion"); }
@Test public void create_fails_with_queryServerice_error() throws Exception { index.setIndexType(IndexType.RANGE); doThrow(new UnsupportedOperationException("I do not support this operation")) .when(queryService).createIndex("testIndex", "test Expression", "testRegion"); RealizationResult realizationResult = indexRealizer.create(index, cache); assertSoftly(softly -> { softly.assertThat(realizationResult.isSuccess()).isFalse(); softly.assertThat(realizationResult.getMessage()).contains("I do not support this operation"); }); }