@Test void getAverageRatingExisting() throws Exception { when(this.solr.getClient(AverageRatingSolrCoreInitializer.DEFAULT_AVERAGE_RATING_SOLR_CORE)) .thenReturn(this.solrClient); String managerId = "averageId2"; when(this.ratingsManager.getIdentifier()).thenReturn(managerId); EntityReference reference = new EntityReference("xwiki:Something", EntityType.PAGE); when(this.entityReferenceSerializer.serialize(reference)).thenReturn("xwiki:Something"); String filterQuery = "filter(managerId:averageId2) AND filter(reference:xwiki\\:Something) " + "AND filter(entityType:PAGE)"; SolrQuery solrQuery = new SolrQuery().addFilterQuery(filterQuery) .setStart(0) .setRows(1) .setSort("updatedAt", SolrQuery.ORDER.asc); QueryResponse response = prepareSolrClientQueryWhenStatement(this.solrClient, solrQuery); when(response.getResults()).thenReturn(this.documentList); when(this.documentList.isEmpty()).thenReturn(false); Map<String, Object> fieldMap = new HashMap<>(); fieldMap.put("id", "average1"); fieldMap.put(AverageRatingQueryField.AVERAGE_VOTE.getFieldName(), 2.341f); fieldMap.put(AverageRatingQueryField.TOTAL_VOTE.getFieldName(), 242); fieldMap.put(AverageRatingQueryField.ENTITY_REFERENCE.getFieldName(), "xwiki:Something"); 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(), 12); SolrDocument solrDocument = new SolrDocument(fieldMap); when(this.documentList.get(0)).thenReturn(solrDocument); AverageRating averageRating = this.averageRatingManager.getAverageRating(reference); AverageRating expectedRating = new DefaultAverageRating("average1") .setAverageVote(2.341f) .setTotalVote(242) .setUpdatedAt(new Date(42)) .setManagerId(managerId) .setScaleUpperBound(12) .setReference(reference); assertEquals(expectedRating, averageRating); }
@Override public AverageRating getAverageRating(EntityReference entityReference) throws RatingsException { SolrQuery solrQuery = new SolrQuery() .addFilterQuery(String.format("filter(%s:%s) AND filter(%s:%s) AND filter(%s:%s)", AverageRatingQueryField.MANAGER_ID.getFieldName(), solrUtils.toFilterQueryString(this.getIdentifier()), AverageRatingQueryField.ENTITY_REFERENCE.getFieldName(), solrUtils.toFilterQueryString(this.entityReferenceSerializer.serialize(entityReference)), AverageRatingQueryField.ENTITY_TYPE.getFieldName(), solrUtils.toFilterQueryString(entityReference.getType()))) .setStart(0) .setRows(1) .setSort(AverageRatingQueryField.UPDATED_AT.getFieldName(), this.getOrder(true)); try { QueryResponse query = this.getAverageRatingSolrClient().query(solrQuery); AverageRating result; if (!query.getResults().isEmpty()) { SolrDocument solrDocument = query.getResults().get(0); result = new DefaultAverageRating(solrUtils.getId(solrDocument)) .setManagerId(solrUtils.get(AverageRatingQueryField.MANAGER_ID.getFieldName(), solrDocument)) .setAverageVote(solrUtils.get(AverageRatingQueryField.AVERAGE_VOTE.getFieldName(), solrDocument)) .setReference(entityReference) .setTotalVote(solrUtils.get(AverageRatingQueryField.TOTAL_VOTE.getFieldName(), solrDocument)) .setScaleUpperBound(solrUtils.get(AverageRatingQueryField.SCALE.getFieldName(), solrDocument)) .setUpdatedAt(solrUtils.get(AverageRatingQueryField.UPDATED_AT.getFieldName(), solrDocument)); } else { return this.createAverageRating(entityReference, UUID.randomUUID().toString()); } return result; } catch (SolrServerException | IOException | SolrException e) { throw new RatingsException("Error while trying to get average ranking value.", e); } }
SolrAverageRatingManager extends AbstractAverageRatingManager { @Override public AverageRating getAverageRating(EntityReference entityReference) throws RatingsException { SolrQuery solrQuery = new SolrQuery() .addFilterQuery(String.format("filter(%s:%s) AND filter(%s:%s) AND filter(%s:%s)", AverageRatingQueryField.MANAGER_ID.getFieldName(), solrUtils.toFilterQueryString(this.getIdentifier()), AverageRatingQueryField.ENTITY_REFERENCE.getFieldName(), solrUtils.toFilterQueryString(this.entityReferenceSerializer.serialize(entityReference)), AverageRatingQueryField.ENTITY_TYPE.getFieldName(), solrUtils.toFilterQueryString(entityReference.getType()))) .setStart(0) .setRows(1) .setSort(AverageRatingQueryField.UPDATED_AT.getFieldName(), this.getOrder(true)); try { QueryResponse query = this.getAverageRatingSolrClient().query(solrQuery); AverageRating result; if (!query.getResults().isEmpty()) { SolrDocument solrDocument = query.getResults().get(0); result = new DefaultAverageRating(solrUtils.getId(solrDocument)) .setManagerId(solrUtils.get(AverageRatingQueryField.MANAGER_ID.getFieldName(), solrDocument)) .setAverageVote(solrUtils.get(AverageRatingQueryField.AVERAGE_VOTE.getFieldName(), solrDocument)) .setReference(entityReference) .setTotalVote(solrUtils.get(AverageRatingQueryField.TOTAL_VOTE.getFieldName(), solrDocument)) .setScaleUpperBound(solrUtils.get(AverageRatingQueryField.SCALE.getFieldName(), solrDocument)) .setUpdatedAt(solrUtils.get(AverageRatingQueryField.UPDATED_AT.getFieldName(), solrDocument)); } else { return this.createAverageRating(entityReference, UUID.randomUUID().toString()); } return result; } catch (SolrServerException | IOException | SolrException e) { throw new RatingsException("Error while trying to get average ranking value.", e); } } }
SolrAverageRatingManager extends AbstractAverageRatingManager { @Override public AverageRating getAverageRating(EntityReference entityReference) throws RatingsException { SolrQuery solrQuery = new SolrQuery() .addFilterQuery(String.format("filter(%s:%s) AND filter(%s:%s) AND filter(%s:%s)", AverageRatingQueryField.MANAGER_ID.getFieldName(), solrUtils.toFilterQueryString(this.getIdentifier()), AverageRatingQueryField.ENTITY_REFERENCE.getFieldName(), solrUtils.toFilterQueryString(this.entityReferenceSerializer.serialize(entityReference)), AverageRatingQueryField.ENTITY_TYPE.getFieldName(), solrUtils.toFilterQueryString(entityReference.getType()))) .setStart(0) .setRows(1) .setSort(AverageRatingQueryField.UPDATED_AT.getFieldName(), this.getOrder(true)); try { QueryResponse query = this.getAverageRatingSolrClient().query(solrQuery); AverageRating result; if (!query.getResults().isEmpty()) { SolrDocument solrDocument = query.getResults().get(0); result = new DefaultAverageRating(solrUtils.getId(solrDocument)) .setManagerId(solrUtils.get(AverageRatingQueryField.MANAGER_ID.getFieldName(), solrDocument)) .setAverageVote(solrUtils.get(AverageRatingQueryField.AVERAGE_VOTE.getFieldName(), solrDocument)) .setReference(entityReference) .setTotalVote(solrUtils.get(AverageRatingQueryField.TOTAL_VOTE.getFieldName(), solrDocument)) .setScaleUpperBound(solrUtils.get(AverageRatingQueryField.SCALE.getFieldName(), solrDocument)) .setUpdatedAt(solrUtils.get(AverageRatingQueryField.UPDATED_AT.getFieldName(), solrDocument)); } else { return this.createAverageRating(entityReference, UUID.randomUUID().toString()); } return result; } catch (SolrServerException | IOException | SolrException e) { throw new RatingsException("Error while trying to get average ranking value.", e); } }  }
SolrAverageRatingManager extends AbstractAverageRatingManager { @Override public AverageRating getAverageRating(EntityReference entityReference) throws RatingsException { SolrQuery solrQuery = new SolrQuery() .addFilterQuery(String.format("filter(%s:%s) AND filter(%s:%s) AND filter(%s:%s)", AverageRatingQueryField.MANAGER_ID.getFieldName(), solrUtils.toFilterQueryString(this.getIdentifier()), AverageRatingQueryField.ENTITY_REFERENCE.getFieldName(), solrUtils.toFilterQueryString(this.entityReferenceSerializer.serialize(entityReference)), AverageRatingQueryField.ENTITY_TYPE.getFieldName(), solrUtils.toFilterQueryString(entityReference.getType()))) .setStart(0) .setRows(1) .setSort(AverageRatingQueryField.UPDATED_AT.getFieldName(), this.getOrder(true)); try { QueryResponse query = this.getAverageRatingSolrClient().query(solrQuery); AverageRating result; if (!query.getResults().isEmpty()) { SolrDocument solrDocument = query.getResults().get(0); result = new DefaultAverageRating(solrUtils.getId(solrDocument)) .setManagerId(solrUtils.get(AverageRatingQueryField.MANAGER_ID.getFieldName(), solrDocument)) .setAverageVote(solrUtils.get(AverageRatingQueryField.AVERAGE_VOTE.getFieldName(), solrDocument)) .setReference(entityReference) .setTotalVote(solrUtils.get(AverageRatingQueryField.TOTAL_VOTE.getFieldName(), solrDocument)) .setScaleUpperBound(solrUtils.get(AverageRatingQueryField.SCALE.getFieldName(), solrDocument)) .setUpdatedAt(solrUtils.get(AverageRatingQueryField.UPDATED_AT.getFieldName(), solrDocument)); } else { return this.createAverageRating(entityReference, UUID.randomUUID().toString()); } return result; } catch (SolrServerException | IOException | SolrException e) { throw new RatingsException("Error while trying to get average ranking value.", e); } }  @Override AverageRating getAverageRating(EntityReference entityReference); }
SolrAverageRatingManager extends AbstractAverageRatingManager { @Override public AverageRating getAverageRating(EntityReference entityReference) throws RatingsException { SolrQuery solrQuery = new SolrQuery() .addFilterQuery(String.format("filter(%s:%s) AND filter(%s:%s) AND filter(%s:%s)", AverageRatingQueryField.MANAGER_ID.getFieldName(), solrUtils.toFilterQueryString(this.getIdentifier()), AverageRatingQueryField.ENTITY_REFERENCE.getFieldName(), solrUtils.toFilterQueryString(this.entityReferenceSerializer.serialize(entityReference)), AverageRatingQueryField.ENTITY_TYPE.getFieldName(), solrUtils.toFilterQueryString(entityReference.getType()))) .setStart(0) .setRows(1) .setSort(AverageRatingQueryField.UPDATED_AT.getFieldName(), this.getOrder(true)); try { QueryResponse query = this.getAverageRatingSolrClient().query(solrQuery); AverageRating result; if (!query.getResults().isEmpty()) { SolrDocument solrDocument = query.getResults().get(0); result = new DefaultAverageRating(solrUtils.getId(solrDocument)) .setManagerId(solrUtils.get(AverageRatingQueryField.MANAGER_ID.getFieldName(), solrDocument)) .setAverageVote(solrUtils.get(AverageRatingQueryField.AVERAGE_VOTE.getFieldName(), solrDocument)) .setReference(entityReference) .setTotalVote(solrUtils.get(AverageRatingQueryField.TOTAL_VOTE.getFieldName(), solrDocument)) .setScaleUpperBound(solrUtils.get(AverageRatingQueryField.SCALE.getFieldName(), solrDocument)) .setUpdatedAt(solrUtils.get(AverageRatingQueryField.UPDATED_AT.getFieldName(), solrDocument)); } else { return this.createAverageRating(entityReference, UUID.randomUUID().toString()); } return result; } catch (SolrServerException | IOException | SolrException e) { throw new RatingsException("Error while trying to get average ranking value.", e); } }  @Override AverageRating getAverageRating(EntityReference entityReference);  }