// Path: linkshortener-web/src/main/java/be/drissamri/Application.java
// @SpringBootApplication
// public class Application {
//   private static final int TWO_SECONDS = 2000;
// 
//   public static void main(String[] args) {
//     SpringApplication.run(Application.class, args);
//   }
// 
//   @Bean
//   public RestTemplate restTemplate(ClientHttpRequestFactory requestFactory) {
//     return new RestTemplate(requestFactory);
//   }
// 
//   @Bean
//   public ClientHttpRequestFactory clientHttpRequestFactory() {
//     final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
//     factory.setReadTimeout(TWO_SECONDS);
//     factory.setConnectTimeout(TWO_SECONDS);
//     return factory;
//   }
// }
// 
// Path: linkshortener-api/src/main/java/be/drissamri/rest/LinkController.java
// @Component
// @Path(LinkController.LINKS)
// public class LinkController {
//   private static final Logger LOGGER = LoggerFactory.getLogger(LinkController.class);
//   public static final String LINKS = "/links";
//   private final LinkService service;
// 
//   @Autowired
//   public LinkController(LinkService srvc) {
//     this.service = srvc;
//   }
// 
//   @GET
//   @Produces(MediaType.APPLICATION_JSON)
//   public final Response find(
//     @Context UriInfo context,
//     @QueryParam("expand") @DefaultValue("false") final boolean expand,
//     @QueryParam("offset") @DefaultValue("1") final int offset,
//     @QueryParam("limit") @DefaultValue("10") final int limit) throws Exception {
//     LOGGER.debug("Find links request. expand: {} - offset: {}, limit: {}", expand, offset, limit);
// 
//     final List<LinkEntity> foundLinks = service.find(offset, limit);
// 
//     final List<Resource> resources = foundLinks
//       .stream()
//       .map(link -> expand ? new LinkResource(context, link) : new Resource(context, link.getHash()))
//       .collect(Collectors.toList());
// 
//     final CollectionResource collection = new CollectionResource(context, resources, offset, limit);
//     LOGGER.debug("Returning collection of {} links", collection.get("size"));
//     return Response.ok().entity(collection).build();
//   }
// 
//   @POST
//   @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
//   @Produces(MediaType.APPLICATION_JSON)
//   public final Response createShortLink(@Context UriInfo context, @FormParam("url") String url) {
//     LOGGER.debug("Shorten request for: {}", url);
//     final LinkResource link = new LinkResource(context, this.service.create(url));
//     LOGGER.debug("Returning link: {}", link);
//     return created(link);
//   }
// 
//   @DELETE
//   @Path("/{hash}")
//   @Produces(MediaType.APPLICATION_JSON)
//   public final Response deleteLinkByHash(@PathParam(value = "hash") String hash) {
//     LOGGER.info("Request link delete for hash: {}", hash);
//     this.service.deleteByHash(hash);
//     return Response.noContent().build();
//   }
// 
//   private Response created(Resource createdResource) {
//     final String href = (String) createdResource.get("href");
//     return Response
//       .created(URI.create(href))
//       .entity(createdResource)
//       .build();
//   }
// }


import be.drissamri.Application;
import be.drissamri.rest.LinkController;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.path.json.JsonPath;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import static com.jayway.restassured.RestAssured.basic;
import static com.jayway.restassured.RestAssured.given;
import static javax.ws.rs.core.Response.Status.*;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.empty;
package be.drissamri.it;



@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class LinkControllerIT {
    private static final String LONG_URL = "http://www.drissamri.be";
    private static final String PARAMETER_HASH = "hash";
    private static final String PARAMETER_URL = "url";
    public static final String API_V1 = "/api/v1";

    @Value("${local.server.port}")
    private int port;

    @Before
    public void setUp() {
        RestAssured.authentication = basic("admin", "secret");
        RestAssured.port = port;
    }

    @Test
    public void shouldReturnOKWhenRetrievingAllLinks() {
        // @formatter:off
        createLink(LONG_URL);

        given()
            .contentType(MediaType.APPLICATION_JSON_VALUE)
// Path: linkshortener-web/src/main/java/be/drissamri/Application.java
// @SpringBootApplication
// public class Application {
//   private static final int TWO_SECONDS = 2000;
// 
//   public static void main(String[] args) {
//     SpringApplication.run(Application.class, args);
//   }
// 
//   @Bean
//   public RestTemplate restTemplate(ClientHttpRequestFactory requestFactory) {
//     return new RestTemplate(requestFactory);
//   }
// 
//   @Bean
//   public ClientHttpRequestFactory clientHttpRequestFactory() {
//     final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
//     factory.setReadTimeout(TWO_SECONDS);
//     factory.setConnectTimeout(TWO_SECONDS);
//     return factory;
//   }
// }
// 
// Path: linkshortener-api/src/main/java/be/drissamri/rest/LinkController.java
// @Component
// @Path(LinkController.LINKS)
// public class LinkController {
//   private static final Logger LOGGER = LoggerFactory.getLogger(LinkController.class);
//   public static final String LINKS = "/links";
//   private final LinkService service;
// 
//   @Autowired
//   public LinkController(LinkService srvc) {
//     this.service = srvc;
//   }
// 
//   @GET
//   @Produces(MediaType.APPLICATION_JSON)
//   public final Response find(
//     @Context UriInfo context,
//     @QueryParam("expand") @DefaultValue("false") final boolean expand,
//     @QueryParam("offset") @DefaultValue("1") final int offset,
//     @QueryParam("limit") @DefaultValue("10") final int limit) throws Exception {
//     LOGGER.debug("Find links request. expand: {} - offset: {}, limit: {}", expand, offset, limit);
// 
//     final List<LinkEntity> foundLinks = service.find(offset, limit);
// 
//     final List<Resource> resources = foundLinks
//       .stream()
//       .map(link -> expand ? new LinkResource(context, link) : new Resource(context, link.getHash()))
//       .collect(Collectors.toList());
// 
//     final CollectionResource collection = new CollectionResource(context, resources, offset, limit);
//     LOGGER.debug("Returning collection of {} links", collection.get("size"));
//     return Response.ok().entity(collection).build();
//   }
// 
//   @POST
//   @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
//   @Produces(MediaType.APPLICATION_JSON)
//   public final Response createShortLink(@Context UriInfo context, @FormParam("url") String url) {
//     LOGGER.debug("Shorten request for: {}", url);
//     final LinkResource link = new LinkResource(context, this.service.create(url));
//     LOGGER.debug("Returning link: {}", link);
//     return created(link);
//   }
// 
//   @DELETE
//   @Path("/{hash}")
//   @Produces(MediaType.APPLICATION_JSON)
//   public final Response deleteLinkByHash(@PathParam(value = "hash") String hash) {
//     LOGGER.info("Request link delete for hash: {}", hash);
//     this.service.deleteByHash(hash);
//     return Response.noContent().build();
//   }
// 
//   private Response created(Resource createdResource) {
//     final String href = (String) createdResource.get("href");
//     return Response
//       .created(URI.create(href))
//       .entity(createdResource)
//       .build();
//   }
// }


// Path: linkshortener-api/src/test/java/be/drissamri/it/LinkControllerIT.java
import be.drissamri.Application;
import be.drissamri.rest.LinkController;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.path.json.JsonPath;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import static com.jayway.restassured.RestAssured.basic;
import static com.jayway.restassured.RestAssured.given;
import static javax.ws.rs.core.Response.Status.*;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.empty;

package be.drissamri.it;



@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class LinkControllerIT {
    private static final String LONG_URL = "http://www.drissamri.be";
    private static final String PARAMETER_HASH = "hash";
    private static final String PARAMETER_URL = "url";
    public static final String API_V1 = "/api/v1";

    @Value("${local.server.port}")
    private int port;

    @Before
    public void setUp() {
        RestAssured.authentication = basic("admin", "secret");
        RestAssured.port = port;
    }

    @Test
    public void shouldReturnOKWhenRetrievingAllLinks() {
        // @formatter:off
        createLink(LONG_URL);

        given()
            .contentType(MediaType.APPLICATION_JSON_VALUE)
            .get(API_V1 + LinkController.LINKS)