### Question:
MasterInfoService implements AdvertisableService { @GET @Path( IS_AVAILABLE_PATH ) public Response isAvailable() { if ( haDb == null ) { return status( FORBIDDEN ).build(); } String role = haDb.role().toLowerCase(); if ( "slave".equals( role ) || "master".equals( role ) ) { return plainTextResponse( OK, role ); } return unknownResponse(); } MasterInfoService( @Context OutputFormat output, @Context GraphDatabaseService db ); @GET Response discover(); @GET @Path( IS_MASTER_PATH ) Response isMaster(); @GET @Path( IS_SLAVE_PATH ) Response isSlave(); @GET @Path( IS_AVAILABLE_PATH ) Response isAvailable(); @Override String getName(); @Override String getServerPath(); static final String BASE_PATH; static final String IS_MASTER_PATH; static final String IS_SLAVE_PATH; static final String IS_AVAILABLE_PATH; }### Answer:
@Test public void shouldReportMasterAsGenerallyAvailableForTransactionProcessing() { HighlyAvailableGraphDatabase database = mock( HighlyAvailableGraphDatabase.class ); when( database.role() ).thenReturn( "master" ); MasterInfoService service = new MasterInfoService( null, database ); Response response = service.isAvailable(); assertEquals( 200, response.getStatus() ); assertEquals( "master", String.valueOf( response.getEntity() ) ); }
@Test public void shouldReportSlaveAsGenerallyAvailableForTransactionProcessing() { HighlyAvailableGraphDatabase database = mock( HighlyAvailableGraphDatabase.class ); when( database.role() ).thenReturn( "slave" ); MasterInfoService service = new MasterInfoService( null, database ); Response response = service.isAvailable(); assertEquals( 200, response.getStatus() ); assertEquals( "slave", String.valueOf( response.getEntity() ) ); }
@Test public void shouldReportNonMasterOrSlaveAsUnavailableForTransactionProcessing() { HighlyAvailableGraphDatabase database = mock( HighlyAvailableGraphDatabase.class ); when( database.role() ).thenReturn( "unknown" ); MasterInfoService service = new MasterInfoService( null, database ); Response response = service.isAvailable(); assertEquals( 404, response.getStatus() ); assertEquals( "UNKNOWN", String.valueOf( response.getEntity() ) ); }