Search in sources :

Example 1 with SomeDependency1

use of com.linkedin.restli.server.resources.fixtures.SomeDependency1 in project rest.li by linkedin.

the class TestInjectResourceFactory method testHappyPath.

@Test
public void testHappyPath() {
    Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(SomeResource1.class, SomeResource2.class, SomeResource5.class);
    // set up mock ApplicationContext
    BeanProvider ctx = createMock(BeanProvider.class);
    EasyMock.expect(ctx.getBean(EasyMock.eq("dep1"))).andReturn(new SomeDependency1()).anyTimes();
    EasyMock.expect(ctx.getBean(EasyMock.eq("dep3"))).andReturn(new SomeDependency1()).anyTimes();
    Map<String, SomeDependency2> map = new HashMap<String, SomeDependency2>();
    map.put("someBeanName", new SomeDependency2());
    EasyMock.expect(ctx.getBeansOfType(EasyMock.eq(SomeDependency2.class))).andReturn(map).anyTimes();
    EasyMock.replay(ctx);
    InjectResourceFactory factory = new InjectResourceFactory(ctx);
    factory.setRootResources(pathRootResourceMap);
    // #1 happy path
    SomeResource1 r1 = factory.create(SomeResource1.class);
    assertNotNull(r1);
    assertNotNull(r1.getDependency1());
    assertNotNull(r1.getDependency2());
    assertNull(r1.getNonInjectedDependency());
    // #2 No deps
    SomeResource2 r2 = factory.create(SomeResource2.class);
    assertNotNull(r2);
    // #3 bean not registered with ResourceFactory
    try {
        factory.create(SomeResource3.class);
        fail("Expected no such bean exception");
    } catch (RestLiInternalException e) {
    // expected
    }
    EasyMock.verify(ctx);
    EasyMock.reset(ctx);
    // #4 derived resource
    SomeResource5 r5 = factory.create(SomeResource5.class);
    assertNotNull(r5);
    assertNotNull(r5.getDependency1());
    assertNotNull(r5.getDerivedDependency1());
    assertNotNull(r5.getDependency2());
    assertNotNull(r5.getDependency3());
    assertNull(r5.getNonInjectedDependency());
}
Also used : HashMap(java.util.HashMap) SomeResource5(com.linkedin.restli.server.resources.fixtures.SomeResource5) SomeResource2(com.linkedin.restli.server.resources.fixtures.SomeResource2) SomeDependency2(com.linkedin.restli.server.resources.fixtures.SomeDependency2) SomeDependency1(com.linkedin.restli.server.resources.fixtures.SomeDependency1) SomeResource1(com.linkedin.restli.server.resources.fixtures.SomeResource1) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Test(org.testng.annotations.Test)

Example 2 with SomeDependency1

use of com.linkedin.restli.server.resources.fixtures.SomeDependency1 in project rest.li by linkedin.

the class TestInjectResourceFactory method testMissingNamedDependency.

@Test
public void testMissingNamedDependency() {
    Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(SomeResource1.class);
    BeanProvider ctx = EasyMock.createMock(BeanProvider.class);
    EasyMock.expect(ctx.getBean(EasyMock.eq("dep1"))).andReturn(null).anyTimes();
    EasyMock.expect(ctx.getBean(EasyMock.eq("dep3"))).andReturn(new SomeDependency1()).anyTimes();
    Map<String, SomeDependency2> map = new HashMap<String, SomeDependency2>();
    map.put("someBeanName", new SomeDependency2());
    EasyMock.expect(ctx.getBeansOfType(EasyMock.eq(SomeDependency2.class))).andReturn(map).anyTimes();
    EasyMock.replay(ctx);
    InjectResourceFactory factory = new InjectResourceFactory(ctx);
    try {
        factory.setRootResources(pathRootResourceMap);
        fail("Expected unresolvable bean exception");
    } catch (RestLiInternalException e) {
        assertTrue(e.getMessage().startsWith("Expected to find"));
    }
    EasyMock.verify(ctx);
}
Also used : SomeDependency2(com.linkedin.restli.server.resources.fixtures.SomeDependency2) SomeDependency1(com.linkedin.restli.server.resources.fixtures.SomeDependency1) HashMap(java.util.HashMap) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Test(org.testng.annotations.Test)

Example 3 with SomeDependency1

use of com.linkedin.restli.server.resources.fixtures.SomeDependency1 in project rest.li by linkedin.

the class TestInjectResourceFactory method testInjectConstructorArgs.

@Test
public void testInjectConstructorArgs() {
    Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(ConstructorArgResource.class);
    // set up mock ApplicationContext
    BeanProvider ctx = createMock(BeanProvider.class);
    EasyMock.expect(ctx.getBean(EasyMock.eq("dep1"))).andReturn(new SomeDependency1()).anyTimes();
    EasyMock.expect(ctx.getBean(EasyMock.eq("dep3"))).andReturn(new SomeDependency1()).anyTimes();
    Map<String, SomeDependency2> map = new HashMap<String, SomeDependency2>();
    map.put("someBeanName", new SomeDependency2());
    EasyMock.expect(ctx.getBeansOfType(EasyMock.eq(SomeDependency2.class))).andReturn(map).anyTimes();
    EasyMock.replay(ctx);
    InjectResourceFactory factory = new InjectResourceFactory(ctx);
    factory.setRootResources(pathRootResourceMap);
    // #1 happy path
    ConstructorArgResource r1 = factory.create(ConstructorArgResource.class);
    assertNotNull(r1);
    assertNotNull(r1.getDependency1());
    assertNotNull(r1.getDependency2());
    assertNull(r1.getNonInjectedDependency());
}
Also used : SomeDependency2(com.linkedin.restli.server.resources.fixtures.SomeDependency2) SomeDependency1(com.linkedin.restli.server.resources.fixtures.SomeDependency1) ConstructorArgResource(com.linkedin.restli.server.resources.fixtures.ConstructorArgResource) HashMap(java.util.HashMap) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Test(org.testng.annotations.Test)

Example 4 with SomeDependency1

use of com.linkedin.restli.server.resources.fixtures.SomeDependency1 in project rest.li by linkedin.

the class TestInjectResourceFactory method testAmbiguousBeanResolution.

@Test
public void testAmbiguousBeanResolution() throws Exception {
    Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(SomeResource1.class, SomeResource2.class, SomeResource4.class);
    // set up mock ApplicationContext
    BeanProvider ctx = EasyMock.createMock(BeanProvider.class);
    EasyMock.expect(ctx.getBean(EasyMock.eq("dep1"))).andReturn(new SomeDependency1()).anyTimes();
    Map<String, SomeDependency2> map2 = new HashMap<String, SomeDependency2>();
    map2.put("someBeanName", new SomeDependency2());
    EasyMock.expect(ctx.getBeansOfType(EasyMock.eq(SomeDependency2.class))).andReturn(map2).anyTimes();
    Map<String, SomeDependency1> map1 = new HashMap<String, SomeDependency1>();
    map1.put("someDep1", new SomeDependency1());
    map1.put("anotherDep1", new SomeDependency1());
    EasyMock.expect(ctx.getBeansOfType(EasyMock.eq(SomeDependency1.class))).andReturn(map1).anyTimes();
    EasyMock.replay(ctx);
    InjectResourceFactory factory = new InjectResourceFactory(ctx);
    // #4 ambiguous dep
    try {
        factory.setRootResources(pathRootResourceMap);
        fail("Expected unresolvable bean exception");
    } catch (RestLiInternalException e) {
        assertTrue(e.getMessage().startsWith("Expected to find"));
    }
    EasyMock.verify(ctx);
    EasyMock.reset(ctx);
}
Also used : SomeDependency2(com.linkedin.restli.server.resources.fixtures.SomeDependency2) SomeDependency1(com.linkedin.restli.server.resources.fixtures.SomeDependency1) HashMap(java.util.HashMap) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Test(org.testng.annotations.Test)

Aggregations

ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)4 SomeDependency1 (com.linkedin.restli.server.resources.fixtures.SomeDependency1)4 SomeDependency2 (com.linkedin.restli.server.resources.fixtures.SomeDependency2)4 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)3 ConstructorArgResource (com.linkedin.restli.server.resources.fixtures.ConstructorArgResource)1 SomeResource1 (com.linkedin.restli.server.resources.fixtures.SomeResource1)1 SomeResource2 (com.linkedin.restli.server.resources.fixtures.SomeResource2)1 SomeResource5 (com.linkedin.restli.server.resources.fixtures.SomeResource5)1