Skip to content

Instantly share code, notes, and snippets.

View dhaval201279's full-sized avatar
🎯
Focusing

Dhaval Shah dhaval201279

🎯
Focusing
View GitHub Profile
@dhaval201279
dhaval201279 / ReservationServiceTest2.java
Last active March 11, 2018 03:00
2nd version ReservationServiceTest - with mock collaborators and passed test case
@RunWith(MockitoJUnitRunner.class)
public class ReservationServiceTest {
ReservationService reservationService;
@Mock
ReservationRepository reservationRepository;
@Before
public void setUp() throws Exception {
reservationService = new ReservationService(reservationRepository);
@dhaval201279
dhaval201279 / ReservationServiceTest3.java
Last active March 11, 2018 03:01
3rd version of ReservationServiceTest - with ReservationRepository returning null
@RunWith(MockitoJUnitRunner.class)
public class ReservationServiceTest {
ReservationService reservationService;
@Mock
ReservationRepository reservationRepository;
@Before
public void setUp() throws Exception {
reservationService = new ReservationService(reservationRepository);
@dhaval201279
dhaval201279 / ReservationService3.java
Last active March 11, 2018 03:02
3rd version of ReservationService - with null check and addition of ReservationNotFoundException
@Service
public class ReservationService {
private ReservationRepository reservationRepository;
public ReservationService(ReservationRepository reservationRepository) {
this.reservationRepository = reservationRepository;
}
public Reservation getReservationDetails(String name) {
System.out.println("Entering and leaving ReservationService : getReservationDetails "
@dhaval201279
dhaval201279 / ReservationRepository1.java
Last active March 11, 2018 05:10
1st version of ReservationRepository - for removing compilation error whilst executing test of ReservationService
package com.its.reservation.repository;
import org.springframework.data.repository.CrudRepository;
public interface ReservationRepository extends CrudRepository<Reservation, Long> {
Reservation findByFirstName(String name);
}
@dhaval201279
dhaval201279 / ReservationRepositoryTest1.java
Last active March 11, 2018 05:23
1st version of ReservationRepositoryTest - that passes when tested with CUT
package com.its.reservation;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;
@dhaval201279
dhaval201279 / ReservationEndToEndTest2.java
Last active March 11, 2018 05:33
final version of ReservationEndToEndTest
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ReservationEndToEndTest {
@Autowired
TestRestTemplate testRestTemplate;
@Test
public void getReservation_shouldReturnReservationDetails() {
// Arrange
@dhaval201279
dhaval201279 / BootifulTddApplication1.java
Created March 11, 2018 05:37
BootifulTddApplication - with caching annotation
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class BootifulTddApplication {
public static void main(String[] args) {
SpringApplication.run(BootifulTddApplication.class, args);
@dhaval201279
dhaval201279 / ReservationService4.java
Created March 11, 2018 05:40
4th version ReservationService - with @Cacheable
@Service
public class ReservationService {
private ReservationRepository reservationRepository;
public ReservationService(ReservationRepository reservationRepository) {
this.reservationRepository = reservationRepository;
}
@Cacheable("reservation")
@dhaval201279
dhaval201279 / ReservationCachingTest1.java
Last active March 11, 2018 05:42
1st version of ReservationCachingTest
package com.its.reservation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.BDDMockito;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
@dhaval201279
dhaval201279 / HttpClientConfigWithIdleAndExpiredConnMgmt.java
Created May 17, 2020 11:09
Http client config with house keeping of idle and expired connections
public Runnable idleAndExpiredConnectionProcessor(final PoolingHttpClientConnectionManager connectionManager) {
return new Runnable() {
@Override
@Scheduled(fixedDelay = 20000)
public void run() {
try {
if (connectionManager != null) {
connectionManager.closeExpiredConnections();
connectionManager.closeIdleConnections(IDLE_CONNECTION_WAIT_TIME_SECS, TimeUnit.SECONDS);
}