Using TumblrJ to write posts, read credentials and running the unit tests
Some more progress with the api. This post will be about writing a post using the API, then reading a users credential information and I’ll finish off with the specifics on running the unit tests.
All code examples will focus on using the code through Spring, but this can all be done without Spring very easily (i.e.: Instead of building your instances through DI, just build them in the code yourself).
Writing
Same as with reading Tumblr information, you’ll need to:
- Setup an instance of the TumblrService class
- That class in turn needs a properly configured ITumblrReader instance. Since there’s only one implementation of that interfae, you’ll use TumblrHttpReader
- That in turns needs a TumblrConnectionOptions object with at the very minimum the name of your Tumblr log.
The small difference with here is that the write() method needs to be given a Credentials object to work.
Therefore, the Spring configuration would look something like:
<beans>
<bean id="tumblrJProperties" class="org.mikem.tumblr.api.util.TumblrJProperties"/>
<bean id="connectionOptions" class="org.mikem.tumblr.api.http.TumblrConnectionOptions">
<property name="name" value="${logname}"/>
</bean>
<bean id="reader" class="org.mikem.tumblr.api.http.TumblrHttpReader">
<property name="tumblrConnectionOptions" ref="connectionOptions"/>
<property name="properties" ref="tumblrJProperties"/>
</bean>
<bean id="credentials" class="org.mikem.tumblr.api.util.Credentials">
<property name="email" value="${email}"/>
<property name="password" value="${password}"/>
</bean>
<bean id="service" class="org.mikem.tumblr.api.TumblrService">
<property name="reader" ref="reader"/>
</bean>
</beans>
In the above configuration, you’ll need to replace the ${} variables with your own values.
A test case to go with this configuration would then look something like:
@Test
public void testWrite() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("test-spring-context.xml");
TumblrService service = (TumblrService) context.getBean("service");
Credentials credentials = (Credentials) context.getBean("credentials");
RegularPost post = new RegularPost();
post.setBody("Test post");
post.setPrivatePost(true);
post.setTitle("Test post title");
TumblePost savedPost = service.write(post, credentials);
Assert.assertNotNull(savedPost);
Assert.assertNotNull(savedPost.getId());
}
Simple enough! Notice that for now, only RegularPost has been tested.
Reading credentials
The Tumblr api offers a way to pull out all the relevant information for a user. To get that information, and using the same Spring configuration outlined above, you’d do something like:
@Test
public void testReadAuthenticationInformaiton() {
ApplicationContext context = new ClassPathXmlApplicationContext("test-spring-context.xml");
TumblrService service = (TumblrService) context.getBean("service");
Credentials credentials = (Credentials) context.getBean("credentials");
User user = service.getUserInformation(credentials);
Assert.assertNotNull(user);
Assert.assertTrue(user.getUserTumblelogs().size() > 0);
}
Check out the User object to see the wealth of information returned by Tumblr.
Running the tests
The unit tests found in the source code are tightly coupled to Tumblr itself, and by extension a users log, email and password. Therefore, so that everyone may run the tests on their own log and with their own credentials, all tests are devised to be configured against a file called private-test-config.properties. That file should be found on the classpath.
The contents is straightforward enough:
logname=mylogname
email=myemail
password=mypassword
If you look at the @Before method of the BaseServiceTest class, you’ll notice the call to that file. Again, if the file isn’t present on the classpath, the tests will fail miserably. I haven’t checked that file in for obvious reasons!
| Print article |