|
| 1 | +package net.trajano.jee.web.test; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertNotNull; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | +import static org.mockito.ArgumentMatchers.anyString; |
| 8 | +import static org.mockito.Mockito.mock; |
| 9 | +import static org.mockito.Mockito.when; |
| 10 | + |
| 11 | +import javax.faces.context.FacesContext; |
| 12 | +import javax.servlet.ServletContext; |
| 13 | + |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Test; |
| 16 | +import org.mockito.Mock; |
| 17 | +import org.mockito.MockitoAnnotations; |
| 18 | + |
| 19 | +import com.sun.faces.config.InitFacesContext; |
| 20 | + |
| 21 | +import net.trajano.jee.bean.NlpBean; |
| 22 | +import net.trajano.jee.nlp.AnalysisResult; |
| 23 | +import net.trajano.jee.nlp.Nlp; |
| 24 | + |
| 25 | +public class NlpBeanTest { |
| 26 | + |
| 27 | + private class FakeContext extends InitFacesContext { |
| 28 | + |
| 29 | + public FakeContext(final ServletContext sc) { |
| 30 | + super(sc); |
| 31 | + setCurrentInstance(context); |
| 32 | + } |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + @Mock |
| 37 | + private FacesContext context; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void before() { |
| 41 | + |
| 42 | + MockitoAnnotations.initMocks(this); |
| 43 | + final ServletContext sc = mock(ServletContext.class); |
| 44 | + new FakeContext(sc); |
| 45 | + assertEquals(context, FacesContext.getCurrentInstance()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testSimpleAnalysis() throws Exception { |
| 50 | + |
| 51 | + final NlpBean bean = new NlpBean(); |
| 52 | + final Nlp nlp = mock(Nlp.class); |
| 53 | + when(nlp.analyze(anyString())).thenReturn(new AnalysisResult()); |
| 54 | + bean.setNlp(nlp); |
| 55 | + bean.setInput("Hello world"); |
| 56 | + assertEquals("Hello world", bean.getInput()); |
| 57 | + assertFalse(bean.isParsed()); |
| 58 | + bean.parse(); |
| 59 | + assertNotNull(bean.getResult()); |
| 60 | + assertTrue(bean.isParsed()); |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments