Changeset 616


Ignore:
Timestamp:
Apr 3, 2012, 2:19:46 PM (5 years ago)
Author:
freek.de.bruijn@…
Message:

Added testSearchConceptsInTag & testGetConceptsByLabel, removed some unnecessary code and added some comments.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/code/conceptwiki/service/service-impl/src/test/java/nl/nbic/conceptwiki/service/ConceptServiceImplUnitTest.java

    r615 r616  
    77import static org.junit.Assert.assertNull;
    88import static org.junit.Assert.assertTrue;
     9import static org.mockito.Mockito.doThrow;
    910import static org.mockito.Mockito.when;
    10 import static org.mockito.Mockito.doThrow;
    1111
    1212import com.google.common.collect.Iterables;
     
    2222import nl.nbic.conceptwiki.common.api.Language;
    2323
    24 import nl.nbic.conceptwiki.common.api.Label;
    2524import nl.nbic.conceptwiki.common.api.SearchResult;
    2625import nl.nbic.conceptwiki.common.impl.ConceptImpl;
     
    3534import nl.nbic.conceptwiki.storage.api.ConceptSearchService;
    3635import nl.nbic.conceptwiki.storage.api.ConceptStorageService;
    37 import org.junit.Ignore;
    3836
    3937import org.junit.Test;
     
    4644
    4745import com.google.common.collect.ImmutableSet;
    48 import java.util.HashSet;
    4946import java.util.Set;
    5047import junit.framework.Assert;
     
    8885
    8986    @Test
     87    /**
     88     * Test searchConcepts, getting no search results because the query is the empty string.
     89     */
    9090    public void testSearchConceptsEmptyQuery() {
    9191        // no setup
     
    100100
    101101    @Test
     102    /**
     103     * Test searchConcepts, getting no search results because the limit is invalid.
     104     */
    102105    public void testSearchConceptsInvalidLimit() {
    103106        // no setup
     
    112115
    113116    @Test
     117    /**
     118     * Test searchConcepts, getting no search results because the mock search service has no test data.
     119     */
    114120    public void testSearchConceptsNoResults() {
    115121        // no setup
    116122
    117123        // exercise
    118         final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE, DEFAULT_LIMIT);
     124        final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE,
     125                        DEFAULT_LIMIT);
    119126
    120127        // verify
     
    124131
    125132    @Test
     133    /**
     134     * Test searchConcepts, getting one search result.
     135     */
    126136    public void testSearchConceptsSingleResult() {
    127137        // setup
    128         final List<SearchResult> searchResults = Arrays.asList((SearchResult) new SearchResultImpl("malaria", "123456"));
    129         when(mockSearchService.searchConcepts(Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(searchResults);
    130 
    131         // exercise
    132         final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE, DEFAULT_LIMIT);
     138        final List<SearchResult> searchResults = Arrays.asList((SearchResult) new SearchResultImpl("malaria", "1234"));
     139        when(mockSearchService.searchConcepts(Mockito.anyString(), Mockito.anyInt(),
     140                        Mockito.anyInt())).thenReturn(searchResults);
     141
     142        // exercise
     143        final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE,
     144                        DEFAULT_LIMIT);
    133145
    134146        // verify
     
    138150
    139151    @Test
     152    /**
     153     * Test searchConcepts, getting one search result with a label.
     154     */
    140155    public void testSearchConceptsSingleResultWithLabel() {
    141156        // setup
     
    143158        final List<SearchResult> searchResults = new ArrayList<SearchResult>();
    144159        searchResults.add(new SearchResultImpl("malaria", uuid));
    145         when(mockSearchService.searchConcepts(Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(searchResults);
     160        when(mockSearchService.searchConcepts(Mockito.anyString(), Mockito.anyInt(),
     161                        Mockito.anyInt())).thenReturn(searchResults);
    146162        // ...
    147163        final ImmutableSet<Label> testLabels = ImmutableSet.of(PREF_LABEL_A);
     
    149165
    150166        // exercise
    151         final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE, DEFAULT_LIMIT);
     167        final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE,
     168                        DEFAULT_LIMIT);
    152169
    153170        // verify
     
    158175
    159176    @Test
     177    /**
     178     * Test searchConcepts, getting multiple search results.
     179     */
    160180    public void testSearchConceptsMultipleResults() {
    161181        // setup
    162         final List<SearchResult> searchResults = Arrays.asList((SearchResult) new SearchResultImpl("malaria", "123456"),
     182        final List<SearchResult> searchResults = Arrays.asList((SearchResult) new SearchResultImpl("malaria", "1234"),
    163183                (SearchResult) new SearchResultImpl("malaria mosquito", "789abc"));
    164         when(mockSearchService.searchConcepts(Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(searchResults);
    165 
    166         // exercise
    167         final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE, DEFAULT_LIMIT);
     184        when(mockSearchService.searchConcepts(Mockito.anyString(), Mockito.anyInt(),
     185                        Mockito.anyInt())).thenReturn(searchResults);
     186
     187        // exercise
     188        final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE,
     189                        DEFAULT_LIMIT);
    168190
    169191        // verify
     
    172194    }
    173195
     196    @Test
     197    /**
     198     * Test searchConceptsInTag in a normal scenario, getting two search results.
     199     */
     200    public void testSearchConceptsInTag() {
     201        // setup
     202        final String uuid = "123456";
     203        final String query = "malaria";
     204                final List<SearchResult> searchResults = Arrays.asList((SearchResult) new SearchResultImpl(query, "123456"),
     205                (SearchResult) new SearchResultImpl("malaria mosquito", "789abc"));
     206                Mockito.when(mockSearchService.searchConcepts(Mockito.eq("tags:" + uuid + " " + query), Mockito.eq(DEFAULT_LIMIT),
     207                        Mockito.eq(0))).thenReturn(searchResults);
     208
     209        // exercise
     210        final List<SearchResult> results = conceptService.searchConceptsInTag(query, uuid, QueryScopeImpl.LITE_SCOPE,
     211                        DEFAULT_LIMIT);
     212
     213        // verify
     214        assertNotNull(results);
     215        assertEquals(2, results.size());
     216    }
     217
     218    @Test
     219    /**
     220     * Test getConceptsByLabel, getting one search result.
     221     */
     222    public void testGetConceptsByLabel() {
     223        // setup
     224        final List<Concept> searchResults = Arrays.asList((Concept) new ConceptImpl.Builder().build());
     225        when(mockStorageService.getConceptsByLabel(Mockito.anyString(), Mockito.eq(QueryScopeImpl.LITE_SCOPE),
     226                        Mockito.anyInt(), Mockito.eq(DEFAULT_LANGUAGE))).thenReturn(searchResults);
     227
     228        // exercise
     229        final List<Concept> results = conceptService.getConceptsByLabel("malaria", QueryScopeImpl.LITE_SCOPE,
     230                        DEFAULT_LIMIT);
     231
     232        // verify
     233        assertNotNull(results);
     234        assertEquals(1, results.size());
     235    }
     236   
    174237    /*
    175238     * Four tests for ConceptServiceImpl.createConcept(). We expect the
    176      * following to be true. <ul> <li>A successfull call returns a new Concept
     239     * following to be true. <ul> <li>A successful call returns a new Concept
    177240     * with a UUID and all the initialized fields <li>An exception from one of
    178241     * the underlying services will fall through to the calling method. </ul>
     
    202265
    203266        // exercise
    204         final Concept result = conceptService.createConcept(concept, DEFAULT_BRANCH);
     267        conceptService.createConcept(concept, DEFAULT_BRANCH);
    205268    }
    206269
     
    212275
    213276        // exercise
    214         final Concept result = conceptService.createConcept(concept, DEFAULT_BRANCH);
     277        conceptService.createConcept(concept, DEFAULT_BRANCH);
    215278    }
    216279
     
    222285
    223286        // exercise
    224         final Concept result = conceptService.createConcept(concept, DEFAULT_BRANCH);
     287        conceptService.createConcept(concept, DEFAULT_BRANCH);
    225288    }
    226289
Note: See TracChangeset for help on using the changeset viewer.