1
- // Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved.
1
+ // Copyright 2018, 2019 Oracle Corporation and/or its affiliates. All rights reserved.
2
2
// Licensed under the Universal Permissive License v 1.0 as shown at
3
3
// http://oss.oracle.com/licenses/upl.
4
4
8
8
import static oracle .kubernetes .operator .logging .MessageKeys .WLS_HEALTH_READ_FAILED ;
9
9
import static oracle .kubernetes .operator .logging .MessageKeys .WLS_HEALTH_READ_FAILED_NO_HTTPCLIENT ;
10
10
import static org .hamcrest .MatcherAssert .assertThat ;
11
- import static org .junit .Assert .*;
11
+ import static org .hamcrest .Matchers .is ;
12
+ import static org .hamcrest .core .IsNull .nullValue ;
12
13
13
14
import com .meterware .simplestub .Memento ;
14
15
import com .meterware .simplestub .Stub ;
15
16
import io .kubernetes .client .models .V1Service ;
17
+ import io .kubernetes .client .models .V1ServicePort ;
18
+ import io .kubernetes .client .models .V1ServiceSpec ;
16
19
import java .util .ArrayList ;
17
20
import java .util .List ;
21
+ import java .util .concurrent .ConcurrentHashMap ;
18
22
import java .util .logging .Level ;
19
23
import java .util .logging .LogRecord ;
20
24
import oracle .kubernetes .TestUtils ;
21
25
import oracle .kubernetes .operator .ProcessingConstants ;
22
26
import oracle .kubernetes .operator .http .HttpClient ;
27
+ import oracle .kubernetes .operator .http .HttpClientStub ;
23
28
import oracle .kubernetes .operator .steps .ReadHealthStep .ReadHealthWithHttpClientStep ;
24
29
import oracle .kubernetes .operator .work .NextAction ;
25
30
import oracle .kubernetes .operator .work .Packet ;
26
31
import oracle .kubernetes .operator .work .Step ;
32
+ import oracle .kubernetes .weblogic .domain .model .ServerHealth ;
27
33
import org .junit .After ;
28
34
import org .junit .Before ;
29
35
import org .junit .Test ;
@@ -74,25 +80,55 @@ public void withHttpClientStep_logIfMissingHTTPClient() {
74
80
V1Service service = Stub .createStub (V1ServiceStub .class );
75
81
Step next = new MockStep (null );
76
82
final String SERVER_NAME = "admin-server" ;
77
- Packet packet = Stub .createStub (PacketStub .class ).withServerName (SERVER_NAME );
83
+ Packet packet =
84
+ Stub .createStub (PacketStub .class ).withServerName (SERVER_NAME ).withGetKeyReturnValue (null );
78
85
79
86
ReadHealthWithHttpClientStep withHttpClientStep =
80
87
new ReadHealthWithHttpClientStep (service , next );
81
88
withHttpClientStep .apply (packet );
82
89
83
90
assertThat (logRecords , containsInfo (WLS_HEALTH_READ_FAILED_NO_HTTPCLIENT , SERVER_NAME ));
91
+ assertThat (packet .get (ProcessingConstants .SERVER_HEALTH_READ ), nullValue ());
92
+ }
93
+
94
+ @ Test
95
+ public void withHttpClientStep_putServerHealthReadToPacketIfSucceeded () {
96
+ V1Service service = Stub .createStub (V1ServiceStub .class );
97
+ Step next = new MockStep (null );
98
+ final String SERVER_NAME = "admin-server" ;
99
+
100
+ HttpClientStub httpClientStub = Stub .createStub (HttpClientStub .class );
101
+
102
+ Packet packet =
103
+ Stub .createStub (PacketStub .class )
104
+ .withServerName (SERVER_NAME )
105
+ .withGetKeyReturnValue (httpClientStub );
106
+ packet .put (
107
+ ProcessingConstants .SERVER_HEALTH_MAP , new ConcurrentHashMap <String , ServerHealth >());
108
+
109
+ ReadHealthWithHttpClientStep withHttpClientStep =
110
+ new ReadHealthWithHttpClientStep (service , next );
111
+ withHttpClientStep .apply (packet );
112
+
113
+ assertThat (packet .get (ProcessingConstants .SERVER_HEALTH_READ ), is (Boolean .TRUE ));
84
114
}
85
115
86
116
abstract static class PacketStub extends Packet {
87
117
88
118
String serverName ;
89
119
boolean getKeyThrowsException ;
120
+ HttpClient getKeyReturnValue ;
90
121
91
122
PacketStub withGetKeyThrowsException (boolean getKeyThrowsException ) {
92
123
this .getKeyThrowsException = getKeyThrowsException ;
93
124
return this ;
94
125
}
95
126
127
+ PacketStub withGetKeyReturnValue (HttpClient getKeyReturnValue ) {
128
+ this .getKeyReturnValue = getKeyReturnValue ;
129
+ return this ;
130
+ }
131
+
96
132
PacketStub withServerName (String serverName ) {
97
133
this .serverName = serverName ;
98
134
return this ;
@@ -104,15 +140,24 @@ public Object get(Object key) {
104
140
if (getKeyThrowsException ) {
105
141
throw CLASSCAST_EXCEPTION ; // to go to catch clause in WithHttpClientStep.apply() method
106
142
}
107
- return null ;
143
+ return getKeyReturnValue ;
108
144
} else if (ProcessingConstants .SERVER_NAME .equals (key )) {
109
145
return serverName ;
110
146
}
111
147
return super .get (key );
112
148
}
113
149
}
114
150
115
- public abstract static class V1ServiceStub extends V1Service {}
151
+ public abstract static class V1ServiceStub extends V1Service {
152
+
153
+ @ Override
154
+ public V1ServiceSpec getSpec () {
155
+ List <V1ServicePort > ports = new ArrayList <>();
156
+ ports .add (new V1ServicePort ().port (7001 ));
157
+ V1ServiceSpec v1ServiceSpec = new V1ServiceSpec ().clusterIP ("127.0.0.1" ).ports (ports );
158
+ return v1ServiceSpec ;
159
+ }
160
+ }
116
161
117
162
static class MockStep extends Step {
118
163
public MockStep (Step next ) {
0 commit comments