// Path: dhis2-models/src/main/java/com/thoughtworks/dhis/models/DataElementType.java
// @Getter
// public enum DataElementType {
//     ADJUSTMENTS("ADJUSTMENTS"),
//     ADJUSTMENT_REASON("ADJUSTMENT_REASON"),
//     ALLOCATED("ALLOCATED"),
//     ALLOCATION_ID("string", "ALLOCATION_ID"),
//     AMC("AMC"),
//     BREAKAGE("BREAKAGE"),
//     BUFFER_STOCK("BUFFER_STOCK", true),
//     DISPENSED("DISPENSED"),
//     EMERGENCY_ORDERED_AMOUNT("EMERGENCY_ORDERED_AMOUNT"),
//     EMERGENCY_REASON_FOR_ORDER("string", "EMERGENCY_REASON_FOR_ORDER"),
//     EXPIRED("EXPIRED"),
//     FROZEN("FROZEN"),
//     LABEL_REMOVED("LABEL_REMOVED"),
//     MAX_STOCK_QUANTITY("MAX_STOCK_QUANTITY", true),
//     MIN_STOCK_QUANTITY("MIN_STOCK_QUANTITY", true),
//     MISSING("MISSING"),
//     MONTHS_OF_STOCK_ON_HAND("MONTHS_OF_STOCK_ON_HAND"),
//     NUMBER_OF_STOCK_OUT_DAYS("NUMBER_OF_STOCK_OUT_DAYS"),
//     ORDERED_AMOUNT("ORDERED_AMOUNT"),
//     ORDER_ID("string", "ORDER_ID"),
//     OTHERS("OTHERS"),
//     PROJECTED_ORDER_AMOUNT("PROJECTED_ORDER_AMOUNT"),
//     REASON_FOR_ORDER("string", "REASON_FOR_ORDER"),
//     RECEIVED("RECEIVED"),
//     RECEIVE_DATE("string", "RECEIVE_DATE"),
//     RECEIVE_SOURCE("string", "RECEIVE_SOURCE"),
//     SAFETY_STOCK("SAFETY_STOCK"),
//     STOCK_ON_HAND("STOCK_ON_HAND"),
//     TMC("TMC"),
//     VVM_CHANGE("VVM_CHANGE"),
//     WASTED("WASTED");
// 
//     private String type = "int";
//     private String activity;
//     private boolean isIndicator = false;
// 
//     DataElementType(String activity, boolean isIndicator) {
//         this.activity = activity;
//         this.isIndicator = isIndicator;
//     }
// 
//     DataElementType(String type, String activity) {
//         this.type = type;
//         this.activity = activity;
//     }
// 
//     DataElementType(String activity) {
//         this.activity = activity;
//     }
// 
//     public static boolean dataElementActivityExists(String activityString) {
//         return getDataElementStrings(false).contains(activityString.trim());
//     }
// 
//     public static boolean indicatorExists(String activityString) {
//         return getDataElementStrings(true).contains(activityString.trim());
//     }
// 
//     public static List<String> getDataElementStrings(boolean isIndicator) {
//         return from(getDataElementTypes(isIndicator)).transform(new Function<DataElementType, String>() {
//             @Override
//             public String apply(DataElementType input) {
//                 return input.getActivity();
//             }
//         }).toList();
//     }
// 
//     private static List<DataElementType> getDataElementTypes(final boolean isIndicator) {
//         List<DataElementType> types = Lists.newArrayList(DataElementType.values());
//         return from(types).filter(new Predicate<DataElementType>() {
//             @Override
//             public boolean apply(DataElementType input) {
//                 return input.isIndicator() == isIndicator;
//             }
//         }).toList();
//     }
// }


import com.google.inject.Inject;
import com.j256.ormlite.dao.Dao;
import com.thoughtworks.dhis.models.DataElementType;
import org.clintonhealthaccess.lmis.LmisTestClass;
import org.clintonhealthaccess.lmis.app.models.Commodity;
import org.clintonhealthaccess.lmis.app.models.Dispensing;
import org.clintonhealthaccess.lmis.app.models.DispensingItem;
import org.clintonhealthaccess.lmis.app.models.User;
import org.clintonhealthaccess.lmis.app.persistence.DbUtil;
import org.clintonhealthaccess.lmis.utils.RobolectricGradleTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static org.clintonhealthaccess.lmis.utils.TestInjectionUtil.setUpInjectionWithMockLmisServer;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.robolectric.Robolectric.application;
    @Test
    public void testSaveDispensingSavesItsDispensingItems() throws Exception {
        commodityService.initialise(new User("test", "pass"));

        Commodity commodity = commodityService.all().get(0);
        Dispensing dispensing = new Dispensing();
        DispensingItem newDispensingItem = new DispensingItem(commodity, 1);
        dispensing.addItem(newDispensingItem);
        dispensingService.addDispensing(dispensing);

        Long count = dbUtil.withDao(DispensingItem.class, new DbUtil.Operation<DispensingItem, Long>() {
            @Override
            public Long operate(Dao<DispensingItem, String> dao) throws SQLException {
                return dao.countOf();
            }
        });

        assertThat(count, is(1L));
    }

    @Test
    public void addDispensingShouldReduceTheStockLevels() {
        commodityService.initialise(new User("test", "pass"));

        Commodity commodity = commodityService.all().get(0);
        int stockLevel = stockService.getStockLevelFor(commodity);
        assertThat(stockLevel, is(10));
        Dispensing dispensing = new Dispensing();
        DispensingItem item1 = new DispensingItem(commodity, 1);
        item1.setDispensing(dispensing);
// Path: dhis2-models/src/main/java/com/thoughtworks/dhis/models/DataElementType.java
// @Getter
// public enum DataElementType {
//     ADJUSTMENTS("ADJUSTMENTS"),
//     ADJUSTMENT_REASON("ADJUSTMENT_REASON"),
//     ALLOCATED("ALLOCATED"),
//     ALLOCATION_ID("string", "ALLOCATION_ID"),
//     AMC("AMC"),
//     BREAKAGE("BREAKAGE"),
//     BUFFER_STOCK("BUFFER_STOCK", true),
//     DISPENSED("DISPENSED"),
//     EMERGENCY_ORDERED_AMOUNT("EMERGENCY_ORDERED_AMOUNT"),
//     EMERGENCY_REASON_FOR_ORDER("string", "EMERGENCY_REASON_FOR_ORDER"),
//     EXPIRED("EXPIRED"),
//     FROZEN("FROZEN"),
//     LABEL_REMOVED("LABEL_REMOVED"),
//     MAX_STOCK_QUANTITY("MAX_STOCK_QUANTITY", true),
//     MIN_STOCK_QUANTITY("MIN_STOCK_QUANTITY", true),
//     MISSING("MISSING"),
//     MONTHS_OF_STOCK_ON_HAND("MONTHS_OF_STOCK_ON_HAND"),
//     NUMBER_OF_STOCK_OUT_DAYS("NUMBER_OF_STOCK_OUT_DAYS"),
//     ORDERED_AMOUNT("ORDERED_AMOUNT"),
//     ORDER_ID("string", "ORDER_ID"),
//     OTHERS("OTHERS"),
//     PROJECTED_ORDER_AMOUNT("PROJECTED_ORDER_AMOUNT"),
//     REASON_FOR_ORDER("string", "REASON_FOR_ORDER"),
//     RECEIVED("RECEIVED"),
//     RECEIVE_DATE("string", "RECEIVE_DATE"),
//     RECEIVE_SOURCE("string", "RECEIVE_SOURCE"),
//     SAFETY_STOCK("SAFETY_STOCK"),
//     STOCK_ON_HAND("STOCK_ON_HAND"),
//     TMC("TMC"),
//     VVM_CHANGE("VVM_CHANGE"),
//     WASTED("WASTED");
// 
//     private String type = "int";
//     private String activity;
//     private boolean isIndicator = false;
// 
//     DataElementType(String activity, boolean isIndicator) {
//         this.activity = activity;
//         this.isIndicator = isIndicator;
//     }
// 
//     DataElementType(String type, String activity) {
//         this.type = type;
//         this.activity = activity;
//     }
// 
//     DataElementType(String activity) {
//         this.activity = activity;
//     }
// 
//     public static boolean dataElementActivityExists(String activityString) {
//         return getDataElementStrings(false).contains(activityString.trim());
//     }
// 
//     public static boolean indicatorExists(String activityString) {
//         return getDataElementStrings(true).contains(activityString.trim());
//     }
// 
//     public static List<String> getDataElementStrings(boolean isIndicator) {
//         return from(getDataElementTypes(isIndicator)).transform(new Function<DataElementType, String>() {
//             @Override
//             public String apply(DataElementType input) {
//                 return input.getActivity();
//             }
//         }).toList();
//     }
// 
//     private static List<DataElementType> getDataElementTypes(final boolean isIndicator) {
//         List<DataElementType> types = Lists.newArrayList(DataElementType.values());
//         return from(types).filter(new Predicate<DataElementType>() {
//             @Override
//             public boolean apply(DataElementType input) {
//                 return input.isIndicator() == isIndicator;
//             }
//         }).toList();
//     }
// }


// Path: app/src/test/java/org/clintonhealthaccess/lmis/app/services/DispensingServiceTest.java
import com.google.inject.Inject;
import com.j256.ormlite.dao.Dao;
import com.thoughtworks.dhis.models.DataElementType;
import org.clintonhealthaccess.lmis.LmisTestClass;
import org.clintonhealthaccess.lmis.app.models.Commodity;
import org.clintonhealthaccess.lmis.app.models.Dispensing;
import org.clintonhealthaccess.lmis.app.models.DispensingItem;
import org.clintonhealthaccess.lmis.app.models.User;
import org.clintonhealthaccess.lmis.app.persistence.DbUtil;
import org.clintonhealthaccess.lmis.utils.RobolectricGradleTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static org.clintonhealthaccess.lmis.utils.TestInjectionUtil.setUpInjectionWithMockLmisServer;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.robolectric.Robolectric.application;

    @Test
    public void testSaveDispensingSavesItsDispensingItems() throws Exception {
        commodityService.initialise(new User("test", "pass"));

        Commodity commodity = commodityService.all().get(0);
        Dispensing dispensing = new Dispensing();
        DispensingItem newDispensingItem = new DispensingItem(commodity, 1);
        dispensing.addItem(newDispensingItem);
        dispensingService.addDispensing(dispensing);

        Long count = dbUtil.withDao(DispensingItem.class, new DbUtil.Operation<DispensingItem, Long>() {
            @Override
            public Long operate(Dao<DispensingItem, String> dao) throws SQLException {
                return dao.countOf();
            }
        });

        assertThat(count, is(1L));
    }

    @Test
    public void addDispensingShouldReduceTheStockLevels() {
        commodityService.initialise(new User("test", "pass"));

        Commodity commodity = commodityService.all().get(0);
        int stockLevel = stockService.getStockLevelFor(commodity);
        assertThat(stockLevel, is(10));
        Dispensing dispensing = new Dispensing();
        DispensingItem item1 = new DispensingItem(commodity, 1);
        item1.setDispensing(dispensing);
        assertThat(item1.getActivitiesValues().get(0).getCommodityAction().getActivityType(), is(DataElementType.DISPENSED.getActivity()));