Example usage for java.util Set containsAll

List of usage examples for java.util Set containsAll

Introduction

In this page you can find the example usage for java.util Set containsAll.

Prototype

boolean containsAll(Collection<?> c);

Source Link

Document

Returns true if this set contains all of the elements of the specified collection.

Usage

From source file:org.mitre.openid.connect.service.impl.TestMITREidDataService_1_2.java

@Test
public void testExportSystemScopes() throws IOException {
    SystemScope scope1 = new SystemScope();
    scope1.setId(1L);/*from ww  w.  j  a  va2  s  .  co m*/
    scope1.setValue("scope1");
    scope1.setDescription("Scope 1");
    scope1.setRestricted(true);
    scope1.setDefaultScope(false);
    scope1.setIcon("glass");

    SystemScope scope2 = new SystemScope();
    scope2.setId(2L);
    scope2.setValue("scope2");
    scope2.setDescription("Scope 2");
    scope2.setRestricted(false);
    scope2.setDefaultScope(false);
    scope2.setIcon("ball");

    SystemScope scope3 = new SystemScope();
    scope3.setId(3L);
    scope3.setValue("scope3");
    scope3.setDescription("Scope 3");
    scope3.setRestricted(false);
    scope3.setDefaultScope(true);
    scope3.setIcon("road");

    Set<SystemScope> allScopes = ImmutableSet.of(scope1, scope2, scope3);

    Mockito.when(clientRepository.getAllClients()).thenReturn(new HashSet<ClientDetailsEntity>());
    Mockito.when(approvedSiteRepository.getAll()).thenReturn(new HashSet<ApprovedSite>());
    Mockito.when(wlSiteRepository.getAll()).thenReturn(new HashSet<WhitelistedSite>());
    Mockito.when(blSiteRepository.getAll()).thenReturn(new HashSet<BlacklistedSite>());
    Mockito.when(authHolderRepository.getAll()).thenReturn(new ArrayList<AuthenticationHolderEntity>());
    Mockito.when(tokenRepository.getAllAccessTokens()).thenReturn(new HashSet<OAuth2AccessTokenEntity>());
    Mockito.when(tokenRepository.getAllRefreshTokens()).thenReturn(new HashSet<OAuth2RefreshTokenEntity>());
    Mockito.when(sysScopeRepository.getAll()).thenReturn(allScopes);

    // do the data export
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(stringWriter);
    writer.beginObject();
    dataService.exportData(writer);
    writer.endObject();
    writer.close();

    // parse the output as a JSON object for testing
    JsonElement elem = new JsonParser().parse(stringWriter.toString());
    JsonObject root = elem.getAsJsonObject();

    // make sure the root is there
    assertThat(root.has(MITREidDataService.MITREID_CONNECT_1_2), is(true));

    JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_2).getAsJsonObject();

    // make sure all the root elements are there
    assertThat(config.has(MITREidDataService.CLIENTS), is(true));
    assertThat(config.has(MITREidDataService.GRANTS), is(true));
    assertThat(config.has(MITREidDataService.WHITELISTEDSITES), is(true));
    assertThat(config.has(MITREidDataService.BLACKLISTEDSITES), is(true));
    assertThat(config.has(MITREidDataService.REFRESHTOKENS), is(true));
    assertThat(config.has(MITREidDataService.ACCESSTOKENS), is(true));
    assertThat(config.has(MITREidDataService.SYSTEMSCOPES), is(true));
    assertThat(config.has(MITREidDataService.AUTHENTICATIONHOLDERS), is(true));

    // make sure the root elements are all arrays
    assertThat(config.get(MITREidDataService.CLIENTS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.GRANTS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.WHITELISTEDSITES).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.BLACKLISTEDSITES).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.REFRESHTOKENS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.ACCESSTOKENS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.SYSTEMSCOPES).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.AUTHENTICATIONHOLDERS).isJsonArray(), is(true));

    // check our scope list (this test)
    JsonArray scopes = config.get(MITREidDataService.SYSTEMSCOPES).getAsJsonArray();

    assertThat(scopes.size(), is(3));
    // check for both of our clients in turn
    Set<SystemScope> checked = new HashSet<>();
    for (JsonElement e : scopes) {
        assertThat(e.isJsonObject(), is(true));
        JsonObject scope = e.getAsJsonObject();

        SystemScope compare = null;
        if (scope.get("value").getAsString().equals(scope1.getValue())) {
            compare = scope1;
        } else if (scope.get("value").getAsString().equals(scope2.getValue())) {
            compare = scope2;
        } else if (scope.get("value").getAsString().equals(scope3.getValue())) {
            compare = scope3;
        }

        if (compare == null) {
            fail("Could not find matching scope value: " + scope.get("value").getAsString());
        } else {
            assertThat(scope.get("value").getAsString(), equalTo(compare.getValue()));
            assertThat(scope.get("description").getAsString(), equalTo(compare.getDescription()));
            assertThat(scope.get("icon").getAsString(), equalTo(compare.getIcon()));
            assertThat(scope.get("restricted").getAsBoolean(), equalTo(compare.isRestricted()));
            assertThat(scope.get("defaultScope").getAsBoolean(), equalTo(compare.isDefaultScope()));
            checked.add(compare);
        }
    }
    // make sure all of our clients were found
    assertThat(checked.containsAll(allScopes), is(true));

}

From source file:org.sakaiproject.tool.assessment.qti.helper.ExtractionHelper.java

private void addRespondusMatchTextAndAnswers(ItemFacade item, Map itemMap) throws RespondusMatchingException {

    List sourceList = (List) itemMap.get("itemMatchSourceText");
    List targetList = (List) itemMap.get("itemAnswer");
    List correctList = (List) itemMap.get("itemMatchingAnswerCorrect");

    sourceList = sourceList == null ? new ArrayList() : sourceList;
    targetList = targetList == null ? new ArrayList() : targetList;
    correctList = correctList == null ? new ArrayList() : correctList;

    List itemTextList = (List) itemMap.get("itemText");
    if (itemTextList != null && itemTextList.size() > 0) {
        item.setInstruction(makeFCKAttachmentFromRespondus((String) itemTextList.get(0)));
    }/*from w  ww  . j av a  2 s  .c  o m*/

    HashMap<String, String> sourceMap = new HashMap<String, String>();
    if (sourceList != null) {
        Iterator iter = sourceList.iterator();
        String[] s = null;
        while (iter.hasNext()) {
            s = ((String) iter.next()).split(":::");
            sourceMap.put(s[0], s[1]);
        }
    }
    HashMap<String, String> targetMap = new HashMap<String, String>();
    if (targetList != null) {
        Iterator iter = targetList.iterator();
        String[] s = null;
        while (iter.hasNext()) {
            s = ((String) iter.next()).split(":::");
            targetMap.put(s[0], s[1]);
        }
    }
    HashMap<String, String> correctMap = new HashMap<String, String>();
    if (correctList != null) {
        Iterator iter = correctList.iterator();
        String[] s = null;
        while (iter.hasNext()) {
            s = ((String) iter.next()).split(":::");
            correctMap.put(s[0], s[1]);
        }
    }

    Set<ItemText> itemTextSet = new HashSet<ItemText>();
    String ident = "";
    String correctVar = "";
    String sourceText = "";
    String targetText = "";
    int itemSequence = 1;
    for (Entry<String, String> source : sourceMap.entrySet()) {
        char answerLabel = 'A';
        sourceText = source.getValue();
        if (sourceText == null)
            sourceText = "";
        sourceText = sourceText.replaceAll("\\?\\?", " ");//SAK-2298
        log.debug("sourceText: " + sourceText);

        ItemText sourceItemText = new ItemText();

        int answerSequence = 1;
        HashSet targetSet = new HashSet();
        for (Entry<String, String> target : targetMap.entrySet()) {

            targetText = target.getValue();
            if (targetText == null)
                targetText = "";
            targetText = targetText.replaceAll("\\?\\?", " ");
            log.debug("targetText: " + targetText);

            Answer answer = new Answer();

            String label = "" + answerLabel++;
            answer.setLabel(label); // up to 26, is this a problem?
            answer.setText(makeFCKAttachmentFromRespondus(targetText));
            answer.setItemText(sourceItemText);
            answer.setItem(item.getData());
            answer.setSequence(Long.valueOf(answerSequence++));
            answer.setScore(Double.valueOf(getCorrectScore(item, 1)));

            ident = source.getKey();
            correctVar = correctMap.get(ident);
            if (target.getKey().equals(correctVar)) {
                answer.setIsCorrect(Boolean.TRUE);
            } else {
                answer.setIsCorrect(Boolean.FALSE);
            }
            targetSet.add(answer);
        }
        sourceItemText.setText(makeFCKAttachmentFromRespondus(sourceText));
        sourceItemText.setItem(item.getData());
        sourceItemText.setSequence(Long.valueOf(itemSequence++));
        sourceItemText.setAnswerSet(targetSet);
        itemTextSet.add(sourceItemText);
    }
    // Respondus allows for more matches than choices.  
    // If any answer does not have a correct choice, throw an exception
    Set<String> correctAnswers = new HashSet<String>();
    Set<String> allAnswers = new HashSet<String>();
    for (ItemText itemText : itemTextSet) {
        Set<AnswerIfc> answers = itemText.getAnswerSet();
        for (AnswerIfc answer : answers) {
            allAnswers.add(answer.getText());
            if (answer.getIsCorrect()) {
                correctAnswers.add(answer.getText());
            }
        }
    }
    if (!correctAnswers.containsAll(allAnswers)) {
        throw new RespondusMatchingException("All answers do not have a valid choice.");
    }

    item.setItemTextSet(itemTextSet);
}

From source file:org.sakaiproject.component.gradebook.GradebookServiceHibernateImpl.java

@Override
public void mergeGradebookDefinitionXml(String toGradebookUid, String fromGradebookXml) {
    final Gradebook gradebook = getGradebook(toGradebookUid);
    GradebookDefinition gradebookDefinition = (GradebookDefinition) VersionedExternalizable
            .fromXml(fromGradebookXml);//from  w ww  .j  a  va  2s  . c  o m

    @SuppressWarnings({ "unchecked", "rawtypes" })
    List<String> assignmentNames = (List<String>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(final Session session) throws HibernateException {
            return session.createQuery(
                    "select asn.name from Assignment as asn where asn.gradebook.id=? and asn.removed=false")
                    .setLong(0, gradebook.getId().longValue()).list();
        }
    });

    // Add any non-externally-managed assignments with non-duplicate names.
    int assignmentsAddedCount = 0;
    for (org.sakaiproject.service.gradebook.shared.Assignment obj : gradebookDefinition.getAssignments()) {
        org.sakaiproject.service.gradebook.shared.Assignment assignmentDef = (org.sakaiproject.service.gradebook.shared.Assignment) obj;

        // Externally managed assessments should not be included.
        if (assignmentDef.isExternallyMaintained()) {
            continue;
        }

        // Skip any input assignments with duplicate names.
        if (assignmentNames.contains(assignmentDef.getName())) {
            if (log.isInfoEnabled())
                log.info("Merge to gradebook " + toGradebookUid + " skipped duplicate assignment named "
                        + assignmentDef.getName());
            continue;
        }

        // All assignments should be unreleased even if they were released in the original.
        createAssignment(gradebook.getId(), assignmentDef.getName(), assignmentDef.getPoints(),
                assignmentDef.getDueDate(), true, false, assignmentDef.isExtraCredit());
        assignmentsAddedCount++;
    }
    if (log.isInfoEnabled())
        log.info("Merge to gradebook " + toGradebookUid + " added " + assignmentsAddedCount + " assignments");

    // Carry over the old gradebook's selected grading scheme if possible.
    String fromGradingScaleUid = gradebookDefinition.getSelectedGradingScaleUid();
    MERGE_GRADE_MAPPING: if (!StringUtils.isEmpty(fromGradingScaleUid)) {
        for (GradeMapping gradeMapping : gradebook.getGradeMappings()) {
            if (gradeMapping.getGradingScale().getUid().equals(fromGradingScaleUid)) {
                // We have a match. Now make sure that the grades are as expected.
                Map<String, Double> inputGradePercents = gradebookDefinition
                        .getSelectedGradingScaleBottomPercents();
                Set<String> gradeCodes = (Set<String>) inputGradePercents.keySet();
                if (gradeCodes.containsAll(gradeMapping.getGradeMap().keySet())) {
                    // Modify the existing grade-to-percentage map.
                    for (String gradeCode : gradeCodes) {
                        gradeMapping.getGradeMap().put(gradeCode, inputGradePercents.get(gradeCode));
                    }
                    gradebook.setSelectedGradeMapping(gradeMapping);
                    updateGradebook(gradebook);
                    if (log.isInfoEnabled())
                        log.info("Merge to gradebook " + toGradebookUid + " updated grade mapping");
                } else {
                    if (log.isInfoEnabled())
                        log.info("Merge to gradebook " + toGradebookUid
                                + " skipped grade mapping change because the " + fromGradingScaleUid
                                + " grade codes did not match");
                }
                break MERGE_GRADE_MAPPING;
            }
        }
        // Did not find a matching grading scale.
        if (log.isInfoEnabled())
            log.info("Merge to gradebook " + toGradebookUid
                    + " skipped grade mapping change because grading scale " + fromGradingScaleUid
                    + " is not defined");
    }
}

From source file:org.rifidi.edge.epcglobal.aleread.wrappers.RifidiReport.java

public void processEvents(Set<DatacontainerEvent> incoming) {
    if (includeFilters.size() > 0 || excludeFilter.size() > 0) {
        Set<DatacontainerEvent> notincluded = new HashSet<DatacontainerEvent>();
        boolean matched = false;
        for (DatacontainerEvent event : incoming) {
            matched = false;/*from www. j a v a2s.  co  m*/
            for (ALEField field : includeFilters.keySet()) {
                String fieldString = adapter.getField(field, event);
                for (PatternMatcher matcher : includeFilters.get(field)) {
                    if (matcher.match(fieldString)) {
                        // remove the event from the list of events that
                        // will
                        // be dropped
                        notincluded.remove(event);
                        matched = true;
                        break;
                    }
                    if (matched) {
                        break;
                    }
                }
                if (matched) {
                    break;
                }
            }
            if (includeFilters.size() > 0) {
                if (!matched) {
                    notincluded.add(event);
                    // we are already removing it, no need to process the
                    // exclude
                    // filters
                    continue;
                }
            }
            for (ALEField field : excludeFilter.keySet()) {
                String fieldString = adapter.getField(field, event);
                for (PatternMatcher matcher : excludeFilter.get(field)) {
                    if (matcher.match(fieldString)) {
                        // add event to the list of events that will be
                        // dropped
                        notincluded.add(event);
                        matched = true;
                        break;
                    }
                    if (matched) {
                        break;
                    }
                }
                if (matched) {
                    break;
                }
            }
            matched = false;
        }
        // remove all events that were not mathched by an include filter
        incoming.removeAll(notincluded);
    }

    if ((options & ECReportOptions.REPORT_ADDITIONS) > 0) {
        // keep the tags that appear in both sets
        tagreads.retainAll(incoming);
        // remove the common tags from incoming
        incoming.removeAll(tagreads);
        // add all tags to tagreads
        tagreads.addAll(incoming);
        // prepare tags to send out
        // check if the set changes at all
        if ((options & ECReportOptions.REPORT_ONLY_ON_CHANGE) > 0) {
            if (!(tagreadsToSend.containsAll(incoming) && incoming.containsAll(tagreadsToSend))) {
                tagreadsToSend.clear();
                tagreadsToSend.addAll(incoming);
                changed = true;
            } else {
                changed = false;
            }
        } else {
            tagreadsToSend.clear();
            tagreadsToSend.addAll(incoming);
        }
    } else if ((options & ECReportOptions.REPORT_DELETIONS) > 0) {
        // remove the tags that appear in both sets
        tagreads.removeAll(incoming);
        // prepare tags that are about to be sent out
        // check if the set changes at all
        if ((options & ECReportOptions.REPORT_ONLY_ON_CHANGE) > 0) {
            if (!(tagreadsToSend.containsAll(incoming) && incoming.containsAll(tagreadsToSend))) {
                tagreadsToSend.clear();
                tagreadsToSend.addAll(tagreads);
                changed = true;
            } else {
                changed = false;
            }
        } else {
            tagreadsToSend.clear();
            tagreadsToSend.addAll(tagreads);
        }
        // create the current set of tags
        tagreads.addAll(incoming);
    } else if ((options & ECReportOptions.REPORT_CURRENT) > 0) {
        // check if the set changes at all
        if ((options & ECReportOptions.REPORT_ONLY_ON_CHANGE) > 0) {
            if (!(tagreadsToSend.containsAll(incoming) && incoming.containsAll(tagreadsToSend))) {
                tagreads.clear();
                tagreads.addAll(incoming);
                changed = true;
            } else {
                changed = false;
            }
        } else {
            tagreadsToSend.clear();
            tagreadsToSend.addAll(incoming);
        }
    } else {
        System.out.println("wtf?");
    }
}

From source file:org.sakaiproject.component.gradebook.GradebookServiceHibernateImpl.java

@SuppressWarnings("rawtypes")
@Override//w  w  w  .  j av  a2s. c om
public void transferGradebookDefinitionXml(String fromGradebookUid, String toGradebookUid,
        String fromGradebookXml) {
    final Gradebook gradebook = getGradebook(toGradebookUid);
    final Gradebook fromGradebook = getGradebook(fromGradebookUid);

    GradebookDefinition gradebookDefinition = (GradebookDefinition) VersionedExternalizable
            .fromXml(fromGradebookXml);

    gradebook.setCategory_type(gradebookDefinition.getCategoryType());
    gradebook.setGrade_type(gradebookDefinition.getGradeType());

    updateGradebook(gradebook);

    List category = getCategories(fromGradebook.getId());

    int assignmentsAddedCount = 0;
    Long catId = null;
    int undefined_nb = 0;

    List catList = gradebookDefinition.getCategory();
    List<Category> catList_tempt = new ArrayList<Category>();

    if (category.size() != 0) {
        //deal with category with assignments
        for (Iterator iter = category.iterator(); iter.hasNext();) {

            int categoryCount = 0;
            String catName = ((Category) iter.next()).getName();

            for (org.sakaiproject.service.gradebook.shared.Assignment obj : gradebookDefinition
                    .getAssignments()) {
                org.sakaiproject.service.gradebook.shared.Assignment assignmentDef = (org.sakaiproject.service.gradebook.shared.Assignment) obj;

                boolean newCategory = false;
                // Externally managed assessments should not be included.
                if (assignmentDef.isExternallyMaintained()) {
                    continue;
                }

                if (catName.equals(assignmentDef.getCategoryName())) {
                    newCategory = true;
                    categoryCount++;
                }

                if (assignmentDef.getCategoryName() != null) {
                    if (!newCategory) {
                    } else if (newCategory && categoryCount == 1) {
                        catId = createCategory(gradebook.getId(), assignmentDef.getCategoryName(),
                                assignmentDef.getWeight(), 0, 0, 0, assignmentDef.isCategoryExtraCredit());
                        Category catTempt = getCategory(catId);

                        catList_tempt.add(catTempt);
                        createAssignmentForCategory(gradebook.getId(), catId, assignmentDef.getName(),
                                assignmentDef.getPoints(), assignmentDef.getDueDate(), true, false,
                                assignmentDef.isExtraCredit());
                        assignmentsAddedCount++;
                    } else {
                        createAssignmentForCategory(gradebook.getId(), catId, assignmentDef.getName(),
                                assignmentDef.getPoints(), assignmentDef.getDueDate(), true, false,
                                assignmentDef.isExtraCredit());
                        assignmentsAddedCount++;
                    }

                }
                //deal with assignments in undefined.
                else {
                    if (undefined_nb == 0) {
                        createAssignment(gradebook.getId(), assignmentDef.getName(), assignmentDef.getPoints(),
                                assignmentDef.getDueDate(), true, false, assignmentDef.isExtraCredit());
                        assignmentsAddedCount++;

                    }
                }
            }
            undefined_nb++;
        }

        //deal with Category without assignments inside
        Iterator it_tempt = catList_tempt.iterator();
        Iterator it = catList.iterator();

        Category cat_cat;
        Category cat_tempt;

        while (it_tempt.hasNext()) {
            cat_tempt = (Category) it_tempt.next();
            while (it.hasNext()) {
                cat_cat = (Category) it.next();
                if (cat_tempt.getName().equals(cat_cat.getName())) {
                    it.remove();
                    it = catList.iterator();
                }
            }
            it = catList.iterator();
        }

        Iterator itUpdate = catList.iterator();
        while (itUpdate.hasNext()) {
            Category catObj = (Category) itUpdate.next();
            createCategory(gradebook.getId(), catObj.getName(), catObj.getWeight(), catObj.getDrop_lowest(),
                    catObj.getDropHighest(), catObj.getKeepHighest(), catObj.isExtraCredit());
        }
    }
    //deal with no categories
    else {
        for (org.sakaiproject.service.gradebook.shared.Assignment obj : gradebookDefinition.getAssignments()) {
            org.sakaiproject.service.gradebook.shared.Assignment assignmentDef = (org.sakaiproject.service.gradebook.shared.Assignment) obj;

            // Externally managed assessments should not be included.
            if (assignmentDef.isExternallyMaintained()) {
                continue;
            }

            // All assignments should be unreleased even if they were released in the original.

            createAssignment(gradebook.getId(), assignmentDef.getName(), assignmentDef.getPoints(),
                    assignmentDef.getDueDate(), true, false, assignmentDef.isExtraCredit());
            assignmentsAddedCount++;
        }

    }

    if (log.isInfoEnabled())
        log.info("Merge to gradebook " + toGradebookUid + " added " + assignmentsAddedCount + " assignments");

    // Carry over the old gradebook's selected grading scheme if possible.
    String fromGradingScaleUid = gradebookDefinition.getSelectedGradingScaleUid();
    MERGE_GRADE_MAPPING: if (!StringUtils.isEmpty(fromGradingScaleUid)) {
        for (GradeMapping gradeMapping : gradebook.getGradeMappings()) {
            if (gradeMapping.getGradingScale().getUid().equals(fromGradingScaleUid)) {
                // We have a match. Now make sure that the grades are as expected.
                Map<String, Double> inputGradePercents = gradebookDefinition
                        .getSelectedGradingScaleBottomPercents();
                Set<String> gradeCodes = (Set<String>) inputGradePercents.keySet();
                if (gradeCodes.containsAll(gradeMapping.getGradeMap().keySet())) {
                    // Modify the existing grade-to-percentage map.
                    for (String gradeCode : gradeCodes) {
                        gradeMapping.getGradeMap().put(gradeCode, inputGradePercents.get(gradeCode));
                    }
                    gradebook.setSelectedGradeMapping(gradeMapping);
                    updateGradebook(gradebook);
                    if (log.isInfoEnabled())
                        log.info("Merge to gradebook " + toGradebookUid + " updated grade mapping");
                } else {
                    if (log.isInfoEnabled())
                        log.info("Merge to gradebook " + toGradebookUid
                                + " skipped grade mapping change because the " + fromGradingScaleUid
                                + " grade codes did not match");
                }
                break MERGE_GRADE_MAPPING;
            }
        }
        // Did not find a matching grading scale.
        if (log.isInfoEnabled())
            log.info("Merge to gradebook " + toGradebookUid
                    + " skipped grade mapping change because grading scale " + fromGradingScaleUid
                    + " is not defined");
    }
}

From source file:de.uni_koblenz.jgralab.utilities.tgschema2java.TgSchema2Java.java

/**
 * Checks if all .java-files belonging to the specified Schema already exist
 * in the commit path. There also must not exist any surplus .java-files.
 * //from   ww w.j ava  2 s .c  o  m
 * @param schema
 *            the Schema whose files shall be checked for existence
 * @return true if all .java-files already exist; false otherwise
 */
private boolean isExistingSchema(Schema schema) {
    String pathName;
    String schemaPath = schema.getPathName();
    Set<String> existingFilePaths = getGeneratedFilePaths(commitPath + File.separator + schemaPath);
    Set<String> requiredFilePaths = new HashSet<>();

    requiredFilePaths.add(commitPath + File.separator + schema.getFileName() + ".java");
    requiredFilePaths.add(commitPath + File.separator + schema.getPathName() + File.separator + "impl"
            + File.separator + schema.getName() + "Factory.java");
    for (Domain d : schema.getDomains()) {
        pathName = d.getPathName();

        if (!pathName.isEmpty()) {
            pathName = pathName.concat(File.separator);
        }
        if (d.toString().startsWith("Enum") || d.toString().startsWith("Record")) {
            requiredFilePaths.add(commitPath + File.separator + schemaPath + File.separator + pathName
                    + d.getSimpleName() + ".java");
        }
    }

    GraphClass gc = schema.getGraphClass();
    requiredFilePaths
            .add(commitPath + File.separator + schemaPath + File.separator + gc.getFileName() + ".java");

    pathName = gc.getPathName();

    if (!pathName.isEmpty()) {
        pathName = pathName.concat(File.separator);
    }
    requiredFilePaths.add(commitPath + File.separator + schemaPath + File.separator + "impl" + File.separator
            + pathName + gc.getSimpleName() + "Impl.java");

    for (VertexClass vc : schema.getGraphClass().getVertexClasses()) {
        requiredFilePaths
                .add(commitPath + File.separator + schemaPath + File.separator + vc.getFileName() + ".java");
        if (!vc.isAbstract()) {
            pathName = vc.getPathName();

            if (!pathName.isEmpty()) {
                pathName = pathName.concat(File.separator);
            }
            requiredFilePaths.add(commitPath + File.separator + schemaPath + File.separator + "impl"
                    + File.separator + pathName + vc.getSimpleName() + "Impl.java");
        }
    }

    for (EdgeClass ec : schema.getGraphClass().getEdgeClasses()) {
        requiredFilePaths
                .add(commitPath + File.separator + schemaPath + File.separator + ec.getFileName() + ".java");
        if (!ec.isAbstract()) {
            pathName = ec.getPathName();

            if (!pathName.isEmpty()) {
                pathName = pathName.concat(File.separator);
            }
            requiredFilePaths.add(commitPath + File.separator + schemaPath + File.separator + "impl"
                    + File.separator + pathName + ec.getSimpleName() + "Impl.java");
            requiredFilePaths.add(commitPath + File.separator + schemaPath + File.separator + "impl"
                    + File.separator + pathName + "Reversed" + ec.getSimpleName() + "Impl.java");
        }
    }

    /*
     * checks if sets of existing and required .java-files are equal
     */
    if (!existingFilePaths.containsAll(requiredFilePaths)
            || !requiredFilePaths.containsAll(existingFilePaths)) {
        return false;
    }
    return true;
}

From source file:com.datatorrent.stram.StreamingContainerManager.java

/**
 * process the heartbeat from each container.
 * called by the RPC thread for each container. (i.e. called by multiple threads)
 *
 * @param heartbeat//from w  w w .  j av a 2 s  .  c  om
 * @return heartbeat response
 */
@SuppressWarnings("StatementWithEmptyBody")
public ContainerHeartbeatResponse processHeartbeat(ContainerHeartbeat heartbeat) {
    long currentTimeMillis = clock.getTime();

    final StreamingContainerAgent sca = this.containers.get(heartbeat.getContainerId());
    if (sca == null || sca.container.getState() == PTContainer.State.KILLED) {
        // could be orphaned container that was replaced and needs to terminate
        LOG.error("Unknown container {}", heartbeat.getContainerId());
        ContainerHeartbeatResponse response = new ContainerHeartbeatResponse();
        response.shutdown = true;
        return response;
    }

    //LOG.debug("{} {} {}", new Object[]{sca.container.containerId, sca.container.bufferServerAddress, sca.container.getState()});
    if (sca.container.getState() == PTContainer.State.ALLOCATED) {
        // capture dynamically assigned address from container
        if (sca.container.bufferServerAddress == null && heartbeat.bufferServerHost != null) {
            sca.container.bufferServerAddress = InetSocketAddress.createUnresolved(heartbeat.bufferServerHost,
                    heartbeat.bufferServerPort);
            LOG.info("Container {} buffer server: {}", sca.container.getExternalId(),
                    sca.container.bufferServerAddress);
        }
        final long containerStartTime = System.currentTimeMillis();
        sca.container.setState(PTContainer.State.ACTIVE);
        sca.container.setStartedTime(containerStartTime);
        sca.container.setFinishedTime(-1);
        sca.jvmName = heartbeat.jvmName;
        poolExecutor.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    containerFile.append(sca.getContainerInfo());
                } catch (IOException ex) {
                    LOG.warn("Cannot write to container file");
                }
                for (PTOperator ptOp : sca.container.getOperators()) {
                    try {
                        JSONObject operatorInfo = new JSONObject();
                        operatorInfo.put("name", ptOp.getName());
                        operatorInfo.put("id", ptOp.getId());
                        operatorInfo.put("container", sca.container.getExternalId());
                        operatorInfo.put("startTime", containerStartTime);
                        operatorFile.append(operatorInfo);
                    } catch (IOException | JSONException ex) {
                        LOG.warn("Cannot write to operator file: ", ex);
                    }
                }
            }
        });
    }

    if (heartbeat.restartRequested) {
        LOG.error("Container {} restart request", sca.container.getExternalId());
        containerStopRequests.put(sca.container.getExternalId(), sca.container.getExternalId());
    }

    sca.memoryMBFree = heartbeat.memoryMBFree;
    sca.gcCollectionCount = heartbeat.gcCollectionCount;
    sca.gcCollectionTime = heartbeat.gcCollectionTime;

    sca.undeployOpers.clear();
    sca.deployOpers.clear();
    if (!this.deployChangeInProgress.get()) {
        sca.deployCnt = this.deployChangeCnt;
    }
    Set<Integer> reportedOperators = Sets.newHashSetWithExpectedSize(sca.container.getOperators().size());

    for (OperatorHeartbeat shb : heartbeat.getContainerStats().operators) {

        long maxEndWindowTimestamp = 0;

        reportedOperators.add(shb.nodeId);
        PTOperator oper = this.plan.getAllOperators().get(shb.getNodeId());

        if (oper == null) {
            LOG.info("Heartbeat for unknown operator {} (container {})", shb.getNodeId(),
                    heartbeat.getContainerId());
            sca.undeployOpers.add(shb.nodeId);
            continue;
        }

        if (shb.requestResponse != null) {
            for (StatsListener.OperatorResponse obj : shb.requestResponse) {
                if (obj instanceof OperatorResponse) { // This is to identify platform requests
                    commandResponse.put((Long) obj.getResponseId(), obj.getResponse());
                    LOG.debug(" Got back the response {} for the request {}", obj, obj.getResponseId());
                } else { // This is to identify user requests
                    oper.stats.responses.add(obj);
                }
            }
        }

        //LOG.debug("heartbeat {} {}/{} {}", oper, oper.getState(), shb.getState(), oper.getContainer().getExternalId());
        if (!(oper.getState() == PTOperator.State.ACTIVE
                && shb.getState() == OperatorHeartbeat.DeployState.ACTIVE)) {
            // deploy state may require synchronization
            processOperatorDeployStatus(oper, shb, sca);
        }

        oper.stats.lastHeartbeat = shb;
        List<ContainerStats.OperatorStats> statsList = shb.getOperatorStatsContainer();

        if (!statsList.isEmpty()) {
            long tuplesProcessed = 0;
            long tuplesEmitted = 0;
            long totalCpuTimeUsed = 0;
            int statCount = 0;
            long maxDequeueTimestamp = -1;
            oper.stats.recordingId = null;

            final OperatorStatus status = oper.stats;
            status.statsRevs.checkout();

            for (Map.Entry<String, PortStatus> entry : status.inputPortStatusList.entrySet()) {
                entry.getValue().recordingId = null;
            }
            for (Map.Entry<String, PortStatus> entry : status.outputPortStatusList.entrySet()) {
                entry.getValue().recordingId = null;
            }
            for (ContainerStats.OperatorStats stats : statsList) {
                if (stats == null) {
                    LOG.warn("Operator {} statistics list contains null element", shb.getNodeId());
                    continue;
                }

                /* report checkpoint-ed WindowId status of the operator */
                if (stats.checkpoint instanceof Checkpoint) {
                    if (oper.getRecentCheckpoint() == null
                            || oper.getRecentCheckpoint().windowId < stats.checkpoint.getWindowId()) {
                        addCheckpoint(oper, (Checkpoint) stats.checkpoint);
                        if (stats.checkpointStats != null) {
                            status.checkpointStats = stats.checkpointStats;
                            status.checkpointTimeMA.add(stats.checkpointStats.checkpointTime);
                        }
                        oper.failureCount = 0;
                    }
                }

                oper.stats.recordingId = stats.recordingId;

                /* report all the other stuff */

                // calculate the stats related to end window
                EndWindowStats endWindowStats = new EndWindowStats(); // end window stats for a particular window id for a particular node
                Collection<ContainerStats.OperatorStats.PortStats> ports = stats.inputPorts;
                if (ports != null) {
                    Set<String> currentInputPortSet = Sets.newHashSetWithExpectedSize(ports.size());
                    for (ContainerStats.OperatorStats.PortStats s : ports) {
                        currentInputPortSet.add(s.id);
                        PortStatus ps = status.inputPortStatusList.get(s.id);
                        if (ps == null) {
                            ps = status.new PortStatus();
                            ps.portName = s.id;
                            status.inputPortStatusList.put(s.id, ps);
                        }
                        ps.totalTuples += s.tupleCount;
                        ps.recordingId = s.recordingId;

                        tuplesProcessed += s.tupleCount;
                        endWindowStats.dequeueTimestamps.put(s.id, s.endWindowTimestamp);

                        Pair<Integer, String> operatorPortName = new Pair<>(oper.getId(), s.id);
                        Long lastEndWindowTimestamp = operatorPortLastEndWindowTimestamps.get(operatorPortName);
                        if (lastEndWindowTimestamp == null) {
                            lastEndWindowTimestamp = lastStatsTimestamp;
                        }
                        long portElapsedMillis = Math.max(s.endWindowTimestamp - lastEndWindowTimestamp, 0);
                        //LOG.debug("=== PROCESSED TUPLE COUNT for {}: {}, {}, {}, {}", operatorPortName, s.tupleCount, portElapsedMillis, operatorPortLastEndWindowTimestamps.get(operatorPortName), lastStatsTimestamp);
                        ps.tuplesPMSMA.add(s.tupleCount, portElapsedMillis);
                        ps.bufferServerBytesPMSMA.add(s.bufferServerBytes, portElapsedMillis);
                        ps.queueSizeMA.add(s.queueSize);

                        operatorPortLastEndWindowTimestamps.put(operatorPortName, s.endWindowTimestamp);
                        if (maxEndWindowTimestamp < s.endWindowTimestamp) {
                            maxEndWindowTimestamp = s.endWindowTimestamp;
                        }
                        if (s.endWindowTimestamp > maxDequeueTimestamp) {
                            maxDequeueTimestamp = s.endWindowTimestamp;
                        }
                    }
                    // need to remove dead ports, for unifiers
                    Iterator<Map.Entry<String, PortStatus>> it = status.inputPortStatusList.entrySet()
                            .iterator();
                    while (it.hasNext()) {
                        Map.Entry<String, PortStatus> entry = it.next();
                        if (!currentInputPortSet.contains(entry.getKey())) {
                            it.remove();
                        }
                    }
                }

                ports = stats.outputPorts;
                if (ports != null) {
                    Set<String> currentOutputPortSet = Sets.newHashSetWithExpectedSize(ports.size());
                    for (ContainerStats.OperatorStats.PortStats s : ports) {
                        currentOutputPortSet.add(s.id);
                        PortStatus ps = status.outputPortStatusList.get(s.id);
                        if (ps == null) {
                            ps = status.new PortStatus();
                            ps.portName = s.id;
                            status.outputPortStatusList.put(s.id, ps);
                        }
                        ps.totalTuples += s.tupleCount;
                        ps.recordingId = s.recordingId;

                        tuplesEmitted += s.tupleCount;
                        Pair<Integer, String> operatorPortName = new Pair<>(oper.getId(), s.id);
                        Long lastEndWindowTimestamp = operatorPortLastEndWindowTimestamps.get(operatorPortName);
                        if (lastEndWindowTimestamp == null) {
                            lastEndWindowTimestamp = lastStatsTimestamp;
                        }
                        long portElapsedMillis = Math.max(s.endWindowTimestamp - lastEndWindowTimestamp, 0);
                        //LOG.debug("=== EMITTED TUPLE COUNT for {}: {}, {}, {}, {}", operatorPortName, s.tupleCount, portElapsedMillis, operatorPortLastEndWindowTimestamps.get(operatorPortName), lastStatsTimestamp);
                        ps.tuplesPMSMA.add(s.tupleCount, portElapsedMillis);
                        ps.bufferServerBytesPMSMA.add(s.bufferServerBytes, portElapsedMillis);

                        operatorPortLastEndWindowTimestamps.put(operatorPortName, s.endWindowTimestamp);
                        if (maxEndWindowTimestamp < s.endWindowTimestamp) {
                            maxEndWindowTimestamp = s.endWindowTimestamp;
                        }
                    }
                    if (ports.size() > 0) {
                        endWindowStats.emitTimestamp = ports.iterator().next().endWindowTimestamp;
                    }
                    // need to remove dead ports, for unifiers
                    Iterator<Map.Entry<String, PortStatus>> it = status.outputPortStatusList.entrySet()
                            .iterator();
                    while (it.hasNext()) {
                        Map.Entry<String, PortStatus> entry = it.next();
                        if (!currentOutputPortSet.contains(entry.getKey())) {
                            it.remove();
                        }
                    }
                }

                // for output operator, just take the maximum dequeue time for emit timestamp.
                // (we don't know the latency for output operators because they don't emit tuples)
                if (endWindowStats.emitTimestamp < 0) {
                    endWindowStats.emitTimestamp = maxDequeueTimestamp;
                }

                if (status.currentWindowId.get() != stats.windowId) {
                    status.lastWindowIdChangeTms = currentTimeMillis;
                    status.currentWindowId.set(stats.windowId);
                }
                totalCpuTimeUsed += stats.cpuTimeUsed;
                statCount++;

                if (oper.getOperatorMeta().getValue(OperatorContext.COUNTERS_AGGREGATOR) != null) {
                    endWindowStats.counters = stats.counters;
                }
                if (oper.getOperatorMeta().getMetricAggregatorMeta() != null
                        && oper.getOperatorMeta().getMetricAggregatorMeta().getAggregator() != null) {
                    endWindowStats.metrics = stats.metrics;
                }

                if (stats.windowId > currentEndWindowStatsWindowId) {
                    Map<Integer, EndWindowStats> endWindowStatsMap = endWindowStatsOperatorMap
                            .get(stats.windowId);
                    if (endWindowStatsMap == null) {
                        endWindowStatsMap = new ConcurrentSkipListMap<Integer, EndWindowStats>();
                        Map<Integer, EndWindowStats> endWindowStatsMapPrevious = endWindowStatsOperatorMap
                                .putIfAbsent(stats.windowId, endWindowStatsMap);
                        if (endWindowStatsMapPrevious != null) {
                            endWindowStatsMap = endWindowStatsMapPrevious;
                        }
                    }
                    endWindowStatsMap.put(shb.getNodeId(), endWindowStats);

                    if (!oper.getInputs().isEmpty()) {
                        long latency = Long.MAX_VALUE;
                        long adjustedEndWindowEmitTimestamp = endWindowStats.emitTimestamp;
                        MovingAverageLong rpcLatency = rpcLatencies.get(oper.getContainer().getExternalId());
                        if (rpcLatency != null) {
                            adjustedEndWindowEmitTimestamp += rpcLatency.getAvg();
                        }
                        PTOperator slowestUpstream = null;
                        for (PTInput input : oper.getInputs()) {
                            PTOperator upstreamOp = input.source.source;
                            if (upstreamOp.getOperatorMeta().getOperator() instanceof Operator.DelayOperator) {
                                continue;
                            }
                            EndWindowStats ews = endWindowStatsMap.get(upstreamOp.getId());
                            long portLatency;
                            if (ews == null) {
                                // This is when the operator is likely to be behind too many windows. We need to give an estimate for
                                // latency at this point, by looking at the number of windows behind
                                int widthMillis = plan.getLogicalPlan()
                                        .getValue(LogicalPlan.STREAMING_WINDOW_SIZE_MILLIS);
                                portLatency = (upstreamOp.stats.currentWindowId.get()
                                        - oper.stats.currentWindowId.get()) * widthMillis;
                            } else {
                                MovingAverageLong upstreamRPCLatency = rpcLatencies
                                        .get(upstreamOp.getContainer().getExternalId());
                                portLatency = adjustedEndWindowEmitTimestamp - ews.emitTimestamp;
                                if (upstreamRPCLatency != null) {
                                    portLatency -= upstreamRPCLatency.getAvg();
                                }
                            }
                            if (portLatency < 0) {
                                portLatency = 0;
                            }
                            if (latency > portLatency) {
                                latency = portLatency;
                                slowestUpstream = upstreamOp;
                            }
                        }
                        status.latencyMA.add(latency);
                        slowestUpstreamOp.put(oper, slowestUpstream);
                    }

                    Set<Integer> allCurrentOperators = plan.getAllOperators().keySet();
                    int numOperators = plan.getAllOperators().size();
                    if (allCurrentOperators.containsAll(endWindowStatsMap.keySet())
                            && endWindowStatsMap.size() == numOperators) {
                        completeEndWindowStatsWindowId = stats.windowId;
                    }
                }
            }

            status.totalTuplesProcessed.add(tuplesProcessed);
            status.totalTuplesEmitted.add(tuplesEmitted);
            OperatorMeta logicalOperator = oper.getOperatorMeta();
            LogicalOperatorStatus logicalStatus = logicalOperator.getStatus();
            if (!oper.isUnifier()) {
                logicalStatus.totalTuplesProcessed += tuplesProcessed;
                logicalStatus.totalTuplesEmitted += tuplesEmitted;
            }
            long lastMaxEndWindowTimestamp = operatorLastEndWindowTimestamps.containsKey(oper.getId())
                    ? operatorLastEndWindowTimestamps.get(oper.getId())
                    : lastStatsTimestamp;
            if (maxEndWindowTimestamp >= lastMaxEndWindowTimestamp) {
                double tuplesProcessedPMSMA = 0.0;
                double tuplesEmittedPMSMA = 0.0;
                if (statCount != 0) {
                    //LOG.debug("CPU for {}: {} / {} - {}", oper.getId(), totalCpuTimeUsed, maxEndWindowTimestamp, lastMaxEndWindowTimestamp);
                    status.cpuNanosPMSMA.add(totalCpuTimeUsed,
                            maxEndWindowTimestamp - lastMaxEndWindowTimestamp);
                }

                for (PortStatus ps : status.inputPortStatusList.values()) {
                    tuplesProcessedPMSMA += ps.tuplesPMSMA.getAvg();
                }
                for (PortStatus ps : status.outputPortStatusList.values()) {
                    tuplesEmittedPMSMA += ps.tuplesPMSMA.getAvg();
                }
                status.tuplesProcessedPSMA.set(Math.round(tuplesProcessedPMSMA * 1000));
                status.tuplesEmittedPSMA.set(Math.round(tuplesEmittedPMSMA * 1000));
            } else {
                //LOG.warn("This timestamp for {} is lower than the previous!! {} < {}", oper.getId(), maxEndWindowTimestamp, lastMaxEndWindowTimestamp);
            }
            operatorLastEndWindowTimestamps.put(oper.getId(), maxEndWindowTimestamp);
            status.listenerStats.add(statsList);
            this.reportStats.put(oper, oper);

            status.statsRevs.commit();
        }
        if (lastStatsTimestamp < maxEndWindowTimestamp) {
            lastStatsTimestamp = maxEndWindowTimestamp;
        }
    }

    sca.lastHeartbeatMillis = currentTimeMillis;

    for (PTOperator oper : sca.container.getOperators()) {
        if (!reportedOperators.contains(oper.getId())) {
            processOperatorDeployStatus(oper, null, sca);
        }
    }

    ContainerHeartbeatResponse rsp = getHeartbeatResponse(sca);

    if (heartbeat.getContainerStats().operators.isEmpty() && isApplicationIdle()) {
        LOG.info("requesting idle shutdown for container {}", heartbeat.getContainerId());
        rsp.shutdown = true;
    } else {
        if (sca.shutdownRequested) {
            LOG.info("requesting shutdown for container {}", heartbeat.getContainerId());
            rsp.shutdown = true;
        }
    }

    List<StramToNodeRequest> requests = rsp.nodeRequests != null ? rsp.nodeRequests
            : new ArrayList<StramToNodeRequest>();
    ConcurrentLinkedQueue<StramToNodeRequest> operatorRequests = sca.getOperatorRequests();
    while (true) {
        StramToNodeRequest r = operatorRequests.poll();
        if (r == null) {
            break;
        }
        requests.add(r);
    }
    rsp.nodeRequests = requests;
    rsp.committedWindowId = committedWindowId;
    return rsp;
}

From source file:org.mitre.openid.connect.service.impl.TestMITREidDataService_1_3.java

@Test
public void testExportAuthenticationHolders() throws IOException {
    OAuth2Request req1 = new OAuth2Request(new HashMap<String, String>(), "client1",
            new ArrayList<GrantedAuthority>(), true, new HashSet<String>(), new HashSet<String>(),
            "http://foo.com", new HashSet<String>(), null);
    Authentication mockAuth1 = new UsernamePasswordAuthenticationToken("user1", "pass1",
            AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
    OAuth2Authentication auth1 = new OAuth2Authentication(req1, mockAuth1);

    AuthenticationHolderEntity holder1 = new AuthenticationHolderEntity();
    holder1.setId(1L);/*w w w.  jav  a2s  .  c  o m*/
    holder1.setAuthentication(auth1);

    OAuth2Request req2 = new OAuth2Request(new HashMap<String, String>(), "client2",
            new ArrayList<GrantedAuthority>(), true, new HashSet<String>(), new HashSet<String>(),
            "http://bar.com", new HashSet<String>(), null);
    OAuth2Authentication auth2 = new OAuth2Authentication(req2, null);

    AuthenticationHolderEntity holder2 = new AuthenticationHolderEntity();
    holder2.setId(2L);
    holder2.setAuthentication(auth2);

    List<AuthenticationHolderEntity> allAuthHolders = ImmutableList.of(holder1, holder2);

    when(clientRepository.getAllClients()).thenReturn(new HashSet<ClientDetailsEntity>());
    when(approvedSiteRepository.getAll()).thenReturn(new HashSet<ApprovedSite>());
    when(wlSiteRepository.getAll()).thenReturn(new HashSet<WhitelistedSite>());
    when(blSiteRepository.getAll()).thenReturn(new HashSet<BlacklistedSite>());
    when(authHolderRepository.getAll()).thenReturn(allAuthHolders);
    when(tokenRepository.getAllAccessTokens()).thenReturn(new HashSet<OAuth2AccessTokenEntity>());
    when(tokenRepository.getAllRefreshTokens()).thenReturn(new HashSet<OAuth2RefreshTokenEntity>());
    when(sysScopeRepository.getAll()).thenReturn(new HashSet<SystemScope>());

    // do the data export
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(stringWriter);
    writer.beginObject();
    dataService.exportData(writer);
    writer.endObject();
    writer.close();

    // parse the output as a JSON object for testing
    JsonElement elem = new JsonParser().parse(stringWriter.toString());
    JsonObject root = elem.getAsJsonObject();

    // make sure the root is there
    assertThat(root.has(MITREidDataService.MITREID_CONNECT_1_3), is(true));

    JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_3).getAsJsonObject();

    // make sure all the root elements are there
    assertThat(config.has(MITREidDataService.CLIENTS), is(true));
    assertThat(config.has(MITREidDataService.GRANTS), is(true));
    assertThat(config.has(MITREidDataService.WHITELISTEDSITES), is(true));
    assertThat(config.has(MITREidDataService.BLACKLISTEDSITES), is(true));
    assertThat(config.has(MITREidDataService.REFRESHTOKENS), is(true));
    assertThat(config.has(MITREidDataService.ACCESSTOKENS), is(true));
    assertThat(config.has(MITREidDataService.SYSTEMSCOPES), is(true));
    assertThat(config.has(MITREidDataService.AUTHENTICATIONHOLDERS), is(true));

    // make sure the root elements are all arrays
    assertThat(config.get(MITREidDataService.CLIENTS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.GRANTS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.WHITELISTEDSITES).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.BLACKLISTEDSITES).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.REFRESHTOKENS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.ACCESSTOKENS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.SYSTEMSCOPES).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.AUTHENTICATIONHOLDERS).isJsonArray(), is(true));

    // check our holder list (this test)
    JsonArray holders = config.get(MITREidDataService.AUTHENTICATIONHOLDERS).getAsJsonArray();

    assertThat(holders.size(), is(2));
    // check for both of our clients in turn
    Set<AuthenticationHolderEntity> checked = new HashSet<>();
    for (JsonElement e : holders) {
        assertThat(e.isJsonObject(), is(true));
        JsonObject holder = e.getAsJsonObject();

        AuthenticationHolderEntity compare = null;
        if (holder.get("id").getAsLong() == holder1.getId()) {
            compare = holder1;
        } else if (holder.get("id").getAsLong() == holder2.getId()) {
            compare = holder2;
        }

        if (compare == null) {
            fail("Could not find matching authentication holder id: " + holder.get("id").getAsString());
        } else {
            assertTrue(holder.get("clientId").getAsString().equals(compare.getClientId()));
            assertTrue(holder.get("approved").getAsBoolean() == compare.isApproved());
            assertTrue(holder.get("redirectUri").getAsString().equals(compare.getRedirectUri()));
            if (compare.getUserAuth() != null) {
                assertTrue(holder.get("savedUserAuthentication").isJsonObject());
                JsonObject savedAuth = holder.get("savedUserAuthentication").getAsJsonObject();
                assertTrue(savedAuth.get("name").getAsString().equals(compare.getUserAuth().getName()));
                assertTrue(savedAuth.get("authenticated").getAsBoolean() == compare.getUserAuth()
                        .isAuthenticated());
                assertTrue(savedAuth.get("sourceClass").getAsString()
                        .equals(compare.getUserAuth().getSourceClass()));
            }
            checked.add(compare);
        }
    }
    // make sure all of our clients were found
    assertThat(checked.containsAll(allAuthHolders), is(true));
}

From source file:org.mitre.openid.connect.service.impl.TestMITREidDataService_1_2.java

@Test
public void testExportClients() throws IOException {
    ClientDetailsEntity client1 = new ClientDetailsEntity();
    client1.setId(1L);/*from w ww .j av  a2s. c om*/
    client1.setAccessTokenValiditySeconds(3600);
    client1.setClientId("client1");
    client1.setClientSecret("clientsecret1");
    client1.setRedirectUris(ImmutableSet.of("http://foo.com/"));
    client1.setScope(ImmutableSet.of("foo", "bar", "baz", "dolphin"));
    client1.setGrantTypes(ImmutableSet.of("implicit", "authorization_code",
            "urn:ietf:params:oauth:grant_type:redelegate", "refresh_token"));
    client1.setAllowIntrospection(true);

    ClientDetailsEntity client2 = new ClientDetailsEntity();
    client2.setId(2L);
    client2.setAccessTokenValiditySeconds(3600);
    client2.setClientId("client2");
    client2.setClientSecret("clientsecret2");
    client2.setRedirectUris(ImmutableSet.of("http://bar.baz.com/"));
    client2.setScope(ImmutableSet.of("foo", "dolphin", "electric-wombat"));
    client2.setGrantTypes(ImmutableSet.of("client_credentials", "urn:ietf:params:oauth:grant_type:redelegate"));
    client2.setAllowIntrospection(false);

    Set<ClientDetailsEntity> allClients = ImmutableSet.of(client1, client2);

    Mockito.when(clientRepository.getAllClients()).thenReturn(allClients);
    Mockito.when(approvedSiteRepository.getAll()).thenReturn(new HashSet<ApprovedSite>());
    Mockito.when(wlSiteRepository.getAll()).thenReturn(new HashSet<WhitelistedSite>());
    Mockito.when(blSiteRepository.getAll()).thenReturn(new HashSet<BlacklistedSite>());
    Mockito.when(authHolderRepository.getAll()).thenReturn(new ArrayList<AuthenticationHolderEntity>());
    Mockito.when(tokenRepository.getAllAccessTokens()).thenReturn(new HashSet<OAuth2AccessTokenEntity>());
    Mockito.when(tokenRepository.getAllRefreshTokens()).thenReturn(new HashSet<OAuth2RefreshTokenEntity>());
    Mockito.when(sysScopeRepository.getAll()).thenReturn(new HashSet<SystemScope>());

    // do the data export
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(stringWriter);
    writer.beginObject();
    dataService.exportData(writer);
    writer.endObject();
    writer.close();

    // parse the output as a JSON object for testing
    JsonElement elem = new JsonParser().parse(stringWriter.toString());
    JsonObject root = elem.getAsJsonObject();

    // make sure the root is there
    assertThat(root.has(MITREidDataService.MITREID_CONNECT_1_2), is(true));

    JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_2).getAsJsonObject();

    // make sure all the root elements are there
    assertThat(config.has(MITREidDataService.CLIENTS), is(true));
    assertThat(config.has(MITREidDataService.GRANTS), is(true));
    assertThat(config.has(MITREidDataService.WHITELISTEDSITES), is(true));
    assertThat(config.has(MITREidDataService.BLACKLISTEDSITES), is(true));
    assertThat(config.has(MITREidDataService.REFRESHTOKENS), is(true));
    assertThat(config.has(MITREidDataService.ACCESSTOKENS), is(true));
    assertThat(config.has(MITREidDataService.SYSTEMSCOPES), is(true));
    assertThat(config.has(MITREidDataService.AUTHENTICATIONHOLDERS), is(true));

    // make sure the root elements are all arrays
    assertThat(config.get(MITREidDataService.CLIENTS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.GRANTS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.WHITELISTEDSITES).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.BLACKLISTEDSITES).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.REFRESHTOKENS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.ACCESSTOKENS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.SYSTEMSCOPES).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.AUTHENTICATIONHOLDERS).isJsonArray(), is(true));

    // check our client list (this test)
    JsonArray clients = config.get(MITREidDataService.CLIENTS).getAsJsonArray();

    assertThat(clients.size(), is(2));
    // check for both of our clients in turn
    Set<ClientDetailsEntity> checked = new HashSet<>();
    for (JsonElement e : clients) {
        assertThat(e.isJsonObject(), is(true));
        JsonObject client = e.getAsJsonObject();

        ClientDetailsEntity compare = null;
        if (client.get("clientId").getAsString().equals(client1.getClientId())) {
            compare = client1;
        } else if (client.get("clientId").getAsString().equals(client2.getClientId())) {
            compare = client2;
        }

        if (compare == null) {
            fail("Could not find matching clientId: " + client.get("clientId").getAsString());
        } else {
            assertThat(client.get("clientId").getAsString(), equalTo(compare.getClientId()));
            assertThat(client.get("secret").getAsString(), equalTo(compare.getClientSecret()));
            assertThat(client.get("accessTokenValiditySeconds").getAsInt(),
                    equalTo(compare.getAccessTokenValiditySeconds()));
            assertThat(client.get("allowIntrospection").getAsBoolean(),
                    equalTo(compare.isAllowIntrospection()));
            assertThat(jsonArrayToStringSet(client.get("redirectUris").getAsJsonArray()),
                    equalTo(compare.getRedirectUris()));
            assertThat(jsonArrayToStringSet(client.get("scope").getAsJsonArray()), equalTo(compare.getScope()));
            assertThat(jsonArrayToStringSet(client.get("grantTypes").getAsJsonArray()),
                    equalTo(compare.getGrantTypes()));
            checked.add(compare);
        }
    }
    // make sure all of our clients were found
    assertThat(checked.containsAll(allClients), is(true));
}

From source file:org.mitre.openid.connect.service.impl.TestMITREidDataService_1_3.java

@Test
public void testExportGrants() throws IOException, ParseException {
    Date creationDate1 = formatter.parse("2014-09-10T22:49:44.090+0000", Locale.ENGLISH);
    Date accessDate1 = formatter.parse("2014-09-10T23:49:44.090+0000", Locale.ENGLISH);

    OAuth2AccessTokenEntity mockToken1 = mock(OAuth2AccessTokenEntity.class);
    when(mockToken1.getId()).thenReturn(1L);

    ApprovedSite site1 = new ApprovedSite();
    site1.setId(1L);/*  w w w  .  j a v a  2s .c  o m*/
    site1.setClientId("foo");
    site1.setCreationDate(creationDate1);
    site1.setAccessDate(accessDate1);
    site1.setUserId("user1");
    site1.setAllowedScopes(ImmutableSet.of("openid", "phone"));
    when(mockToken1.getApprovedSite()).thenReturn(site1);

    Date creationDate2 = formatter.parse("2014-09-11T18:49:44.090+0000", Locale.ENGLISH);
    Date accessDate2 = formatter.parse("2014-09-11T20:49:44.090+0000", Locale.ENGLISH);
    Date timeoutDate2 = formatter.parse("2014-10-01T20:49:44.090+0000", Locale.ENGLISH);

    ApprovedSite site2 = new ApprovedSite();
    site2.setId(2L);
    site2.setClientId("bar");
    site2.setCreationDate(creationDate2);
    site2.setAccessDate(accessDate2);
    site2.setUserId("user2");
    site2.setAllowedScopes(ImmutableSet.of("openid", "offline_access", "email", "profile"));
    site2.setTimeoutDate(timeoutDate2);

    Set<ApprovedSite> allApprovedSites = ImmutableSet.of(site1, site2);

    Mockito.when(clientRepository.getAllClients()).thenReturn(new HashSet<ClientDetailsEntity>());
    Mockito.when(approvedSiteRepository.getAll()).thenReturn(allApprovedSites);
    Mockito.when(blSiteRepository.getAll()).thenReturn(new HashSet<BlacklistedSite>());
    Mockito.when(wlSiteRepository.getAll()).thenReturn(new HashSet<WhitelistedSite>());
    Mockito.when(authHolderRepository.getAll()).thenReturn(new ArrayList<AuthenticationHolderEntity>());
    Mockito.when(tokenRepository.getAllAccessTokens()).thenReturn(new HashSet<OAuth2AccessTokenEntity>());
    Mockito.when(tokenRepository.getAllRefreshTokens()).thenReturn(new HashSet<OAuth2RefreshTokenEntity>());
    Mockito.when(sysScopeRepository.getAll()).thenReturn(new HashSet<SystemScope>());

    // do the data export
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(stringWriter);
    writer.beginObject();
    dataService.exportData(writer);
    writer.endObject();
    writer.close();

    // parse the output as a JSON object for testing
    JsonElement elem = new JsonParser().parse(stringWriter.toString());
    JsonObject root = elem.getAsJsonObject();

    // make sure the root is there
    assertThat(root.has(MITREidDataService.MITREID_CONNECT_1_3), is(true));

    JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_3).getAsJsonObject();

    // make sure all the root elements are there
    assertThat(config.has(MITREidDataService.CLIENTS), is(true));
    assertThat(config.has(MITREidDataService.GRANTS), is(true));
    assertThat(config.has(MITREidDataService.WHITELISTEDSITES), is(true));
    assertThat(config.has(MITREidDataService.BLACKLISTEDSITES), is(true));
    assertThat(config.has(MITREidDataService.REFRESHTOKENS), is(true));
    assertThat(config.has(MITREidDataService.ACCESSTOKENS), is(true));
    assertThat(config.has(MITREidDataService.SYSTEMSCOPES), is(true));
    assertThat(config.has(MITREidDataService.AUTHENTICATIONHOLDERS), is(true));

    // make sure the root elements are all arrays
    assertThat(config.get(MITREidDataService.CLIENTS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.GRANTS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.WHITELISTEDSITES).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.BLACKLISTEDSITES).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.REFRESHTOKENS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.ACCESSTOKENS).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.SYSTEMSCOPES).isJsonArray(), is(true));
    assertThat(config.get(MITREidDataService.AUTHENTICATIONHOLDERS).isJsonArray(), is(true));

    // check our scope list (this test)
    JsonArray sites = config.get(MITREidDataService.GRANTS).getAsJsonArray();

    assertThat(sites.size(), is(2));
    // check for both of our sites in turn
    Set<ApprovedSite> checked = new HashSet<>();
    for (JsonElement e : sites) {
        assertThat(e.isJsonObject(), is(true));
        JsonObject site = e.getAsJsonObject();

        ApprovedSite compare = null;
        if (site.get("id").getAsLong() == site1.getId().longValue()) {
            compare = site1;
        } else if (site.get("id").getAsLong() == site2.getId().longValue()) {
            compare = site2;
        }

        if (compare == null) {
            fail("Could not find matching whitelisted site id: " + site.get("id").getAsString());
        } else {
            assertThat(site.get("clientId").getAsString(), equalTo(compare.getClientId()));
            assertThat(site.get("creationDate").getAsString(),
                    equalTo(formatter.print(compare.getCreationDate(), Locale.ENGLISH)));
            assertThat(site.get("accessDate").getAsString(),
                    equalTo(formatter.print(compare.getAccessDate(), Locale.ENGLISH)));
            if (site.get("timeoutDate").isJsonNull()) {
                assertNull(compare.getTimeoutDate());
            } else {
                assertThat(site.get("timeoutDate").getAsString(),
                        equalTo(formatter.print(compare.getTimeoutDate(), Locale.ENGLISH)));
            }
            assertThat(site.get("userId").getAsString(), equalTo(compare.getUserId()));
            assertThat(jsonArrayToStringSet(site.getAsJsonArray("allowedScopes")),
                    equalTo(compare.getAllowedScopes()));
            checked.add(compare);
        }
    }
    // make sure all of our clients were found
    assertThat(checked.containsAll(allApprovedSites), is(true));
}