Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniRegisterTemplateKmip.java
Go to the documentation of this file.
1 package com.p6r.kmipserverlib;
2 
3 import org.junit.*;
4 
5 import java.nio.charset.Charset;
6 
7 import static org.junit.Assert.assertEquals;
8 
19 
20  @BeforeClass
21  public static void oneTimeSetUp() {
22  // NOOP
23  System.out.println("@BeforeClass - oneTimeSetUp");
24  }
25 
26  @AfterClass
27  public static void oneTimeTearDown() {
28  // NOOP
29  System.out.println("@AfterClass - oneTimeTearDown");
30  }
31 
32  @Before
33  public void setUp() {
34  // NOOP
35  System.out.println("@Before - setUp");
36  }
37 
38  @After
39  public void tearDown() {
40  // NOOP
41  System.out.println("@After - tearDown");
42  }
43 
49  @Test
51  System.out.println("@Test - JNICall-RegisterTemplateKMIP");
52 
53  // -> this parser is multi-thread safe by using JNI monitor locking
54  // -> use one parser object per server thread is recommended
55  P6KMIPServerLib sl = new P6KMIPServerLib();
56 
57  // -> KMIP 1.3 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
58  String testMessage =
59  "<RequestMessage><RequestHeader><ProtocolVersion><ProtocolVersionMajor type=\"Integer\" value=\"1\"/><ProtocolVersionMinor type=\"Integer\" value=\"2\"/></ProtocolVersion><BatchCount type=\"Integer\" value=\"1\"/></RequestHeader><BatchItem><Operation type=\"Enumeration\" value=\"Register\"/><RequestPayload><ObjectType type=\"Enumeration\" value=\"Template\"/><TemplateAttribute><Attribute><AttributeName type=\"TextString\" value=\"Name\"/><AttributeValue><NameValue type=\"TextString\" value=\"TC-312-12-template8\"/><NameType type=\"Enumeration\" value=\"UninterpretedTextString\"/></AttributeValue></Attribute></TemplateAttribute><Template><Attribute><AttributeName type=\"TextString\" value=\"Object Group\"/><AttributeValue type=\"TextString\" value=\"Group1\"/></Attribute><Attribute><AttributeName type=\"TextString\" value=\"Application Specific Information\"/><AttributeValue><ApplicationNamespace type=\"TextString\" value=\"ssl\"/><ApplicationData type=\"TextString\" value=\"www.example.com\"/></AttributeValue></Attribute><Attribute><AttributeName type=\"TextString\" value=\"Contact Information\"/><AttributeValue type=\"TextString\" value=\"Joe\"/></Attribute><Attribute><AttributeName type=\"TextString\" value=\"x-Purpose\"/><AttributeValue type=\"TextString\" value=\"demonstration\"/></Attribute><Attribute><AttributeName type=\"TextString\" value=\"x-ID\"/><AttributeValue type=\"TextString\" value=\"TC-312-12-from-template\"/></Attribute></Template></RequestPayload></BatchItem></RequestMessage>";
60 
61  try {
62  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
63 
64  String libVersion = sl.getLibraryVersion();
65  System.out.println(libVersion);
66 
67  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
68  // -> the type of message: TTLV, XML, JSON can be determine by the mime type passed in the HTTP request, or lack of one if just using SSL connection
69  sl.setMessageBuffer(testMessage.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
70 
71  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
72  RequestHeader rh = sl.getRequestHeader();
73  assertEquals("1.2", rh.getProtocolVersion());
74 
75  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
76  for (int i = 0; i < rh.getBatchCount(); i++) {
77 
78  BatchItem bi = sl.getBatchItem(i + 1);
79  if (bi instanceof RegisterTemplateBatchItem) {
80  RegisterTemplateBatchItem tb = (RegisterTemplateBatchItem) bi;
81 
82  // -> batch id is not required if only one batch item is present
83  byte[] batchId = tb.getUniqueBatchId();
84  assertEquals(null, batchId);
85 
86  String[] names = tb.getTemplateNames();
87  assertEquals(null, names);
88 
89  String[] registerAttributes = tb.getTemplateAttributes();
90  assertEquals(1, registerAttributes.length);
91  assertEquals("Name: TC-312-12-template8 - text_string", registerAttributes[0]);
92 
93  String[] objAttributes = tb.getAttributeList();
94  assertEquals(5, objAttributes.length);
95  assertEquals("Object Group: Group1", objAttributes[0]);
96  assertEquals("Application Specific Information: ssl - www.example.com", objAttributes[1]);
97  assertEquals("Contact Information: Joe", objAttributes[2]);
98  assertEquals("x-Purpose: demonstration", objAttributes[3]);
99  assertEquals("x-ID: TC-312-12-from-template", objAttributes[4]);
100  }
101  }
102  sl.freeLibrary();
103 
104  } catch (Exception e) {
105  // -> we shoud not get here
106  System.out.println(e.toString());
107  assertEquals(0, 1);
108  }
109 
110  }
111 }
void JNICall_RegisterTemplateKMIP()
Test: Verify parser can handle an XML formated Register Template operation.
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.