// Path: examples/microservice/dao-api/src/main/java/org/osgi/enroute/examples/microservice/dao/PersonDao.java
// @ProviderType
// public interface PersonDao {
//     
//     public List<PersonDTO> select();
// 
//     public PersonDTO findByPK(Long pk) ;
// 
//     public Long save(PersonDTO data);
// 
//     public void update(PersonDTO data);
// 
//     public void delete(Long pk) ;
// }
// 
// Path: examples/microservice/dao-api/src/main/java/org/osgi/enroute/examples/microservice/dao/dto/PersonDTO.java
// public class PersonDTO {
// 
// 	public long personId;
// 	public String firstName;
// 	public String lastName;
// 
// 	public List<AddressDTO> addresses = new ArrayList<>();
// }


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.osgi.namespace.service.ServiceNamespace.SERVICE_NAMESPACE;
import static org.osgi.service.jaxrs.runtime.JaxrsServiceRuntimeConstants.JAX_RS_SERVICE_ENDPOINT;
import java.util.Collections;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.osgi.annotation.bundle.Capability;
import org.osgi.enroute.examples.microservice.dao.PersonDao;
import org.osgi.enroute.examples.microservice.dao.dto.PersonDTO;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.jaxrs.runtime.JaxrsServiceRuntime;
import org.osgi.util.converter.Converters;
import org.osgi.util.tracker.ServiceTracker;
    private void registerDao() {
        registration = bundle.getBundleContext().registerService(PersonDao.class, mockDAO, null);
    }
    
    @Test
    public void testRestServiceRegistered() throws Exception {
        
        assertEquals(0, jaxrsServiceRuntime.getRuntimeDTO().defaultApplication.resourceDTOs.length);
        
        registerDao();
        
        assertEquals(1, jaxrsServiceRuntime.getRuntimeDTO().defaultApplication.resourceDTOs.length);
    }

    @Test
    public void testGetPerson() throws Exception {
        
        registerDao();
        
        // Set up a Base URI
        String base = Converters.standardConverter().convert(
                runtimeTracker.getServiceReference().getProperty(JAX_RS_SERVICE_ENDPOINT)).to(String.class);
        WebTarget target = client.target(base);
        
        // There should be no results in the answer
        assertEquals("[]", target.path("person")
            .request()
            .get(String.class));
        
        // Add a person to the DAO
// Path: examples/microservice/dao-api/src/main/java/org/osgi/enroute/examples/microservice/dao/PersonDao.java
// @ProviderType
// public interface PersonDao {
//     
//     public List<PersonDTO> select();
// 
//     public PersonDTO findByPK(Long pk) ;
// 
//     public Long save(PersonDTO data);
// 
//     public void update(PersonDTO data);
// 
//     public void delete(Long pk) ;
// }
// 
// Path: examples/microservice/dao-api/src/main/java/org/osgi/enroute/examples/microservice/dao/dto/PersonDTO.java
// public class PersonDTO {
// 
// 	public long personId;
// 	public String firstName;
// 	public String lastName;
// 
// 	public List<AddressDTO> addresses = new ArrayList<>();
// }


// Path: examples/microservice/rest-service-test/src/main/java/org/osgi/enroute/examples/microservice/rest/service/test/RestServiceIntegrationTest.java
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.osgi.namespace.service.ServiceNamespace.SERVICE_NAMESPACE;
import static org.osgi.service.jaxrs.runtime.JaxrsServiceRuntimeConstants.JAX_RS_SERVICE_ENDPOINT;
import java.util.Collections;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.osgi.annotation.bundle.Capability;
import org.osgi.enroute.examples.microservice.dao.PersonDao;
import org.osgi.enroute.examples.microservice.dao.dto.PersonDTO;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.jaxrs.runtime.JaxrsServiceRuntime;
import org.osgi.util.converter.Converters;
import org.osgi.util.tracker.ServiceTracker;

    private void registerDao() {
        registration = bundle.getBundleContext().registerService(PersonDao.class, mockDAO, null);
    }
    
    @Test
    public void testRestServiceRegistered() throws Exception {
        
        assertEquals(0, jaxrsServiceRuntime.getRuntimeDTO().defaultApplication.resourceDTOs.length);
        
        registerDao();
        
        assertEquals(1, jaxrsServiceRuntime.getRuntimeDTO().defaultApplication.resourceDTOs.length);
    }

    @Test
    public void testGetPerson() throws Exception {
        
        registerDao();
        
        // Set up a Base URI
        String base = Converters.standardConverter().convert(
                runtimeTracker.getServiceReference().getProperty(JAX_RS_SERVICE_ENDPOINT)).to(String.class);
        WebTarget target = client.target(base);
        
        // There should be no results in the answer
        assertEquals("[]", target.path("person")
            .request()
            .get(String.class));
        
        // Add a person to the DAO
        PersonDTO dto = new PersonDTO();