Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniModifyAttributeKmip.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 
18 public class JniModifyAttributeKmip {
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
50  public void JniModifyAttributeKmip() {
51  System.out.println("@Test - JNICall-ModifyAttributeKMIP");
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 2.0 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
58  String testMessage20_1 = "<RequestMessage>\n" +
59  "<RequestHeader>\n" +
60  " <ProtocolVersion>\n" +
61  " <ProtocolVersionMajor type=\"Integer\" value=\"2\"/>\n" +
62  " <ProtocolVersionMinor type=\"Integer\" value=\"0\"/>\n" +
63  " </ProtocolVersion>\n" +
64  " <ClientCorrelationValue type=\"TextString\" value=\"AKLC-M-3-20 step=4\"/>\n" +
65  " <BatchCount type=\"Integer\" value=\"1\"/>\n" +
66  "</RequestHeader>\n" +
67  "<BatchItem>\n" +
68  " <Operation type=\"Enumeration\" value=\"ModifyAttribute\"/>\n" +
69  " <RequestPayload>\n" +
70  " <UniqueIdentifier type=\"TextString\" value=\"e82a80f0-fe20-4cbb-a1b6-00e3a81a7e9f\"/>\n" +
71  " <CurrentAttribute>\n" +
72  " <Name>\n" +
73  " <NameValue type=\"TextString\" value=\"Fake-Modify-test-23\"/>\n" +
74  " <NameType type=\"Enumeration\" value=\"UninterpretedTextString\"/>\n" +
75  " </Name>\n" +
76  " </CurrentAttribute>\n" +
77  " <NewAttribute>\n" +
78  " <ActivationDate type=\"DateTime\" value=\"2018-03-18T15:16:30Z\"/>\n" +
79  " </NewAttribute>\n" +
80  " </RequestPayload>\n" +
81  "</BatchItem>\n" +
82  "</RequestMessage>";
83 
84  String testMessage20_2 = "<RequestMessage>\n" +
85  "<RequestHeader>\n" +
86  " <ProtocolVersion>\n" +
87  " <ProtocolVersionMajor type=\"Integer\" value=\"2\"/>\n" +
88  " <ProtocolVersionMinor type=\"Integer\" value=\"0\"/>\n" +
89  " </ProtocolVersion>\n" +
90  " <ClientCorrelationValue type=\"TextString\" value=\"AKLC-M-3-20 step=4\"/>\n" +
91  " <BatchCount type=\"Integer\" value=\"1\"/>\n" +
92  "</RequestHeader>\n" +
93  "<BatchItem>\n" +
94  " <Operation type=\"Enumeration\" value=\"AddAttribute\"/>\n" +
95  " <RequestPayload>\n" +
96  " <UniqueIdentifier type=\"TextString\" value=\"f82a80f0-fe20-4cbb-a1b6-00e3a81a7e90\"/>\n" +
97  " <CurrentAttribute>\n" +
98  " <Name>\n" +
99  " <NameValue type=\"TextString\" value=\"JackTheGermanShepherd\"/>\n" +
100  " <NameType type=\"Enumeration\" value=\"UninterpretedTextString\"/>\n" +
101  " </Name>\n" +
102  " </CurrentAttribute>\n" +
103  " </RequestPayload>\n" +
104  "</BatchItem>\n" +
105  "</RequestMessage>";
106 
107  String testMessage14 = "<RequestMessage>\n" +
108  "<RequestHeader>\n" +
109  "<ProtocolVersion>\n" +
110  "<ProtocolVersionMajor type=\"Integer\" value=\"1\"/>\n" +
111  "<ProtocolVersionMinor type=\"Integer\" value=\"4\"/>\n" +
112  "</ProtocolVersion>\n" +
113  "<BatchCount type=\"Integer\" value=\"1\"/>\n" +
114  "</RequestHeader>\n" +
115  "<BatchItem>\n" +
116  " <Operation type=\"Enumeration\" value=\"ModifyAttribute\"/>\n" +
117  " <RequestPayload>\n" +
118  " <UniqueIdentifier type=\"TextString\" value=\"91bdcfb4-130b-439d-af45-de781286e9e4\"/>\n" +
119  " <Attribute>\n" +
120  " <AttributeName type=\"TextString\" value=\"Activation Date\"/>\n" +
121  " <AttributeValue type=\"DateTime\" value=\"2018-03-19T14:28:10Z\"/>\n" +
122  " </Attribute>\n" +
123  " </RequestPayload>\n" +
124  "</BatchItem>\n" +
125  "</RequestMessage>";
126 
127  try {
128  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
129 
130  String libVersion = sl.getLibraryVersion();
131  System.out.println(libVersion);
132 
133  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
134  // -> the type of message: TTLC, XML, JSON can be determine by the mime type passed in the HTTP request, or lack of one if just using SSL connection
135  sl.setMessageBuffer(testMessage20_1.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
136 
137  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
138  RequestHeader rh = sl.getRequestHeader();
139  assertEquals("2.0", rh.getProtocolVersion());
140 
141  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
142  for (int i = 0; i < rh.getBatchCount(); i++) {
143 
144  BatchItem bi = sl.getBatchItem(i + 1);
145  if (bi instanceof ModifyAttribute2BatchItem) {
146  ModifyAttribute2BatchItem rb = (ModifyAttribute2BatchItem) bi;
147 
148  String uniqueId = rb.getUniqueId();
149  assertEquals("e82a80f0-fe20-4cbb-a1b6-00e3a81a7e9f", uniqueId);
150 
151  String currentAttribute = rb.getCurrentAttribute();
152  assertEquals("Name: Fake-Modify-test-23 - text_string", currentAttribute);
153 
154  String newAttribute = rb.getNewAttribute();
155  assertEquals("Activation Date: 2018-3-18T15:16:30Z", newAttribute);
156  }
157  }
158 
159  // -> test KMIP 2.0 version of Add Attribute
160  sl.setMessageBuffer(testMessage20_2.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
161  rh = sl.getRequestHeader();
162  assertEquals("2.0", rh.getProtocolVersion());
163 
164  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
165  for (int i = 0; i < rh.getBatchCount(); i++) {
166 
167  BatchItem bi = sl.getBatchItem(i + 1);
168  if (bi instanceof AddAttributeBatchItem) {
169  AddAttributeBatchItem rb = (AddAttributeBatchItem) bi;
170 
171  String uniqueId = rb.getUniqueId();
172  assertEquals("f82a80f0-fe20-4cbb-a1b6-00e3a81a7e90", uniqueId);
173 
174  String currentAttribute = rb.getAttribute();
175  assertEquals("Name: JackTheGermanShepherd - text_string", currentAttribute);
176  }
177  }
178 
179  // -> show that it works for KMIP 1.x as well
180  sl.setMessageBuffer(testMessage14.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
181  rh = sl.getRequestHeader();
182  assertEquals("1.4", rh.getProtocolVersion());
183 
184  for (int i = 0; i < rh.getBatchCount(); i++) {
185 
186  BatchItem bi = sl.getBatchItem(i + 1);
187  if (bi instanceof ModifyAttributeBatchItem) {
188  ModifyAttributeBatchItem rb = (ModifyAttributeBatchItem) bi;
189 
190  String uniqueId = rb.getUniqueId();
191  assertEquals("91bdcfb4-130b-439d-af45-de781286e9e4", uniqueId);
192 
193  String attribute = rb.getAttribute();
194  assertEquals("Activation Date: 2018-3-19T14:28:10Z", attribute);
195  }
196  }
197 
198  sl.freeLibrary();
199 
200  } catch (Exception e) {
201  // -> we shoud not get here
202  System.out.println(e.toString());
203  assertEquals(0, 1);
204  }
205  }
206 }
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.
void JniModifyAttributeKmip()
Test: Modify Attribute operation for KMIP 1.x and 2.0.