Changeset 615


Ignore:
Timestamp:
Apr 2, 2012, 9:52:57 AM (6 years ago)
Author:
david.vanenckevort@…
Message:
  • Added update, delete and getConcept test methods
File:
1 edited

Legend:

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

    r612 r615  
    55import static org.junit.Assert.assertFalse;
    66import static org.junit.Assert.assertNotNull;
     7import static org.junit.Assert.assertNull;
    78import static org.junit.Assert.assertTrue;
    89import static org.mockito.Mockito.when;
     
    4546
    4647import com.google.common.collect.ImmutableSet;
    47 
     48import java.util.HashSet;
     49import java.util.Set;
     50import junit.framework.Assert;
     51import nl.nbic.conceptwiki.common.api.Changeset.Action;
     52import nl.nbic.conceptwiki.common.impl.ChangesetImpl;
     53import nl.nbic.conceptwiki.common.impl.ChangesetImpl.ActionImpl;
    4854
    4955/**
    50  * Implementation of the AbstractConceptServiceUnitTest tests.
     56 * Unit tests for the ConceptServiceImpl.
     57 *
    5158 * @author <a href="mailto:kees.burger@nbic.nl">Kees Burger</a>
    5259 * @author <a href="mailto:freek.de.bruijn@nbic.nl">Freek de Bruijn</a>
     60 * @author <a href="mailto:david.van.enckevort@nbic.nl">David van Enckevort</a>
    5361 */
    5462@RunWith(MockitoJUnitRunner.class)
    5563public class ConceptServiceImplUnitTest {
     64
    5665    public static final String DEFAULT_LABEL = "Malaria";
    5766    public static final Language DEFAULT_LANGUAGE = new LanguageImpl("en");
    58         private static final int DEFAULT_LIMIT = 10;
     67    private static final int DEFAULT_LIMIT = 10;
    5968    /*
    6069     * The branch id used during the tests.
    61     */
     70     */
    6271    private static final int DEFAULT_BRANCH = 1;
    63 
    64         /**
    65          * System under test.
    66          */
    67         @InjectMocks
    68         private ConceptServiceImpl conceptService;
    69        
    70         /**
    71          * Collaborators.
    72          */
    73         @Spy
     72    /**
     73     * System under test.
     74     */
     75    @InjectMocks
     76    private ConceptServiceImpl conceptService;
     77    /**
     78     * Collaborators.
     79     */
     80    @Spy
    7481    private ValidationUtility validationUtility = new ValidationUtilityImpl();
    75         @Mock
     82    @Mock
    7683    private ConceptStorageService mockStorageService;
    77         @Mock
     84    @Mock
    7885    private ConceptSearchService mockSearchService;
    79         @Mock
     86    @Mock
    8087    private HistoryService mockHistoryService;
    81        
    82         @Test
    83         public void testSearchConceptsEmptyQuery() {
    84                 // no setup
    85 
    86                 // exercise
    87                 final List<SearchResult> results = conceptService.searchConcepts("", QueryScopeImpl.LITE_SCOPE, DEFAULT_LIMIT);
    88                
    89                 // verify
    90                 assertNotNull(results);
    91                 assertTrue(results.isEmpty());
    92         }
    93    
    94         @Test
    95         public void testSearchConceptsInvalidLimit() {
    96                 // no setup
    97 
    98                 // exercise
    99                 final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE, -1);
    100                
    101                 // verify
    102                 assertNotNull(results);
    103                 assertTrue(results.isEmpty());
    104         }
    105    
    106         @Test
    107         public void testSearchConceptsNoResults() {
    108                 // no setup
    109 
    110                 // exercise
    111                 final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE, DEFAULT_LIMIT);
    112                
    113                 // verify
    114                 assertNotNull(results);
    115                 assertTrue(results.isEmpty());
    116         }
    117 
    118         @Test
    119         public void testSearchConceptsSingleResult() {
    120                 // setup
    121                 final List<SearchResult> searchResults = Arrays.asList((SearchResult) new SearchResultImpl("malaria", "123456"));
    122                 when(mockSearchService.searchConcepts(Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(searchResults);
    123 
    124                 // exercise
    125                 final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE, DEFAULT_LIMIT);
    126                
    127                 // verify
    128                 assertNotNull(results);
    129                 assertFalse(results.isEmpty());
    130         }
    131        
    132         @Test
    133         public void testSearchConceptsSingleResultWithLabel() {
    134                 // setup
    135                 final String uuid = "123456";
    136                 final List<SearchResult> searchResults = new ArrayList<SearchResult>();
    137                 searchResults.add(new SearchResultImpl("malaria", uuid));
    138                 when(mockSearchService.searchConcepts(Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(searchResults);
    139                 // ...
    140                 final ImmutableSet<Label> testLabels = ImmutableSet.of(PREF_LABEL_A);
    141                 when(mockStorageService.getConcept(uuid)).thenReturn(new ConceptImpl.Builder().labels(testLabels).build());
    142 
    143                 // exercise
    144                 final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE, DEFAULT_LIMIT);
    145                
    146                 // verify
    147                 assertNotNull(results);
    148                 assertEquals(1, results.size());
    149                 assertEquals(testLabels, results.get(0).getLabels());
    150         }
    151 
    152         @Test
    153         public void testSearchConceptsMultipleResults() {
    154                 // setup
    155                 final List<SearchResult> searchResults = Arrays.asList((SearchResult) new SearchResultImpl("malaria", "123456"),
    156                                 (SearchResult) new SearchResultImpl("malaria mosquito", "789abc"));
    157                 when(mockSearchService.searchConcepts(Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(searchResults);
    158 
    159                 // exercise
    160                 final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE, DEFAULT_LIMIT);
    161                
    162                 // verify
    163                 assertNotNull(results);
    164                 assertEquals(2, results.size());
    165         }
     88
     89    @Test
     90    public void testSearchConceptsEmptyQuery() {
     91        // no setup
     92
     93        // exercise
     94        final List<SearchResult> results = conceptService.searchConcepts("", QueryScopeImpl.LITE_SCOPE, DEFAULT_LIMIT);
     95
     96        // verify
     97        assertNotNull(results);
     98        assertTrue(results.isEmpty());
     99    }
     100
     101    @Test
     102    public void testSearchConceptsInvalidLimit() {
     103        // no setup
     104
     105        // exercise
     106        final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE, -1);
     107
     108        // verify
     109        assertNotNull(results);
     110        assertTrue(results.isEmpty());
     111    }
     112
     113    @Test
     114    public void testSearchConceptsNoResults() {
     115        // no setup
     116
     117        // exercise
     118        final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE, DEFAULT_LIMIT);
     119
     120        // verify
     121        assertNotNull(results);
     122        assertTrue(results.isEmpty());
     123    }
     124
     125    @Test
     126    public void testSearchConceptsSingleResult() {
     127        // 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);
     133
     134        // verify
     135        assertNotNull(results);
     136        assertFalse(results.isEmpty());
     137    }
     138
     139    @Test
     140    public void testSearchConceptsSingleResultWithLabel() {
     141        // setup
     142        final String uuid = "123456";
     143        final List<SearchResult> searchResults = new ArrayList<SearchResult>();
     144        searchResults.add(new SearchResultImpl("malaria", uuid));
     145        when(mockSearchService.searchConcepts(Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(searchResults);
     146        // ...
     147        final ImmutableSet<Label> testLabels = ImmutableSet.of(PREF_LABEL_A);
     148        when(mockStorageService.getConcept(uuid)).thenReturn(new ConceptImpl.Builder().labels(testLabels).build());
     149
     150        // exercise
     151        final List<SearchResult> results = conceptService.searchConcepts("malaria", QueryScopeImpl.LITE_SCOPE, DEFAULT_LIMIT);
     152
     153        // verify
     154        assertNotNull(results);
     155        assertEquals(1, results.size());
     156        assertEquals(testLabels, results.get(0).getLabels());
     157    }
     158
     159    @Test
     160    public void testSearchConceptsMultipleResults() {
     161        // setup
     162        final List<SearchResult> searchResults = Arrays.asList((SearchResult) new SearchResultImpl("malaria", "123456"),
     163                (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);
     168
     169        // verify
     170        assertNotNull(results);
     171        assertEquals(2, results.size());
     172    }
    166173
    167174    /*
    168      * Four tests for ConceptServiceImpl.createConcept(). We expect the following to be true.
    169      * <ul>
    170      * <li>A successfull call returns a new Concept with a UUID and all the initialized fields
    171      * <li>An exception from one of the underlying services will fall through to the calling method.
    172      * </ul>
     175     * Four tests for ConceptServiceImpl.createConcept(). We expect the
     176     * following to be true. <ul> <li>A successfull call returns a new Concept
     177     * with a UUID and all the initialized fields <li>An exception from one of
     178     * the underlying services will fall through to the calling method. </ul>
    173179     */
    174180    @Test
     
    178184        final Concept mockedResult = new ConceptImpl.Builder().labels(Sets.newHashSet(label)).uuid(UUID.randomUUID().toString()).build();
    179185        when(mockStorageService.createConcept(Mockito.any(Concept.class), Mockito.anyInt())).thenReturn(mockedResult);
    180        
    181         // exercise
    182         final Concept result = conceptService.createConcept(concept, DEFAULT_BRANCH);
    183        
     186
     187        // exercise
     188        final Concept result = conceptService.createConcept(concept, DEFAULT_BRANCH);
     189
    184190        // verify
    185191        assertNotNull(result);
     
    188194        assertEquals(LabelType.PREFERRED, Iterables.getOnlyElement(result.getLabels()).getType());
    189195    }
    190    
     196
    191197    @Test(expected = RuntimeException.class)
    192198    public void testCreateConceptFailureOfStorageService() {
     
    194200        final Concept concept = new ConceptImpl.Builder().labels(Sets.newHashSet(label)).build();
    195201        when(mockStorageService.createConcept(Mockito.any(Concept.class), Mockito.anyInt())).thenThrow(new RuntimeException());
    196        
     202
    197203        // exercise
    198204        final Concept result = conceptService.createConcept(concept, DEFAULT_BRANCH);
     
    204210        final Concept concept = new ConceptImpl.Builder().labels(Sets.newHashSet(label)).build();
    205211        doThrow(new RuntimeException()).when(mockHistoryService).storeRevision(Mockito.anyString(), Mockito.any(Changeset.class));
    206        
     212
    207213        // exercise
    208214        final Concept result = conceptService.createConcept(concept, DEFAULT_BRANCH);
     
    214220        final Concept concept = new ConceptImpl.Builder().labels(Sets.newHashSet(label)).build();
    215221        doThrow(new RuntimeException()).when(mockSearchService).indexConcept(Mockito.any(Concept.class));
    216        
    217         // exercise
    218         final Concept result = conceptService.createConcept(concept, DEFAULT_BRANCH);
    219     }
    220 
    221     @Test
    222     // TODO Enable when implementation is fixed.
    223     @Ignore("Implementation doesn't support the history service yet.")
    224     public void deleteConcept() {
     222
     223        // exercise
     224        final Concept result = conceptService.createConcept(concept, DEFAULT_BRANCH);
     225    }
     226
     227    /*
     228     * Test for a successful deletion of the concept. This test will currently
     229     * fail, because it verifies that the history service is invoked, while the
     230     * current implementation doesn't.
     231     */
     232    @Test
     233    public void testDeleteConcept() {
    225234        when(mockStorageService.isExistingConcept(Mockito.anyString())).thenReturn(true);
    226235        final String uuid = UUID.randomUUID().toString();
    227        
     236
    228237        conceptService.deleteConcept(uuid, "Deletion of a valid concept.", DEFAULT_BRANCH);
    229        
     238
    230239        Mockito.verify(mockStorageService).markAsDeleted(Mockito.eq(uuid), Mockito.anyString(), Mockito.anyInt());
    231240        Mockito.verify(mockHistoryService).storeRevision(Mockito.eq(uuid), Mockito.any(Changeset.class));
    232241    }
    233    
     242
    234243    @Test(expected = IllegalArgumentException.class)
    235     public void deleteNonExistantConcept() {
     244    public void testDeleteNonExistantConcept() {
    236245        when(mockStorageService.isExistingConcept(Mockito.anyString())).thenReturn(false);
    237246        final String uuid = UUID.randomUUID().toString();
     247
     248        conceptService.deleteConcept(uuid, "Deletion of a non-existant object.", DEFAULT_BRANCH);
     249    }
     250
     251    @Test(expected = IllegalArgumentException.class)
     252    public void testDeleteInvalidConcept() {
     253        conceptService.deleteConcept("Invalid UUID", "Deletion of an invalid UUID", DEFAULT_BRANCH);
     254    }
     255
     256    @Test
     257    public void testGetNonExistingConcept() {
     258        final String uuid = UUID.randomUUID().toString();
     259        when(mockStorageService.getConcept(uuid, DEFAULT_BRANCH)).thenReturn(null);
     260        final Concept result = conceptService.getConcept(uuid, DEFAULT_BRANCH);
     261
     262        assertNull(result);
     263    }
     264
     265    @Test
     266    public void testGetConcept() {
     267        final String uuid = UUID.randomUUID().toString();
     268        final Label label = new LabelImpl(LabelType.PREFERRED, DEFAULT_LABEL, DEFAULT_LANGUAGE);
     269        final Concept concept = new ConceptImpl.Builder().labels(Sets.newHashSet(label)).uuid(uuid).build();
     270        when(mockStorageService.getConcept(uuid, DEFAULT_BRANCH)).thenReturn(concept);
     271
     272        final Concept result = conceptService.getConcept(uuid, DEFAULT_BRANCH);
     273
     274        assertNotNull(result);
     275        Assert.assertEquals(uuid, result.getUuid());
     276        Assert.assertEquals(label, Iterables.getOnlyElement(result.getLabels()));
     277    }
     278
     279    @Test
     280    public void testUpdateConcept() {
     281        final String uuid = UUID.randomUUID().toString();
     282        final Label label = new LabelImpl(LabelType.PREFERRED, DEFAULT_LABEL, DEFAULT_LANGUAGE);
     283        final Label newLabel = new LabelImpl(LabelType.PREFERRED, DEFAULT_LABEL + "_2", DEFAULT_LANGUAGE);
     284        final Set<Label> oldLabels = Sets.newHashSet(label);
     285        final Set<Label> newLabels = Sets.newHashSet(newLabel);
     286        final Action addAction = new ActionImpl().withLabels(newLabels);
     287        final Action removeAction = new ActionImpl().withLabels(oldLabels);
     288        final Changeset changeset = new ChangesetImpl().withAdd(addAction).withRemove(removeAction);
     289        final Concept concept = new ConceptImpl.Builder().labels(Sets.newHashSet(label)).uuid(uuid).build();
     290
     291        when(mockStorageService.getConcept(uuid, DEFAULT_BRANCH)).thenReturn(concept);
     292
     293        conceptService.updateConcept(uuid, changeset, DEFAULT_BRANCH);
    238294       
    239         conceptService.deleteConcept(uuid, "Deletion of a non-existant object.", DEFAULT_BRANCH);
    240     }
    241    
    242     @Test(expected = IllegalArgumentException.class)
    243     public void deleteInvalidConcept() {
    244         conceptService.deleteConcept("Invalid UUID", "Deletion of an invalid UUID", DEFAULT_BRANCH);
     295        Mockito.verify(mockStorageService).addLabels(Mockito.eq(uuid), Mockito.eq(newLabels), Mockito.eq(DEFAULT_BRANCH));
     296        Mockito.verify(mockStorageService).removeLabels(Mockito.eq(uuid), Mockito.eq(oldLabels), Mockito.eq(DEFAULT_BRANCH));
    245297    }
    246298}
Note: See TracChangeset for help on using the changeset viewer.