package com.rewedigital.examples.msintegration.productinformation.product;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

import javax.inject.Inject;

import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.rewedigital.examples.msintegration.productinformation.helper.AbstractIntegrationTest;
import com.rewedigital.examples.msintegration.productinformation.infrastructure.eventing.LastPublishedVersion;
import com.rewedigital.examples.msintegration.productinformation.infrastructure.eventing.LastPublishedVersionRepository;

public class ProductRestControllerTest extends AbstractIntegrationTest {

    @Inject
    private LastPublishedVersionRepository lastPublishedVersionRepository;

    @Test
    public void testProductInsert() {

        final Product product = new Product();
        product.setName("Bla!");

        final Product response = restTemplate.postForObject("/products", product, Product.class);

        // wait until event is received from kafka

        assertThat(response).isNotNull();
        assertThat(response.getVersion()).isNotNull();

        assertThatLastPublishedVersionBecomes(response.getId(), 0L);
    }

    @Test
    public void testProductBadInsert() {

        final Product product = new Product();
        product.setId("108");

        final ResponseEntity<?> response = restTemplate.postForEntity("/products", product, Object.class);

        assertThat(response).isNotNull();
        assertThat(response.getStatusCode()).isEqualByComparingTo(HttpStatus.BAD_REQUEST);
    }


    private void assertThatLastPublishedVersionBecomes(final String id, final long version) {
       LastPublishedVersion result = null;
        int tryCount = 0;
        while (result == null && tryCount <= 5) {
            result = lastPublishedVersionRepository.findOne("product-" + id);
            if(result != null && result.getVersion() == version) {
                return;
            }
            try {
                Thread.sleep(100);
            } catch (final InterruptedException e) {
                e.printStackTrace();
            }
            ++tryCount;
        }
        fail("expected last published version of [" + id + "] to become [" + version + "]");
    }
}

@@ -73,264 +73,8 @@
 t;%0A%0A
-import static org.assertj.core.api.Assertions.assertThat;%0Aimport static org.assertj.core.api.Assertions.fail;%0A%0Aimport javax.inject.Inject;%0A%0Aimport org.junit.Test;%0Aimport org.springframework.http.HttpStatus;%0Aimport org.springframework.http.ResponseEntity;%0A%0A
 impo
@@ -397,16 +397,271 @@
 ository;
+%0Aimport org.junit.Test;%0Aimport org.springframework.http.HttpStatus;%0Aimport org.springframework.http.ResponseEntity;%0A%0Aimport javax.inject.Inject;%0A%0Aimport static org.assertj.core.api.Assertions.assertThat;%0Aimport static org.assertj.core.api.Assertions.fail;
 %0A%0Apublic
@@ -944,16 +944,58 @@
 %22Bla!%22);
+%0A        product.setProductNumber(%221234%22);
 %0A%0A      

fix test