@Test void saveAverageRating() throws Exception { String managerId = "myManager"; int scale = 12; EntityReference reference = new EntityReference("foobar", EntityType.PAGE); when(this.entityReferenceSerializer.serialize(reference)).thenReturn("wiki:foobar"); SolrClient averageSolrClient = mock(SolrClient.class); when(this.solr.getClient(AverageRatingSolrCoreInitializer.DEFAULT_AVERAGE_RATING_SOLR_CORE)) .thenReturn(averageSolrClient); String filterQuery = "filter(managerId:saveRank3) AND filter(reference:wiki\\:foobar) " + "AND filter(entityType:PAGE)"; SolrQuery averageQuery = new SolrQuery().addFilterQuery(filterQuery) .setStart(0) .setRows(1) .setSort("updatedAt", SolrQuery.ORDER.asc); QueryResponse averageResponse = prepareSolrClientQueryWhenStatement(averageSolrClient, averageQuery); SolrDocumentList averageDocumentList = mock(SolrDocumentList.class); when(averageResponse.getResults()).thenReturn(averageDocumentList); when(averageDocumentList.isEmpty()).thenReturn(false); Map<String, Object> fieldMap = new HashMap<>(); fieldMap.put("id", "average1"); fieldMap.put(AverageRatingQueryField.AVERAGE_VOTE.getFieldName(), 6.0f); fieldMap.put(AverageRatingQueryField.TOTAL_VOTE.getFieldName(), 2); fieldMap.put(AverageRatingQueryField.ENTITY_REFERENCE.getFieldName(), "wiki:foobar"); fieldMap.put(AverageRatingQueryField.ENTITY_TYPE.getFieldName(), "PAGE"); fieldMap.put(AverageRatingQueryField.MANAGER_ID.getFieldName(), managerId); fieldMap.put(AverageRatingQueryField.UPDATED_AT.getFieldName(), new Date(42)); fieldMap.put(AverageRatingQueryField.SCALE.getFieldName(), scale); SolrDocument solrDocument = new SolrDocument(fieldMap); when(averageDocumentList.get(0)).thenReturn(solrDocument); DefaultAverageRating expectedModifiedAverage = new DefaultAverageRating("average1") .setAverageVote(5.5f) .setTotalVote(2) .setManagerId(managerId) .setReference(reference) .setScaleUpperBound(scale); SolrInputDocument expectedAverageInputDocument = new SolrInputDocument(); expectedAverageInputDocument.setField("id", "average1"); expectedAverageInputDocument.setField(AverageRatingQueryField.ENTITY_REFERENCE.getFieldName(), "wiki:foobar"); expectedAverageInputDocument.setField(AverageRatingQueryField.UPDATED_AT.getFieldName(), new Date(0)); expectedAverageInputDocument.setField(AverageRatingQueryField.TOTAL_VOTE.getFieldName(), 2L); expectedAverageInputDocument.setField(AverageRatingQueryField.SCALE.getFieldName(), scale); expectedAverageInputDocument.setField(AverageRatingQueryField.MANAGER_ID.getFieldName(), managerId); expectedAverageInputDocument.setField(AverageRatingQueryField.AVERAGE_VOTE.getFieldName(), 5.5); expectedAverageInputDocument.setField(AverageRatingQueryField.ENTITY_TYPE.getFieldName(), "PAGE"); when(averageSolrClient.add(any(SolrInputDocument.class))).then(invocationOnMock -> { SolrInputDocument obtainedInputDocument = invocationOnMock.getArgument(0); Date updatedAt = (Date) obtainedInputDocument.getFieldValue("updatedAt"); expectedAverageInputDocument.setField(AverageRatingQueryField.UPDATED_AT.getFieldName(), updatedAt); expectedModifiedAverage.setUpdatedAt(updatedAt); assertEquals(expectedAverageInputDocument.toString(), obtainedInputDocument.toString()); return null; }); this.averageRatingManager.saveAverageRating(new DefaultAverageRating("average1") .setReference(reference) .setAverageVote(5.5f) .setManagerId(managerId) .setScaleUpperBound(scale) .setTotalVote(2) .setUpdatedAt(new Date(42))); verify(averageSolrClient).add(any(SolrInputDocument.class)); verify(averageSolrClient).commit(); }
@Override protected void saveAverageRating(AverageRating averageRating) throws RatingsException { try { this.getAverageRatingSolrClient().add(this.getInputDocumentFromAverageRating(averageRating)); this.getAverageRatingSolrClient().commit(); } catch (SolrException | SolrServerException | IOException e) { throw new RatingsException(String.format("Error when trying to save average rating [%s]", averageRating), e); } }
SolrAverageRatingManager extends AbstractAverageRatingManager { @Override protected void saveAverageRating(AverageRating averageRating) throws RatingsException { try { this.getAverageRatingSolrClient().add(this.getInputDocumentFromAverageRating(averageRating)); this.getAverageRatingSolrClient().commit(); } catch (SolrException | SolrServerException | IOException e) { throw new RatingsException(String.format("Error when trying to save average rating [%s]", averageRating), e); } } }
SolrAverageRatingManager extends AbstractAverageRatingManager { @Override protected void saveAverageRating(AverageRating averageRating) throws RatingsException { try { this.getAverageRatingSolrClient().add(this.getInputDocumentFromAverageRating(averageRating)); this.getAverageRatingSolrClient().commit(); } catch (SolrException | SolrServerException | IOException e) { throw new RatingsException(String.format("Error when trying to save average rating [%s]", averageRating), e); } }  }
SolrAverageRatingManager extends AbstractAverageRatingManager { @Override protected void saveAverageRating(AverageRating averageRating) throws RatingsException { try { this.getAverageRatingSolrClient().add(this.getInputDocumentFromAverageRating(averageRating)); this.getAverageRatingSolrClient().commit(); } catch (SolrException | SolrServerException | IOException e) { throw new RatingsException(String.format("Error when trying to save average rating [%s]", averageRating), e); } }  @Override AverageRating getAverageRating(EntityReference entityReference); }
SolrAverageRatingManager extends AbstractAverageRatingManager { @Override protected void saveAverageRating(AverageRating averageRating) throws RatingsException { try { this.getAverageRatingSolrClient().add(this.getInputDocumentFromAverageRating(averageRating)); this.getAverageRatingSolrClient().commit(); } catch (SolrException | SolrServerException | IOException e) { throw new RatingsException(String.format("Error when trying to save average rating [%s]", averageRating), e); } }  @Override AverageRating getAverageRating(EntityReference entityReference);  }