Exemplo n.º 1
0
 @Test
 public void testExample() throws FileNotFoundException {
   CompositeNode loadedCompositeNode =
       TestUtils.readInputToCnSn(
           "/parts/ietf-interfaces_interfaces.xml", XmlToCompositeNodeProvider.INSTANCE);
   BrokerFacade brokerFacade = mock(BrokerFacade.class);
   when(brokerFacade.readOperationalData(any(InstanceIdentifier.class)))
       .thenReturn(loadedCompositeNode);
   assertEquals(loadedCompositeNode, brokerFacade.readOperationalData(null));
 }
  @Test
  public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
    initMocking();
    RpcResult<TransactionStatus> rpcResult =
        new DummyRpcResult.Builder<TransactionStatus>().result(TransactionStatus.COMMITED).build();

    when(brokerFacade.commitConfigurationDataPost(
            any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
        .thenReturn(mock(CheckedFuture.class));

    ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor =
        ArgumentCaptor.forClass(YangInstanceIdentifier.class);
    ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);

    String URI_1 = "/config";
    assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
    verify(brokerFacade)
        .commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
    String identifier =
        "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
    assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());

    String URI_2 = "/config/test-interface:interfaces";
    assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
    verify(brokerFacade, times(2))
        .commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
    identifier =
        "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces, (urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)block]";
    assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
  }
  private void mockInvokeRpc(CompositeNode result, boolean sucessful, Collection<RpcError> errors) {

    DummyRpcResult.Builder<CompositeNode> builder =
        new DummyRpcResult.Builder<CompositeNode>().result(result).isSuccessful(sucessful);
    if (!errors.isEmpty()) {
      builder.errors(errors);
    }
    RpcResult<CompositeNode> rpcResult = builder.build();
    when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class)))
        .thenReturn(Futures.<RpcResult<CompositeNode>>immediateFuture(rpcResult));
  }
  @Test
  public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
    initMocking();

    when(brokerFacade.commitConfigurationDataPost(
            any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
        .thenReturn(null);

    String URI_1 = "/config";
    assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));

    String URI_2 = "/config/test-interface:interfaces";
    assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
  }
  @Test
  public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
    controllerContext.setSchemas(schemaContextYangsIetf);
    when(brokerFacade.commitConfigurationDataPost(
            any(DOMMountPoint.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
        .thenReturn(mock(CheckedFuture.class));

    DOMMountPoint mountInstance = mock(DOMMountPoint.class);
    when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
    DOMMountPointService mockMountService = mock(DOMMountPointService.class);
    when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
        .thenReturn(Optional.of(mountInstance));

    ControllerContext.getInstance().setMountService(mockMountService);

    String uri = "/config/ietf-interfaces:interfaces/interface/0/";
    assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
    uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
    assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));

    assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
  }