Search in sources :

Example 1 with BaseDoc

use of org.sonar.server.es.BaseDoc in project sonarqube by SonarSource.

the class BaseDocTest method getField_fails_if_missing_field.

@Test
public void getField_fails_if_missing_field() {
    Map<String, Object> fields = Collections.emptyMap();
    BaseDoc doc = new BaseDoc(someType, fields) {

        @Override
        public String getId() {
            return null;
        }
    };
    try {
        doc.getNullableField("a_string");
        fail();
    } catch (IllegalStateException e) {
        assertThat(e).hasMessage("Field a_string not specified in query options");
    }
}
Also used : BaseDoc(org.sonar.server.es.BaseDoc) Test(org.junit.Test)

Example 2 with BaseDoc

use of org.sonar.server.es.BaseDoc in project sonarqube by SonarSource.

the class BaseDocTest method getFields_fails_with_ISE_if_setParent_has_not_been_called_on_IndexRelationType.

@Test
public void getFields_fails_with_ISE_if_setParent_has_not_been_called_on_IndexRelationType() {
    IndexType.IndexRelationType relationType = IndexType.relation(IndexType.main(Index.withRelations("foo"), "bar"), "donut");
    BaseDoc doc = new BaseDoc(relationType) {

        @Override
        public String getId() {
            throw new UnsupportedOperationException("getId not implemented");
        }
    };
    assertThatThrownBy(() -> doc.getFields()).isInstanceOf(IllegalStateException.class).hasMessage("parent must be set on a doc associated to a IndexRelationType (see BaseDoc#setParent(String))");
}
Also used : BaseDoc(org.sonar.server.es.BaseDoc) IndexType(org.sonar.server.es.IndexType) Test(org.junit.Test)

Example 3 with BaseDoc

use of org.sonar.server.es.BaseDoc in project sonarqube by SonarSource.

the class BaseDocTest method getField.

@Test
public void getField() {
    Map<String, Object> fields = new HashMap<>();
    fields.put("a_string", "foo");
    fields.put("a_int", 42);
    fields.put("a_null", null);
    BaseDoc doc = new BaseDoc(someType, fields) {

        @Override
        public String getId() {
            return null;
        }
    };
    assertThat((String) doc.getNullableField("a_string")).isEqualTo("foo");
    assertThat((int) doc.getNullableField("a_int")).isEqualTo(42);
    assertThat((String) doc.getNullableField("a_null")).isNull();
}
Also used : BaseDoc(org.sonar.server.es.BaseDoc) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 4 with BaseDoc

use of org.sonar.server.es.BaseDoc in project sonarqube by SonarSource.

the class BaseDocTest method getFieldAsDate.

@Test
public void getFieldAsDate() {
    BaseDoc doc = new BaseDoc(someType, new HashMap<>()) {

        @Override
        public String getId() {
            return null;
        }
    };
    Date now = new Date();
    doc.setField("javaDate", now);
    assertThat(doc.getFieldAsDate("javaDate")).isEqualToIgnoringMillis(now);
    doc.setField("stringDate", EsUtils.formatDateTime(now));
    assertThat(doc.getFieldAsDate("stringDate")).isEqualToIgnoringMillis(now);
}
Also used : BaseDoc(org.sonar.server.es.BaseDoc) Date(java.util.Date) Test(org.junit.Test)

Example 5 with BaseDoc

use of org.sonar.server.es.BaseDoc in project sonarqube by SonarSource.

the class BaseDocTest method getFields_contains_join_field_and_indexType_field_when_setParent_has_been_called_on_IndexRelationType.

@Test
public void getFields_contains_join_field_and_indexType_field_when_setParent_has_been_called_on_IndexRelationType() {
    Index index = Index.withRelations("foo");
    IndexType.IndexRelationType relationType = IndexType.relation(IndexType.main(index, "bar"), "donut");
    BaseDoc doc = new BaseDoc(relationType) {

        {
            setParent("miam");
        }

        @Override
        public String getId() {
            throw new UnsupportedOperationException("getId not implemented");
        }
    };
    Map<String, Object> fields = doc.getFields();
    assertThat((Map) fields.get(index.getJoinField())).isEqualTo(ImmutableMap.of("name", relationType.getName(), "parent", "miam"));
    assertThat(fields).containsEntry("indexType", relationType.getName());
}
Also used : BaseDoc(org.sonar.server.es.BaseDoc) Index(org.sonar.server.es.Index) IndexType(org.sonar.server.es.IndexType) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)6 BaseDoc (org.sonar.server.es.BaseDoc)6 Date (java.util.Date)2 HashMap (java.util.HashMap)2 IndexType (org.sonar.server.es.IndexType)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 Index (org.sonar.server.es.Index)1