    @Test
    public void testNewInstance() throws Exception {

        Attributes mpps_create = load("testdata/mpps-create.xml");
        IDWithIssuer pid = IDWithIssuer.pidWithIssuer(mpps_create, null);
        String sourceAET = mpps_create.getString(Tag.PerformedStationAETitle);
        clearData(pid);
        StoreParam storeParam = ParamFactory.createStoreParam();
        StoreContext storeContext = new StoreContext(storeParam);
        PerformedProcedureStep pps = mppsService.createPerformedProcedureStep(
                MPPS_IUID, mpps_create, storeParam);
        assertEquals(PerformedProcedureStep.Status.IN_PROGRESS, pps.getStatus());
        PPSWithIAN ppsWithIAN = mppsService.updatePerformedProcedureStep(
                MPPS_IUID, load("testdata/mpps-set.xml"), storeParam);
        assertEquals(PerformedProcedureStep.Status.COMPLETED, ppsWithIAN.pps.getStatus());
        storeParam.setRetrieveAETs("AET_1","AET_2");
        storeParam.setExternalRetrieveAET("AET_3");
        storeContext.setAvailability(Availability.ONLINE);
        Attributes modified1 = new Attributes();
        Instance ct1 = storeService.newInstance(sourceAET,
                load("testdata/store-ct-1.xml"), modified1, storeContext);
        assertTrue(modified1.isEmpty());
        storeParam.setRetrieveAETs("AET_2");
        storeParam.setExternalRetrieveAET("AET_3");
        storeContext.setAvailability(Availability.NEARLINE);
        Attributes modified2 = new Attributes();
        Instance ct2 = storeService.newInstance(sourceAET,
                load("testdata/store-ct-2.xml"), modified2, storeContext);
        assertEquals(2, modified2.size());
        assertEquals("TEST-REPLACE", modified2.getString(Tag.StudyID));
        assertEquals("0", modified2.getString(Tag.SeriesNumber));
        storeParam.setRetrieveAETs("AET_1","AET_2");
        storeParam.setExternalRetrieveAET("AET_4");
        storeContext.setAvailability(Availability.ONLINE);
        Attributes modified3 = new Attributes();
        Instance pr1 = storeService.newInstance(sourceAET,
                load("testdata/store-pr-1.xml"), modified3, storeContext);
        assertEquals(1, modified3.size());
        assertEquals("TEST-REPLACE", modified3.getString(Tag.StudyID));
        List<Attributes> ians = ianQueryService.createIANsforMPPS(storeContext.getCurrentMPPS());
        assertEquals(1, ians.size());

        Series ctSeries = ct2.getSeries();
        Series prSeries = pr1.getSeries();
        Study study = prSeries.getStudy();
        assertEquals(ct1.getSeries().getPk(), ctSeries.getPk());
        assertEquals(ctSeries.getStudy().getPk(), study.getPk());
        assertArrayEquals(new String[]{"CT", "PR" }, sort(study.getModalitiesInStudy()));
        assertArrayEquals(
                new String[]{
                        "1.2.840.10008.5.1.4.1.1.11.1",
                        "1.2.840.10008.5.1.4.1.1.2" },
                sort(study.getSOPClassesInStudy()));
        assertArrayEquals(new String[]{"AET_2"}, ctSeries.getRetrieveAETs());
        assertArrayEquals(new String[]{"AET_1", "AET_2"}, sort(prSeries.getRetrieveAETs()));
        assertArrayEquals(new String[]{"AET_2"}, study.getRetrieveAETs());
        assertEquals("AET_3", ctSeries.getExternalRetrieveAET());
        assertEquals("AET_4", prSeries.getExternalRetrieveAET());
        assertNull(study.getExternalRetrieveAET());
        assertEquals(Availability.NEARLINE, ctSeries.getAvailability());
        assertEquals(Availability.ONLINE, prSeries.getAvailability());
        assertEquals(Availability.NEARLINE, study.getAvailability());
    }