예제 #1
0
 protected static Configuration getDefaultPluginConfiguration(ResourceType resourceType) {
   ConfigurationTemplate pluginConfigDefaultTemplate =
       resourceType.getPluginConfigurationDefinition().getDefaultTemplate();
   return (pluginConfigDefaultTemplate != null)
       ? pluginConfigDefaultTemplate.createConfiguration()
       : new Configuration();
 }
  public ConfigurationDefinitionUpdateReport updateConfigurationDefinition(
      ConfigurationDefinition newDefinition, ConfigurationDefinition existingDefinition) {

    ConfigurationDefinitionUpdateReport updateReport =
        new ConfigurationDefinitionUpdateReport(existingDefinition);

    /*
     * handle grouped and ungrouped properties separately. for ungrouped, we don't need to care about the group, but
     * for the grouped ones we need to start at group level and then look at the properties. This is done below.
     *
     * First look at the ungrouped ones.
     */

    List<PropertyDefinition> existingPropertyDefinitions =
        existingDefinition.getNonGroupedProperties();
    List<PropertyDefinition> newPropertyDefinitions = newDefinition.getNonGroupedProperties();
    if (existingPropertyDefinitions != null) {
      for (PropertyDefinition newProperty : newPropertyDefinitions) {
        PropertyDefinition existingProp = existingDefinition.get(newProperty.getName());
        if (existingProp != null) {
          log.debug("Updating nonGrouped property [" + existingProp + "]");

          updatePropertyDefinition(existingProp, newProperty);
          updateReport.addUpdatedPropertyDefinition(newProperty);
        } else {
          log.debug("Adding nonGrouped property [" + newProperty + "]");

          existingDefinition.put(newProperty);
          updateReport.addNewPropertyDefinition(newProperty);
        }
      }

      existingDefinition =
          removeNoLongerUsedProperties(
              newDefinition, existingDefinition, existingPropertyDefinitions);

    } else {
      // TODO what if existingDefinitions is null?
      // we probably don't run in here, as the initial persisting is done
      // somewhere else.
    }

    /*
     * Now update / delete contained groups We need to be careful here, as groups are present in PropertyDefinition
     * as "backlink" from PropertyDefinition to group
     */
    List<PropertyGroupDefinition> existingGroups = existingDefinition.getGroupDefinitions();
    List<PropertyGroupDefinition> newGroups = newDefinition.getGroupDefinitions();

    List<PropertyGroupDefinition> toPersist = missingInFirstList(existingGroups, newGroups);
    List<PropertyGroupDefinition> toDelete = missingInFirstList(newGroups, existingGroups);
    List<PropertyGroupDefinition> toUpdate = intersection(existingGroups, newGroups);

    // delete groups no longer present
    for (PropertyGroupDefinition group : toDelete) {
      List<PropertyDefinition> groupedDefinitions =
          existingDefinition.getPropertiesInGroup(group.getName());

      // first look for contained stuff
      for (PropertyDefinition def : groupedDefinitions) {
        log.debug("Removing property [" + def + "] from group [" + group + "]");

        existingPropertyDefinitions.remove(def);
        existingDefinition.getPropertyDefinitions().remove(def.getName());
        def.setPropertyGroupDefinition(null);
        entityManager.remove(def);
      }

      // then remove the definition itself
      log.debug("Removing group [" + group + "]");

      existingGroups.remove(group);
      entityManager.remove(group);
    }

    // update existing groups that stay
    for (PropertyGroupDefinition group : toUpdate) {
      String groupName = group.getName();

      List<PropertyDefinition> newGroupedDefinitions =
          newDefinition.getPropertiesInGroup(groupName);
      for (PropertyDefinition nDef : newGroupedDefinitions) {
        PropertyDefinition existingProperty =
            existingDefinition.getPropertyDefinitions().get(nDef.getName());
        if (existingProperty != null) {
          log.debug("Updating property [" + nDef + "] in group [" + group + "]");

          updatePropertyDefinition(existingProperty, nDef);
          updateReport.addUpdatedPropertyDefinition(nDef);

        } else {
          log.debug("Adding property [" + nDef + "] to group [" + group + "]");

          existingDefinition.put(nDef);
          updateReport.addNewPropertyDefinition(nDef);
        }
      }

      // delete outdated properties of this group
      existingDefinition =
          removeNoLongerUsedProperties(
              newDefinition,
              existingDefinition,
              existingDefinition.getPropertiesInGroup(groupName));
    }

    // persist new groups
    for (PropertyGroupDefinition group : toPersist) {

      // First persist a new group definition and then link the properties to it
      log.debug("Persisting new group [" + group + "]");

      entityManager.persist(group);
      existingGroups.add(group); // iterating over this does not update the underlying crap

      List<PropertyDefinition> defs = newDefinition.getPropertiesInGroup(group.getName());
      Map<String, PropertyDefinition> exPDefs = existingDefinition.getPropertyDefinitions();
      for (PropertyDefinition def : defs) {
        entityManager.persist(def);
        def.setPropertyGroupDefinition(group);
        def.setConfigurationDefinition(existingDefinition);

        if (!exPDefs.containsKey(def.getName())) {
          updateReport.addNewPropertyDefinition(def);
        }

        exPDefs.put(def.getName(), def);
      }
    }

    /*
     * Now work on the templates.
     */
    Map<String, ConfigurationTemplate> existingTemplates = existingDefinition.getTemplates();
    Map<String, ConfigurationTemplate> newTemplates = newDefinition.getTemplates();
    List<String> toRemove = new ArrayList<String>();
    List<String> templatesToUpdate = new ArrayList<String>();

    for (String name : existingTemplates.keySet()) {
      if (newTemplates.containsKey(name)) {
        templatesToUpdate.add(name);
      } else {
        toRemove.add(name);
      }
    }

    for (String name : toRemove) {
      log.debug("Removing template [" + name + "]");

      ConfigurationTemplate template = existingTemplates.remove(name);
      entityManager.remove(template);
    }

    for (String name : templatesToUpdate) {
      log.debug("Updating template [" + name + "]");

      updateTemplate(existingDefinition.getTemplate(name), newTemplates.get(name));
    }

    for (String name : newTemplates.keySet()) {
      // add completely new templates
      if (!existingTemplates.containsKey(name)) {
        log.debug("Adding template [" + name + "]");

        ConfigurationTemplate newTemplate = newTemplates.get(name);

        // we need to set a valid configurationDefinition, where we will live on.
        newTemplate.setConfigurationDefinition(existingDefinition);

        entityManager.persist(newTemplate);
        existingTemplates.put(name, newTemplate);
      }
    }

    return updateReport;
  }
  private void updateTemplate(ConfigurationTemplate existingDT, ConfigurationTemplate newDT) {

    try {
      Configuration existConf = existingDT.getConfiguration();
      Configuration newConf = newDT.getConfiguration();
      Collection<String> exNames = existConf.getNames();
      Collection<String> newNames = newConf.getNames();
      List<String> toRemove = new ArrayList<String>();
      for (String name : exNames) {
        Property prop = newConf.get(name);
        if (prop instanceof PropertySimple) {
          PropertySimple ps = newConf.getSimple(name);
          if (ps != null) {
            Property eprop = existConf.get(name);
            if (eprop instanceof PropertySimple) {
              PropertySimple exps = existConf.getSimple(name);
              if (ps.getStringValue() != null) {
                exps.setStringValue(ps.getStringValue());
                //                                System.out.println("  updated " + name + " to
                // value " + ps.getStringValue());
              }
            } else {
              if (eprop != null) {
                //                                System.out.println("Can't yet handle target prop:
                // " + eprop);
              }
            }
          } else { // property not in new template -> deleted
            toRemove.add(name);
          }
        } else {
          if (prop != null) {
            //                        System.out.println("Can't yet handle source prop: " + prop);
          }
        }
      }
      for (String name : toRemove) existConf.remove(name);

      // now check for new names and add them
      for (String name : newNames) {
        if (!exNames.contains(name)) {
          Property prop = newConf.get(name);
          if (prop instanceof PropertySimple) {
            PropertySimple ps = newConf.getSimple(name);
            if (ps.getStringValue() != null) {
              // TODO add a new property
              //                            Collection<Property> properties =
              // existConf.getProperties();
              //                            properties = new ArrayList<Property>(properties);
              //                            properties.add(ps);
              //                            existConf.setProperties(properties);
              Property property = ps.deepCopy(false);
              existConf.put(property);
            }
          }
        }
      }

      entityManager.flush();
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }
예제 #4
0
  @SuppressWarnings({"rawtypes", "unchecked"})
  @Test
  public void loadResourceConfigurationWithType() throws Exception {
    // tell the method story as it happens: mock or create dependencies and configure
    // those dependencies to get the method under test to completion.
    ResourceContext mockResourceContext = mock(ResourceContext.class);

    ResourceType mockResourceType = mock(ResourceType.class);
    when(mockResourceContext.getResourceType()).thenReturn(mockResourceType);

    ConfigurationDefinition mockConfigurationDefinition = mock(ConfigurationDefinition.class);
    when(mockResourceType.getResourceConfigurationDefinition())
        .thenReturn(mockConfigurationDefinition);

    ConfigurationTemplate mockConfigurationTemplate = mock(ConfigurationTemplate.class);
    when(mockConfigurationDefinition.getDefaultTemplate()).thenReturn(mockConfigurationTemplate);

    Configuration mockConfiguration = mock(Configuration.class);
    when(mockConfigurationTemplate.getConfiguration()).thenReturn(mockConfiguration);

    Property mockProperty = mock(Property.class);
    when(mockConfiguration.get(eq("__type"))).thenReturn(mockProperty);

    Map<String, PropertyDefinition> mockMap = (Map<String, PropertyDefinition>) mock(Map.class);
    when(mockConfigurationDefinition.getPropertyDefinitions()).thenReturn(mockMap);

    ConfigurationLoadDelegate mockConfigurationLoadDelegate = mock(ConfigurationLoadDelegate.class);
    PowerMockito.whenNew(ConfigurationLoadDelegate.class)
        .withParameterTypes(ConfigurationDefinition.class, ASConnection.class, Address.class)
        .withArguments(
            any(ConfigurationDefinition.class), any(ASConnection.class), any(Address.class))
        .thenReturn(mockConfigurationLoadDelegate);

    when(mockConfigurationLoadDelegate.loadResourceConfiguration()).thenReturn(mockConfiguration);

    PropertySimple pathPropertySimple = new PropertySimple("path", "abc=def,xyz=test1");
    when(mockConfiguration.get(eq("path"))).thenReturn(pathPropertySimple);
    when(mockResourceContext.getPluginConfiguration()).thenReturn(mockConfiguration);

    ASConnection mockASConnection = mock(ASConnection.class);

    PropertySimple typePropertySimple = new PropertySimple("__type", "xyz");
    PowerMockito.whenNew(PropertySimple.class)
        .withParameterTypes(String.class, Object.class)
        .withArguments(eq("__type"), eq("xyz"))
        .thenReturn(typePropertySimple);

    // create object to test and inject required dependencies
    TemplatedComponent objectUnderTest = new TemplatedComponent();

    objectUnderTest.context = mockResourceContext;
    objectUnderTest.testConnection = mockASConnection;

    // run code under test
    Configuration result = objectUnderTest.loadResourceConfiguration();

    // verify the results (Assert and mock verification)
    Assert.assertEquals(result, mockConfiguration);

    verify(mockMap, times(1)).remove(eq("__type"));
    verify(mockConfiguration, times(1)).get(eq("__type"));
    verify(mockConfiguration, never()).get(eq("__name"));
    verify(mockConfiguration, times(1)).get(eq("path"));
    verify(mockConfiguration, times(1)).put(eq(typePropertySimple));

    PowerMockito.verifyNew(PropertySimple.class).withArguments(eq("__type"), eq("xyz"));
    PowerMockito.verifyNew(ConfigurationLoadDelegate.class)
        .withArguments(
            any(ConfigurationDefinition.class), eq(mockASConnection), any(Address.class));
  }
예제 #5
0
  @SuppressWarnings({"rawtypes", "unchecked"})
  @Test
  public void updateResourceConfigurationWithName() throws Exception {
    // tell the method story as it happens: mock or create dependencies and configure
    // those dependencies to get the method under test to completion.
    ResourceContext mockResourceContext = mock(ResourceContext.class);

    ResourceType mockResourceType = mock(ResourceType.class);
    when(mockResourceContext.getResourceType()).thenReturn(mockResourceType);

    ConfigurationDefinition mockConfigurationDefinition = mock(ConfigurationDefinition.class);
    when(mockResourceType.getResourceConfigurationDefinition())
        .thenReturn(mockConfigurationDefinition);

    ConfigurationDefinition mockConfigurationDefinitionCopy = mock(ConfigurationDefinition.class);
    when(mockConfigurationDefinition.copy()).thenReturn(mockConfigurationDefinitionCopy);

    ConfigurationTemplate mockConfigurationTemplate = mock(ConfigurationTemplate.class);
    when(mockConfigurationDefinitionCopy.getDefaultTemplate())
        .thenReturn(mockConfigurationTemplate);

    Configuration mockConfiguration = mock(Configuration.class);
    when(mockConfigurationTemplate.getConfiguration()).thenReturn(mockConfiguration);

    Property mockProperty = mock(Property.class);
    when(mockConfiguration.get(eq("__type"))).thenReturn(null);
    when(mockConfiguration.get(eq("__name"))).thenReturn(mockProperty);

    Map<String, PropertyDefinition> mockMap = (Map<String, PropertyDefinition>) mock(Map.class);
    when(mockConfigurationDefinitionCopy.getPropertyDefinitions()).thenReturn(mockMap);

    ConfigurationUpdateReport mockReport = mock(ConfigurationUpdateReport.class);
    when(mockReport.getConfiguration()).thenReturn(mockConfiguration);

    ConfigurationWriteDelegate mockConfigurationWriteDelegate =
        mock(ConfigurationWriteDelegate.class);
    PowerMockito.whenNew(ConfigurationWriteDelegate.class)
        .withParameterTypes(ConfigurationDefinition.class, ASConnection.class, Address.class)
        .withArguments(
            any(ConfigurationDefinition.class), any(ASConnection.class), any(Address.class))
        .thenReturn(mockConfigurationWriteDelegate);

    ASConnection mockASConnection = mock(ASConnection.class);
    when(mockASConnection.execute(any(ReadResource.class))).thenReturn(new Result());

    // create object to test and inject required dependencies
    TemplatedComponent objectUnderTest = new TemplatedComponent();

    objectUnderTest.context = mockResourceContext;
    objectUnderTest.testConnection = mockASConnection;

    // run code under test
    objectUnderTest.updateResourceConfiguration(mockReport);

    // verify the results (Assert and mock verification)
    verify(mockMap, times(1)).remove(eq("__name"));
    verify(mockConfiguration, times(1)).get(eq("__type"));
    verify(mockConfiguration, times(1)).get(eq("__name"));
    verify(mockConfiguration, times(1)).remove(eq("__name"));

    PowerMockito.verifyNew(ConfigurationWriteDelegate.class)
        .withArguments(
            any(ConfigurationDefinition.class), eq(mockASConnection), any(Address.class));
  }