package dist.service;
import api.v1.OrderSearch;
import dist.models.DistLocation;
import dist.models.DistLocationHost;
import dist.models.DistLocationHostPK;
import dist.models.DistProduct;
import models.OrderModel;
import org.joda.time.DateTime;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import service.OrderService;
import service.PoseidonPropertyService;
import service.PoseidonService;
import util.InMemoryDbTest;
import java.util.Date;
import java.util.List;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
public class DistSyncerTest extends InMemoryDbTest {
private final static Logger logger = LoggerFactory.getLogger(DistSyncerTest.class);
@Test
public void testSyncAndDeleteOneOrder() {
DateTime now = new DateTime(2016,2,25,14,0,PoseidonService.getTimeZone());
PoseidonService.setNow(now);
OrderModel order = OrderModel.find.byId(1L);
assertNotNull("Fant ikke bestilling med id 1",order);
DistSyncer syncer = new DistSyncer();
syncer.syncOrder(order);
String locationName = "offshore_"+String.valueOf(order.id);
String productFile = "/opdata/ma/maritim/offshorebymetno/" + String.valueOf(order.id) + "[._].+";
DistProduct product = DistProduct.find.byId(locationName);
DistProduct distProduct = product;
assertEquals("Uventet produknavn", distProduct.name, locationName);
assertEquals("Uventet produktfil", distProduct.file, productFile);
assertEquals("Uventet produkttype", distProduct.type, "perlre");
DistLocation location = DistLocation.find.byId(locationName);
assertEquals("Uventet location.name",location.name, locationName);
assertEquals("Uventet location.remote_info",location.remote_info, "htdocs/kundedata/" + locationName);
DistLocationHostPK hostPk = new DistLocationHostPK();
String host1 = "c1vb1";
hostPk.host = host1;
hostPk.location = locationName;
DistLocationHost locationHost = DistLocationHost.find.byId(hostPk);
assertEquals("Uventet host locationName", locationHost.pk.location, locationName);
assertEquals("Uventet hostname", locationHost.pk.host, host1);
syncer.deleteOrder(order);
product = DistProduct.find.byId(locationName);
assertNull("Fant produkt som skulle ha blitt slettet",product);
}
@Test
public void testSyncAllAndDeleteNonActiveFromDistDb() {
OrderService service = new OrderService(fakeUser);
List<OrderModel> orders = service.findAll();
DateTime now = new DateTime(2016,2,25,14,0,PoseidonService.getTimeZone());
PoseidonService.setNow(now);
assertNotNull("Fant ingen bestillinger", orders);
assertTrue("Fant ingen bestillinger",orders.size() >0);
logger.info("Fant {} bestillinger", orders.size());
DistSyncer syncer = new DistSyncer();
for (OrderModel order : orders){
syncer.syncOrder(order);
}
OrderSearch params = new OrderSearch();
params.status = "active";
params.invertStatus = true;
List<OrderModel> allNotActiveOrders = service.search(params);
assertNotNull("Fant ingen ikke-aktive bestillinger", allNotActiveOrders);
assertTrue("Fant ingen ikke-aktive bestillinger",allNotActiveOrders.size() >0);
logger.info("Fant {} ikke-aktive bestillinger", allNotActiveOrders.size());
// delete orders
for (OrderModel order : allNotActiveOrders) {
syncer.deleteOrder(order);
}
String prefix = PoseidonPropertyService.getProperty("dist.location.nameprefix");
OrderModel bestillingUtenSluttdato = OrderModel.find.byId(4L);
assertNotNull("Fant ikke ordre med id 4",bestillingUtenSluttdato);
assertNull("ordre nr 4 har sluttdato",bestillingUtenSluttdato.findEndDate());
// bestillinger med sluttdato skal ha blitt fjernet, hvis nĂ¥ > sluttdato
OrderModel bestillingMedSluttdato = OrderModel.find.byId(1L);
Date sluttdato = bestillingMedSluttdato.findEndDate();
assertNotNull("Bestilling med id 1 har ikke sluttdato", sluttdato);
assertTrue(sluttdato.before(now.toDate()));
String nonExistingTestLocationName = prefix + bestillingMedSluttdato.id;
DistProduct product = DistProduct.find.byId(nonExistingTestLocationName);
assertNull("Fant produkt som skulle ha blitt slettet",product);
}
}