Changeset 610

Show
Ignore:
Timestamp:
27-03-12 14:06:05 (2 years ago)
Author:
david.vanenckevort@…
Message:

* Added tests for the ConceptServiceImpl?.createConcept() call.

Files:
1 modified

Legend:

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

    r609 r610  
    11package nl.nbic.conceptwiki.service; 
    22 
     3import static org.junit.Assert.assertEquals; 
    34import static org.junit.Assert.assertFalse; 
    45import static org.junit.Assert.assertNotNull; 
    56import static org.mockito.Mockito.when; 
     7import static org.mockito.Mockito.doThrow; 
    68 
     9import com.google.common.collect.Iterables; 
     10import com.google.common.collect.Lists; 
     11import com.google.common.collect.Sets; 
    712import java.util.ArrayList; 
    813import java.util.List; 
     14import java.util.UUID; 
     15import nl.nbic.conceptwiki.common.api.Changeset; 
     16import nl.nbic.conceptwiki.common.api.Concept; 
     17import nl.nbic.conceptwiki.common.api.Label; 
     18import nl.nbic.conceptwiki.common.api.Label.LabelType; 
     19import nl.nbic.conceptwiki.common.api.Language; 
    920 
    1021import nl.nbic.conceptwiki.common.api.SearchResult; 
     22import nl.nbic.conceptwiki.common.impl.ConceptImpl; 
     23import nl.nbic.conceptwiki.common.impl.LabelImpl; 
     24import nl.nbic.conceptwiki.common.impl.LanguageImpl; 
    1125import nl.nbic.conceptwiki.common.impl.QueryScopeImpl; 
    1226import nl.nbic.conceptwiki.common.impl.SearchResultImpl; 
     
    3448@RunWith(MockitoJUnitRunner.class) 
    3549public class ConceptServiceImplUnitTest { 
     50    public static final String DEFAULT_LABEL = "Malaria"; 
     51    public static final Language DEFAULT_LANGUAGE = new LanguageImpl("en"); 
    3652        private static final int DEFAULT_LIMIT = 10; 
     53    /* 
     54     * The branch id used during the tests. 
     55    */ 
     56    private static final int DEFAULT_BRANCH = 1; 
    3757 
    3858        /** 
     
    7595                assertFalse(results.isEmpty()); 
    7696        } 
     97 
     98    /* 
     99     * Four tests for ConceptServiceImpl.createConcept(). We expect the following to be true. 
     100     * <ul> 
     101     * <li>A successfull call returns a new Concept with a UUID and all the initialized fields 
     102     * <li>An exception from one of the underlying services will fall through to the calling method. 
     103     * </ul> 
     104     */ 
     105    @Test 
     106    public void testCreateConcept() { 
     107        final Label label = new LabelImpl(LabelType.PREFERRED, DEFAULT_LABEL, DEFAULT_LANGUAGE); 
     108        final Concept concept = new ConceptImpl.Builder().labels(Sets.newHashSet(label)).build(); 
     109        final Concept mockedResult = new ConceptImpl.Builder().labels(Sets.newHashSet(label)).uuid(UUID.randomUUID().toString()).build(); 
     110        when(mockStorageService.createConcept(Mockito.any(Concept.class), Mockito.anyInt())).thenReturn(mockedResult); 
     111         
     112        // exercise 
     113        final Concept result = conceptService.createConcept(concept, DEFAULT_BRANCH); 
     114         
     115        // verify 
     116        assertNotNull(result); 
     117        assertNotNull(result.getUuid()); 
     118        assertEquals(DEFAULT_LABEL, Iterables.getOnlyElement(result.getLabels()).getText()); 
     119        assertEquals(LabelType.PREFERRED, Iterables.getOnlyElement(result.getLabels()).getType()); 
     120    } 
     121     
     122    @Test(expected = RuntimeException.class) 
     123    public void testCreateConceptFailureOfStorageService() { 
     124        final Label label = new LabelImpl(LabelType.PREFERRED, DEFAULT_LABEL, DEFAULT_LANGUAGE); 
     125        final Concept concept = new ConceptImpl.Builder().labels(Sets.newHashSet(label)).build(); 
     126        when(mockStorageService.createConcept(Mockito.any(Concept.class), Mockito.anyInt())).thenThrow(new RuntimeException()); 
     127         
     128        // exercise 
     129        final Concept result = conceptService.createConcept(concept, DEFAULT_BRANCH); 
     130    } 
     131 
     132    @Test(expected = RuntimeException.class) 
     133    public void testCreateConceptFailureOfHistoryService() { 
     134        final Label label = new LabelImpl(LabelType.PREFERRED, DEFAULT_LABEL, DEFAULT_LANGUAGE); 
     135        final Concept concept = new ConceptImpl.Builder().labels(Sets.newHashSet(label)).build(); 
     136        doThrow(new RuntimeException()).when(mockHistoryService).storeRevision(Mockito.anyString(), Mockito.any(Changeset.class)); 
     137         
     138        // exercise 
     139        final Concept result = conceptService.createConcept(concept, DEFAULT_BRANCH); 
     140    } 
     141 
     142    @Test(expected = RuntimeException.class) 
     143    public void testCreateConceptFailureOfIndexService() { 
     144        final Label label = new LabelImpl(LabelType.PREFERRED, DEFAULT_LABEL, DEFAULT_LANGUAGE); 
     145        final Concept concept = new ConceptImpl.Builder().labels(Sets.newHashSet(label)).build(); 
     146        doThrow(new RuntimeException()).when(mockSearchService).indexConcept(Mockito.any(Concept.class)); 
     147         
     148        // exercise 
     149        final Concept result = conceptService.createConcept(concept, DEFAULT_BRANCH); 
     150    } 
     151 
    77152}